Class: SyntaxTree::Haml::PrettyPrint

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Visitor

#visit

Constructor Details

#initialize(q) ⇒ PrettyPrint

Returns a new instance of PrettyPrint.



8
9
10
# File 'lib/syntax_tree/haml/pretty_print.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/pretty_print.rb', line 6

def q
  @q
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/pretty_print.rb', line 13

def visit_comment(node)
  group("comment") do
    if node.value[:conditional]
      pp_field("conditional", node.value[:conditional])
    elsif node.value[:text]
      pp_field("text", node.value[:text])
    end

    bool_field("revealed") if node.value[:revealed]
    pp_field("children", node.children) if node.children.any?
  end
end

#visit_doctype(node) ⇒ Object



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

def visit_doctype(node)
  group("doctype") do
    if DOCTYPE_TYPES.key?(node.value[:type])
      pp_field("type", node.value[:type])
    elsif DOCTYPE_VERSIONS.include?(node.value[:version])
      pp_field("version", node.value[:version])
    else
      pp_field("text", node.value[:text])
    end

    pp_field("encoding", node.value[:encoding]) if node.value[:encoding]
  end
end

#visit_filter(node) ⇒ Object



42
43
44
45
46
47
# File 'lib/syntax_tree/haml/pretty_print.rb', line 42

def visit_filter(node)
  group("filter") do
    text_field("name", node.value[:name])
    pp_field("text", node.value[:text])
  end
end

#visit_haml_comment(node) ⇒ Object



50
51
52
# File 'lib/syntax_tree/haml/pretty_print.rb', line 50

def visit_haml_comment(node)
  group("haml_comment") { pp_field("text", node.value[:text]) }
end

#visit_plain(node) ⇒ Object



55
56
57
# File 'lib/syntax_tree/haml/pretty_print.rb', line 55

def visit_plain(node)
  group("plain") { pp_field("text", node.value[:text]) }
end

#visit_root(node) ⇒ Object

Visit the root node of the AST.



60
61
62
63
64
# File 'lib/syntax_tree/haml/pretty_print.rb', line 60

def visit_root(node)
  group("root") do
    pp_field("children", node.children) if node.children.any?
  end
end

#visit_script(node) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/syntax_tree/haml/pretty_print.rb', line 67

def visit_script(node)
  group("script") do
    pp_field("text", node.value[:text])
    bool_field("escape_html") if node.value[:escape_html]
    bool_field("preserve") if node.value[:preserve]
    pp_field("children", node.children) if node.children.any?
  end
end

#visit_silent_script(node) ⇒ Object



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

def visit_silent_script(node)
  group("silent-script") do
    pp_field("text", node.value[:text])
    pp_field("children", node.children) if node.children.any?
  end
end

#visit_tag(node) ⇒ Object

Visit a tag node.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/syntax_tree/haml/pretty_print.rb', line 85

def visit_tag(node)
  group("tag") do
    pp_field("name", node.value[:name])

    if node.value[:attributes].any?
      pp_field("attributes", node.value[:attributes])
    end

    if node.value[:dynamic_attributes].new
      pp_field(
        "dynamic_attributes.new",
        node.value[:dynamic_attributes].new
      )
    end

    if node.value[:dynamic_attributes].old
      pp_field(
        "dynamic_attributes.old",
        node.value[:dynamic_attributes].old
      )
    end

    if node.value[:object_ref] != :nil
      pp_field("object_ref", node.value[:object_ref])
    end

    if node.value[:nuke_outer_whitespace]
      bool_field("nuke_outer_whitespace")
    end

    if node.value[:nuke_inner_whitespace]
      bool_field("nuke_inner_whitespace")
    end

    bool_field("self_closing") if node.value[:self_closing]
    pp_field("value", node.value[:value]) if node.value[:value]
    pp_field("children", node.children) if node.children.any?
  end
end