Class: SyntaxTree::CSS::PrettyPrint

Inherits:
BasicVisitor show all
Defined in:
lib/syntax_tree/css/pretty_print.rb

Overview

A pretty-print visitor.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BasicVisitor

#visit, #visit_all, #visit_child_nodes

Constructor Details

#initialize(q) ⇒ PrettyPrint

Returns a new instance of PrettyPrint.



9
10
11
# File 'lib/syntax_tree/css/pretty_print.rb', line 9

def initialize(q)
  @q = q
end

Instance Attribute Details

#qObject (readonly)

Returns the value of attribute q.



7
8
9
# File 'lib/syntax_tree/css/pretty_print.rb', line 7

def q
  @q
end

Instance Method Details

#visit_at_keyword_token(node) ⇒ Object

Visit an AtKeywordToken node.



18
19
20
21
22
23
# File 'lib/syntax_tree/css/pretty_print.rb', line 18

def visit_at_keyword_token(node)
  token("at-keyword-token") do
    q.breakable
    q.pp(node.value)
  end
end

#visit_at_rule(node) ⇒ Object

Visit an AtRule node.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/syntax_tree/css/pretty_print.rb', line 26

def visit_at_rule(node)
  token("at-rule") do
    q.breakable
    q.pp(node.name)

    q.breakable
    q.text("(prelude")
    q.nest(2) do
      q.breakable("")
      q.seplist(node.prelude) { |token| q.pp(token) }
    end

    q.breakable("")
    q.text(")")

    q.breakable
    q.pp(node.block)
  end
end

#visit_bad_string_token(node) ⇒ Object

Visit a BadStringToken node.



47
48
49
50
51
52
# File 'lib/syntax_tree/css/pretty_print.rb', line 47

def visit_bad_string_token(node)
  token("bad-string-token") do
    q.breakable
    q.pp(node.value)
  end
end

#visit_bad_url_token(node) ⇒ Object

Visit a BadURLToken node.



55
56
57
58
59
60
# File 'lib/syntax_tree/css/pretty_print.rb', line 55

def visit_bad_url_token(node)
  token("bad-url-token") do
    q.breakable
    q.pp(node.value)
  end
end

#visit_cdc_token(node) ⇒ Object

Visit a CDCToken node.



63
64
65
# File 'lib/syntax_tree/css/pretty_print.rb', line 63

def visit_cdc_token(node)
  token("cdc-token")
end

#visit_cdo_token(node) ⇒ Object

Visit a CDOToken node.



68
69
70
# File 'lib/syntax_tree/css/pretty_print.rb', line 68

def visit_cdo_token(node)
  token("cdo-token")
end

#visit_class_selector(node) ⇒ Object

Visit a Selectors::ClassSelector node.



354
355
356
357
358
359
# File 'lib/syntax_tree/css/pretty_print.rb', line 354

def visit_class_selector(node)
  token("class-selector") do
    q.breakable
    q.pp(node.value)
  end
end

#visit_close_curly_token(node) ⇒ Object

Visit a CloseCurlyToken node.



73
74
75
# File 'lib/syntax_tree/css/pretty_print.rb', line 73

def visit_close_curly_token(node)
  token("close-curly-token")
end

#visit_close_paren_token(node) ⇒ Object

Visit a CloseParenToken node.



78
79
80
# File 'lib/syntax_tree/css/pretty_print.rb', line 78

def visit_close_paren_token(node)
  token("close-paren-token")
end

#visit_close_square_token(node) ⇒ Object

Visit a CloseSquareToken node.



83
84
85
# File 'lib/syntax_tree/css/pretty_print.rb', line 83

def visit_close_square_token(node)
  token("close-square-token")
end

#visit_colon_token(node) ⇒ Object

Visit a ColonToken node.



88
89
90
# File 'lib/syntax_tree/css/pretty_print.rb', line 88

def visit_colon_token(node)
  token("colon-token")
end

#visit_comma_token(node) ⇒ Object

Visit a CommaToken node.



101
102
103
# File 'lib/syntax_tree/css/pretty_print.rb', line 101

def visit_comma_token(node)
  token("comma-token")
end

#visit_comment_token(node) ⇒ Object

Visit a CommentToken node.



93
94
95
96
97
98
# File 'lib/syntax_tree/css/pretty_print.rb', line 93

def visit_comment_token(node)
  token("comment-token") do
    q.breakable
    q.pp(node.value)
  end
end

#visit_css_stylesheet(node) ⇒ Object

Visit a CSSStyleSheet node.



106
107
108
109
110
111
112
113
# File 'lib/syntax_tree/css/pretty_print.rb', line 106

def visit_css_stylesheet(node)
  token("css-stylesheet") do
    if node.rules.any?
      q.breakable
      q.seplist(node.rules) { |rule| q.pp(rule) }
    end
  end
end

#visit_declaration(node) ⇒ Object

Visit a Declaration node.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/syntax_tree/css/pretty_print.rb', line 116

def visit_declaration(node)
  token("declaration") do
    q.breakable
    q.text(node.name)

    if node.important?
      q.breakable
      q.text("!important")
    end

    q.breakable
    q.seplist(node.value) { |token| q.pp(token) }
  end
end

#visit_delim_token(node) ⇒ Object

Visit a DelimToken node.



132
133
134
135
136
137
# File 'lib/syntax_tree/css/pretty_print.rb', line 132

def visit_delim_token(node)
  token("delim-token") do
    q.breakable
    q.pp(node.value)
  end
end

#visit_dimension_token(node) ⇒ Object

Visit a DimensionToken node.



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/syntax_tree/css/pretty_print.rb', line 140

def visit_dimension_token(node)
  token("dimension-token") do
    q.breakable
    q.text(node.type)

    q.breakable
    q.pp(node.value)

    q.breakable
    q.text(node.unit)
  end
end

#visit_eof_token(node) ⇒ Object

Visit an EOFToken node.



154
155
156
# File 'lib/syntax_tree/css/pretty_print.rb', line 154

def visit_eof_token(node)
  token("eof-token")
end

#visit_function(node) ⇒ Object

Visit a Function node.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/syntax_tree/css/pretty_print.rb', line 159

def visit_function(node)
  token("function") do
    q.breakable
    q.text(node.name)

    q.breakable
    q.text("(value")
    q.nest(2) do
      q.breakable
      q.seplist(node.value) { |token| q.pp(token) }
    end

    q.breakable("")
    q.text(")")
  end
end

#visit_function_token(node) ⇒ Object

Visit a FunctionToken node.



177
178
179
180
181
182
# File 'lib/syntax_tree/css/pretty_print.rb', line 177

def visit_function_token(node)
  token("function-token") do
    q.breakable
    q.pp(node.value)
  end
end

#visit_hash_token(node) ⇒ Object

Visit a HashToken node.



193
194
195
196
197
198
199
200
201
# File 'lib/syntax_tree/css/pretty_print.rb', line 193

def visit_hash_token(node)
  token("hash-token") do
    q.breakable
    q.pp(node.value)

    q.breakable
    q.text(node.type)
  end
end

#visit_id_selector(node) ⇒ Object

Visit a Selectors::IdSelector node.



362
363
364
365
366
367
# File 'lib/syntax_tree/css/pretty_print.rb', line 362

def visit_id_selector(node)
  token("id-selector") do
    q.breakable
    q.pp(node.value)
  end
end

#visit_ident_token(node) ⇒ Object

Visit an IdentToken node.



185
186
187
188
189
190
# File 'lib/syntax_tree/css/pretty_print.rb', line 185

def visit_ident_token(node)
  token("ident-token") do
    q.breakable
    q.pp(node.value)
  end
end

#visit_number_token(node) ⇒ Object

Visit a NumberToken node.



204
205
206
207
208
209
210
211
212
# File 'lib/syntax_tree/css/pretty_print.rb', line 204

def visit_number_token(node)
  token("number-token") do
    q.breakable
    q.text(node.type)

    q.breakable
    q.pp(node.value)
  end
end

#visit_open_curly_token(node) ⇒ Object

Visit an OpenCurlyToken node.



215
216
217
# File 'lib/syntax_tree/css/pretty_print.rb', line 215

def visit_open_curly_token(node)
  token("open-curly-token")
end

#visit_open_paren_token(node) ⇒ Object

Visit an OpenParenToken node.



220
221
222
# File 'lib/syntax_tree/css/pretty_print.rb', line 220

def visit_open_paren_token(node)
  token("open-paren-token")
end

#visit_open_square_token(node) ⇒ Object

Visit an OpenSquareToken node.



225
226
227
# File 'lib/syntax_tree/css/pretty_print.rb', line 225

def visit_open_square_token(node)
  token("open-square-token")
end

#visit_percentage_token(node) ⇒ Object

Visit a PercentageToken node.



230
231
232
233
234
235
236
237
238
# File 'lib/syntax_tree/css/pretty_print.rb', line 230

def visit_percentage_token(node)
  token("percentage-token") do
    q.breakable
    q.text(node.type)

    q.breakable
    q.pp(node.value)
  end
end

#visit_pseudo_class_function(node) ⇒ Object

Visit a Selectors::PseudoClassFunction node.



378
379
380
381
382
383
384
385
386
387
388
# File 'lib/syntax_tree/css/pretty_print.rb', line 378

def visit_pseudo_class_function(node)
  token("pseudo-class-function") do
    q.breakable
    q.pp(node.name)

    if node.arguments.any?
      q.breakable
      q.seplist(node.arguments) { |argument| q.pp(argument) }
    end
  end
end

#visit_pseudo_class_selector(node) ⇒ Object

Visit a Selectors::PseudoClassSelector node.



370
371
372
373
374
375
# File 'lib/syntax_tree/css/pretty_print.rb', line 370

def visit_pseudo_class_selector(node)
  token("pseudo-class-selector") do
    q.breakable
    q.pp(node.value)
  end
end

#visit_pseudo_element_selector(node) ⇒ Object

Visit a Selectors::PseudoElementSelector node.



391
392
393
394
395
396
# File 'lib/syntax_tree/css/pretty_print.rb', line 391

def visit_pseudo_element_selector(node)
  token("pseudo-element-selector") do
    q.breakable
    q.pp(node.value)
  end
end

#visit_qualified_rule(node) ⇒ Object

Visit a QualifiedRule node.



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/syntax_tree/css/pretty_print.rb', line 241

def visit_qualified_rule(node)
  token("qualified-rule") do
    q.breakable
    q.text("(prelude")
    q.nest(2) do
      q.breakable("")
      q.seplist(node.prelude) { |token| q.pp(token) }
    end

    q.breakable("")
    q.text(")")

    q.breakable
    q.pp(node.block)
  end
end

#visit_semicolon_token(node) ⇒ Object

Visit a SemicolonToken node.



259
260
261
# File 'lib/syntax_tree/css/pretty_print.rb', line 259

def visit_semicolon_token(node)
  token("semicolon-token")
end

#visit_simple_block(node) ⇒ Object

Visit a SimpleBlock node.



264
265
266
267
268
269
270
271
272
273
274
# File 'lib/syntax_tree/css/pretty_print.rb', line 264

def visit_simple_block(node)
  token("simple-block") do
    q.breakable
    q.pp(node.token)

    if node.value.any?
      q.breakable
      q.seplist(node.value) { |val| q.pp(val) }
    end
  end
end

#visit_string_token(node) ⇒ Object

Visit a StringToken node.



277
278
279
280
281
282
# File 'lib/syntax_tree/css/pretty_print.rb', line 277

def visit_string_token(node)
  token("string-token") do
    q.breakable
    q.pp(node.value)
  end
end

#visit_style_rule(node) ⇒ Object

Visit a StyleRule node.



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
# File 'lib/syntax_tree/css/pretty_print.rb', line 285

def visit_style_rule(node)
  token("style-rule") do
    q.breakable
    q.text("(selectors")
    q.nest(2) do
      q.breakable
      q.seplist(node.selectors) { |token| q.pp(token) }
    end

    q.breakable("")
    q.text(")")

    q.breakable
    q.text("(declarations")

    if node.declarations.any?
      q.nest(2) do
        q.breakable
        q.seplist(node.declarations) { |token| q.pp(token) }
      end

      q.breakable("")
    end

    q.text(")")
  end
end

#visit_stylesheet(node) ⇒ Object

Visit a StyleSheet node.



314
315
316
317
318
319
320
321
# File 'lib/syntax_tree/css/pretty_print.rb', line 314

def visit_stylesheet(node)
  token("stylesheet") do
    if node.rules.any?
      q.breakable
      q.seplist(node.rules) { |rule| q.pp(rule) }
    end
  end
end

#visit_type_selector(node) ⇒ Object

Visit a Selectors::TypeSelector node.



399
400
401
402
403
404
405
406
407
408
409
# File 'lib/syntax_tree/css/pretty_print.rb', line 399

def visit_type_selector(node)
  token("type-selector") do
    if node.prefix
      q.breakable
      q.pp(node.prefix)
    end

    q.breakable
    q.pp(node.value)
  end
end

#visit_urange(node) ⇒ Object

Visit a URange node.



324
325
326
327
328
329
330
331
# File 'lib/syntax_tree/css/pretty_print.rb', line 324

def visit_urange(node)
  token("urange") do
    q.breakable
    q.pp(node.start_value)
    q.text("-")
    q.pp(node.end_value)
  end
end

#visit_url_token(node) ⇒ Object

Visit a URLToken node.



334
335
336
337
338
339
# File 'lib/syntax_tree/css/pretty_print.rb', line 334

def visit_url_token(node)
  token("url-token") do
    q.breakable
    q.pp(node.value)
  end
end

#visit_whitespace_token(node) ⇒ Object

Visit a WhitespaceToken node.



342
343
344
345
346
347
# File 'lib/syntax_tree/css/pretty_print.rb', line 342

def visit_whitespace_token(node)
  token("whitespace-token") do
    q.breakable
    q.pp(node.value)
  end
end

#visit_wqname(node) ⇒ Object

Visit a Selectors::WqName node.



412
413
414
415
416
417
418
419
420
421
422
# File 'lib/syntax_tree/css/pretty_print.rb', line 412

def visit_wqname(node)
  token("wqname") do
    if node.prefix
      q.breakable
      q.pp(node.prefix)
    end

    q.breakable
    q.pp(node.name)
  end
end