Class: SyntaxTree::Haml::Format

Inherits:
Visitor
  • Object
show all
Defined in:
lib/syntax_tree/haml/format.rb

Defined Under Namespace

Classes: HTMLAttributesPart, HashAttributesPart, LiteralHashValue, PartList, PlainPart, PrefixPart

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Visitor

#visit

Constructor Details

#initialize(q) ⇒ Format

Returns a new instance of Format.



8
9
10
# File 'lib/syntax_tree/haml/format.rb', line 8

def initialize(q)
  @q = q
end

Instance Attribute Details

#qObject (readonly)

Returns the value of attribute q.



6
7
8
# File 'lib/syntax_tree/haml/format.rb', line 6

def q
  @q
end

Class Method Details

.hash_key(key) ⇒ Object



247
248
249
250
251
252
253
254
# File 'lib/syntax_tree/haml/format.rb', line 247

def self.hash_key(key)
  if key.match?(/^@|[-:]/)
    quote = SyntaxTree::Formatter::OPTIONS[:quote]
    "#{quote}#{Quotes.normalize(key, quote)}#{quote}:"
  else
    "#{key}:"
  end
end

.hash_value(value) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/syntax_tree/haml/format.rb', line 256

def self.hash_value(value)
  case value
  when LiteralHashValue
    value.value
  when String
    quote = SyntaxTree::Formatter::OPTIONS[:quote]
    "#{quote}#{Quotes.normalize(value, quote)}#{quote}"
  else
    value.to_s
  end
end

Instance Method Details

#visit_comment(node) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/syntax_tree/haml/format.rb', line 13

def visit_comment(node)
  with_children(node) do
    q.text("/")
    q.text("!") if node.value[:revealed]

    if node.value[:conditional]
      q.text(node.value[:conditional])
    elsif node.value[:text]
      q.text(" #{node.value[:text]}")
    end
  end
end

#visit_doctype(node) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/syntax_tree/haml/format.rb', line 27

def visit_doctype(node)
  parts = ["!!!"]

  parts << if DOCTYPE_TYPES.key?(node.value[:type])
    DOCTYPE_TYPES[node.value[:type]]
  elsif DOCTYPE_VERSIONS.include?(node.value[:version])
    node.value[:version]
  else
    node.value[:type]
  end

  parts << node.value[:encoding] if node.value[:encoding]
  q.text(parts.join(" "))
end

#visit_filter(node) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/syntax_tree/haml/format.rb', line 43

def visit_filter(node)
  q.group do
    q.text(":")
    q.text(node.value[:name])

    q.indent do
      q.breakable(force: true)

      segments = node.value[:text].strip.split("\n")
      q.seplist(segments, -> { q.breakable(force: true) }) do |segment|
        q.text(segment)
      end
    end
  end
end

#visit_haml_comment(node) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/syntax_tree/haml/format.rb', line 60

def visit_haml_comment(node)
  q.text("-#")
  text = node.value[:text].strip

  if text.include?("\n")
    q.indent do
      q.breakable(force: true)
      q.seplist(
        text.split("\n"),
        -> { q.breakable(force: true) }
      ) { |segment| q.text(segment) }
    end
  else
    q.text(" #{text}")
  end
end

#visit_plain(node) ⇒ Object



78
79
80
81
82
# File 'lib/syntax_tree/haml/format.rb', line 78

def visit_plain(node)
  text = node.value[:text]
  q.text("\\") if escaped?(text)
  q.text(text)
end

#visit_root(node) ⇒ Object

Visit the root node of the AST.



85
86
87
88
89
90
# File 'lib/syntax_tree/haml/format.rb', line 85

def visit_root(node)
  node.children.each do |child|
    visit(child)
    q.breakable(force: true)
  end
end

#visit_script(node) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/syntax_tree/haml/format.rb', line 93

def visit_script(node)
  with_children(node) do
    q.text("&") if node.value[:escape_html]

    node.value[:preserve] ? q.text("~") : q.text("=")

    q.text(" ")
    q.text(node.value[:text].strip)
  end
end

#visit_silent_script(node) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/syntax_tree/haml/format.rb', line 105

def visit_silent_script(node)
  q.group do
    q.text("- ")
    q.text(node.value[:text].strip)

    node.children.each do |child|
      if continuation?(node, child)
        q.breakable(force: true)
        visit(child)
      else
        q.indent do
          q.breakable(force: true)
          visit(child)
        end
      end
    end
  end
end

#visit_tag(node) ⇒ Object

Visit a tag node.



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/syntax_tree/haml/format.rb', line 269

def visit_tag(node)
  parts = PartList.new(node)

  # If we have a tag that isn't a div, then we need to print out that
  # name of that tag first. If it is a div, first we'll check if there
  # are any other things that would force us to print out the div
  # explicitly, and otherwise we'll leave it off.
  if node.value[:name] != "div"
    parts << PrefixPart.new("%", node.value[:name])
  end

  # If we have a class attribute, then we're going to print that here
  # using the special class syntax.
  if node.value[:attributes].key?("class")
    parts << PrefixPart.new(
      ".",
      node.value[:attributes]["class"].tr(" ", ".")
    )
  end

  # If we have an id attribute, then we're going to print that here
  # using the special id syntax.
  if node.value[:attributes].key?("id")
    parts << PrefixPart.new("#", node.value[:attributes]["id"])
  end

  # If we're using dynamic attributes on this tag, then they come in as
  # a string that looks like the output of Hash#inspect from Ruby. So
  # here we're going to split it all up and print it out nicely.
  if node.value[:dynamic_attributes].new
    parts << HTMLAttributesPart.new(node.value[:dynamic_attributes].new)
  end

  # If there are any static attributes that are not class or id (because
  # we already took care of those), then we're going to print them out
  # here.
  static =
    node.value[:attributes].reject do |key, _|
      key == "class" || key == "id"
    end

  parts << HashAttributesPart.new(static) if static.any?

  # If there are dynamic attributes that don't use the newer syntax, then
  # we're going to print them out here.
  if node.value[:dynamic_attributes].old
    parts << PlainPart.new("%div") if parts.empty?

    if ::Haml::AttributeParser.available?
      dynamic = parse_attributes(node.value[:dynamic_attributes].old)
      parts << if dynamic.is_a?(LiteralHashValue)
        PlainPart.new(dynamic.value)
      else
        HashAttributesPart.new(dynamic)
      end
    else
      parts << PlainPart.new(node.value[:dynamic_attributes].old)
    end
  end

  # https://haml.info/docs/yardoc/file.REFERENCE.html#object-reference-
  if node.value[:object_ref] != :nil
    parts << PlainPart.new("%div") if parts.empty?
    parts << PlainPart.new(node.value[:object_ref])
  end

  # https://haml.info/docs/yardoc/file.REFERENCE.html#whitespace-removal--and-
  parts << PlainPart.new(">") if node.value[:nuke_outer_whitespace]
  parts << PlainPart.new("<") if node.value[:nuke_inner_whitespace]

  # https://haml.info/docs/yardoc/file.REFERENCE.html#empty-void-tags-
  parts << PlainPart.new("/") if node.value[:self_closing]

  # If there is a value part, then we're going to print slightly
  # differently as the value goes after the tag declaration.
  if (value = node.value[:value]) && !value.empty?
    with_children(node) do
      q.group { parts.format(q) }
      q.indent do
        # Split between the declaration of the tag and the contents of the
        # tag.
        q.breakable("")

        if node.value[:parse] && value.match?(/#[{$@]/)
          # There's a weird case here where if the value includes
          # interpolation and it's marked as { parse: true }, then we
          # don't actually want the = prefix, and we want to remove extra
          # escaping.
          q.if_break { q.text("") }.if_flat { q.text(" ") }
          q.text(value[1...-1].gsub(/\\"/, "\""))
        elsif node.value[:parse]
          q.text("= ")
          q.text(value)
        else
          q.if_break { q.text("") }.if_flat { q.text(" ") }
          q.text(value)
        end
      end
    end
  else
    with_children(node) { parts.format(q) }
  end
end