@@ -123,6 +123,8 @@ def visit_silent_script(node)
123
123
124
124
LiteralHashValue = Struct . new ( :value )
125
125
126
+ StringHashValue = Struct . new ( :value , :quote )
127
+
126
128
# When formatting a tag, there are a lot of different kinds of things that
127
129
# can be printed out. There's the tag name, the attributes, the content,
128
130
# etc. This object is responsible for housing all of those parts.
@@ -220,19 +222,34 @@ def length
220
222
private
221
223
222
224
def format_value ( q , hash , level = 0 )
225
+ quote = SyntaxTree ::Formatter ::OPTIONS [ :quote ]
226
+
223
227
q . group do
224
228
q . text ( "{" )
225
229
q . indent do
226
230
q . group do
227
231
q . breakable ( level == 0 ? "" : " " )
228
232
q . seplist ( hash , nil , :each_pair ) do |key , value |
229
- q . text ( Format . hash_key ( key ) )
233
+ if key . match? ( /^@|[-:]/ )
234
+ q . text ( "#{ quote } #{ Quotes . normalize ( key , quote ) } #{ quote } :" )
235
+ else
236
+ q . text ( "#{ key } :" )
237
+ end
238
+
230
239
q . text ( " " )
231
240
232
- if value . is_a? ( Hash )
241
+ case value
242
+ when Hash
233
243
format_value ( q , value , level + 1 )
244
+ when LiteralHashValue
245
+ q . text ( value . value )
246
+ when StringLiteral
247
+ qq = Formatter . new ( "" )
248
+ qq . with_target ( q . target ) { value . format ( qq ) }
249
+ when String
250
+ q . text ( "#{ quote } #{ Quotes . normalize ( value , quote ) } #{ quote } " )
234
251
else
235
- q . text ( Format . hash_value ( value ) )
252
+ q . text ( value . to_s )
236
253
end
237
254
end
238
255
end
@@ -244,27 +261,6 @@ def format_value(q, hash, level = 0)
244
261
end
245
262
end
246
263
247
- def self . hash_key ( key )
248
- if key . match? ( /^@|[-:]/ )
249
- quote = SyntaxTree ::Formatter ::OPTIONS [ :quote ]
250
- "#{ quote } #{ Quotes . normalize ( key , quote ) } #{ quote } :"
251
- else
252
- "#{ key } :"
253
- end
254
- end
255
-
256
- def self . hash_value ( value )
257
- case value
258
- when LiteralHashValue
259
- value . value
260
- when String
261
- quote = SyntaxTree ::Formatter ::OPTIONS [ :quote ]
262
- "#{ quote } #{ Quotes . normalize ( value , quote ) } #{ quote } "
263
- else
264
- value . to_s
265
- end
266
- end
267
-
268
264
# Visit a tag node.
269
265
def visit_tag ( node )
270
266
parts = PartList . new ( node )
@@ -405,7 +401,7 @@ def parse_attributes(source)
405
401
::Haml ::AttributeParser . parse ( source )
406
402
parsed . to_h { |key , value | [ key , parse_attributes ( value ) ] }
407
403
in [ :program , [ [ :string_literal , *] , *] ]
408
- source [ 1 ...- 1 ]
404
+ SyntaxTree . parse ( source ) . statements . body [ 0 ]
409
405
else
410
406
LiteralHashValue . new ( source )
411
407
end
0 commit comments