Class: SyntaxTree::Haml::Tag

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

Defined Under Namespace

Classes: HTMLAttributesPart, HashAttributesPart, LiteralHashValue, PlainPart, PrefixPart

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Tag

Returns a new instance of Tag.



110
111
112
# File 'lib/syntax_tree/haml/tag.rb', line 110

def initialize(node)
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



108
109
110
# File 'lib/syntax_tree/haml/tag.rb', line 108

def node
  @node
end

Class Method Details

.hash_key(key) ⇒ Object



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

def self.hash_key(key)
  key.match?(/^@|[-:]/) ? "\"#{key}\":" : "#{key}:"
end

.hash_value(value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/syntax_tree/haml/tag.rb', line 12

def self.hash_value(value)
  case value
  when LiteralHashValue
    value.value
  when String
    "\"#{Quotes.normalize(value, "\"")}\""
  else
    value.to_s
  end
end

Instance Method Details

#format(q) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/syntax_tree/haml/tag.rb', line 114

def format(q)
  parts = []

  # 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 { |key, _| key == "class" || key == "id" }
  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 node.value[:value]
    return Haml.with_children(node, q) do
      q.group do
        align = 0

        parts.each do |part|
          part.format(q, align)
          align += part.length
        end
      end

      q.indent do
        # Split between the declaration of the tag and the contents of the
        # tag.
        q.breakable("")

        if node.value[:parse] && node.value[: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(node.value[:value][1...-1].gsub(/\\"/, "\""))
        elsif node.value[:parse]
          q.text("= ")
          q.text(node.value[:value])
        else
          q.if_break { q.text("") }.if_flat { q.text(" ") }
          q.text(node.value[:value])
        end
      end
    end
  end

  # In case none of the other if statements have matched and we're
  # printing a div, we need to explicitly add it back into the array.
  if parts.empty? && node.value[:name] == "div"
    parts << PlainPart.new("%div")
  end

  Haml.with_children(node, q) do
    align = 0

    parts.each do |part|
      part.format(q, align)
      align += part.length
    end
  end
end

#pretty_print(q) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/syntax_tree/haml/tag.rb', line 233

def pretty_print(q)
  q.group(2, "(tag", ")") do
    q.breakable
    q.text("name=")
    q.pp(node.value[:name])

    if node.value[:attributes].any?
      q.breakable
      q.text("attributes=")
      q.pp(node.value[:attributes])
    end

    if node.value[:dynamic_attributes].new
      q.breakable
      q.text("dynamic_attributes.new=")
      q.pp(node.value[:dynamic_attributes].new)
    end

    if node.value[:dynamic_attributes].old
      q.breakable
      q.text("dynamic_attributes.old=")
      q.pp(node.value[:dynamic_attributes].old)
    end

    if node.value[:object_ref] != :nil
      q.breakable
      q.text("object_ref=")
      q.pp(node.value[:object_ref])
    end

    if node.value[:nuke_outer_whitespace]
      q.breakable
      q.text("nuke_outer_whitespace")
    end

    if node.value[:nuke_inner_whitespace]
      q.breakable
      q.text("nuke_inner_whitespace")
    end

    if node.value[:self_closing]
      q.breakable
      q.text("self_closing")
    end

    if node.value[:value]
      q.breakable
      q.text("value=")
      q.pp(node.value[:value])
    end

    if node.children.any?
      q.breakable
      q.text("children=")
      q.pp(node.children)
    end
  end
end