Class: HtmlBeautifier::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/htmlbeautifier/builder.rb

Constant Summary collapse

RUBY_INDENT =
%r{ ^ ( if | unless | while | begin | elsif | else )\b
| \b ( do | \{ ) ( \s* \| [^\|]+ \| )? $
}x
RUBY_OUTDENT =
%r{ ^ ( end | elsif | else |\} ) \b
}x
ELEMENT_CONTENT =
%r{ (?:[^<>]|<%.*?%>)* }mx

Instance Method Summary collapse

Constructor Details

#initialize(output, tab_stops) ⇒ Builder

Returns a new instance of Builder.



15
16
17
18
19
20
21
# File 'lib/htmlbeautifier/builder.rb', line 15

def initialize(output, tab_stops)
  @level = 0
  @new_line = true
  @tab = ' ' * tab_stops
  @output = output
  @ie_cc_levels = []
end

Instance Method Details

#close_element(e) ⇒ Object



77
78
79
80
# File 'lib/htmlbeautifier/builder.rb', line 77

def close_element(e)
  outdent
  emit e
end

#close_ie_cc(e) ⇒ Object



87
88
89
90
91
# File 'lib/htmlbeautifier/builder.rb', line 87

def close_ie_cc(e)
  raise "Unclosed conditional comment" if @ie_cc_levels.empty?
  @level = @ie_cc_levels.pop
  emit e
end

#embed(opening, code, closing) ⇒ Object



45
46
47
48
49
50
# File 'lib/htmlbeautifier/builder.rb', line 45

def embed(opening, code, closing)
  lines = code.split(/\n/).map{ |l| l.strip }
  outdent if lines.first =~ RUBY_OUTDENT
  emit opening + code + closing
  indent if lines.last =~ RUBY_INDENT
end

#emit(s) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/htmlbeautifier/builder.rb', line 32

def emit(s)
  if (@new_line)
    @output << (@tab * @level)
  end
  @output << s
  @new_line = false
end

#foreign_block(opening, code, closing) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/htmlbeautifier/builder.rb', line 52

def foreign_block(opening, code, closing)
  emit opening
  unless code.empty?
    indent

    lines = code.split(/\n/)
    lines.shift while lines.first.strip.empty?
    lines.pop while lines.last.strip.empty?
    indentation = lines.first[/^ +/]

    whitespace
    lines.each do |line|
      emit line.rstrip.sub(/^#{indentation}/, '')
      whitespace
    end

    outdent
  end
  emit closing
end

#indentObject



23
24
25
# File 'lib/htmlbeautifier/builder.rb', line 23

def indent
  @level += 1
end

#open_element(e) ⇒ Object



82
83
84
85
# File 'lib/htmlbeautifier/builder.rb', line 82

def open_element(e)
  emit e
  indent
end

#open_ie_cc(e) ⇒ Object



93
94
95
96
97
# File 'lib/htmlbeautifier/builder.rb', line 93

def open_ie_cc(e)
  emit e
  @ie_cc_levels.push @level
  indent
end

#outdentObject



27
28
29
30
# File 'lib/htmlbeautifier/builder.rb', line 27

def outdent
  @level -= 1
  raise "Outdented too far" if @level < 0
end

#standalone_element(e) ⇒ Object



73
74
75
# File 'lib/htmlbeautifier/builder.rb', line 73

def standalone_element(e)
  emit e
end

#text(t) ⇒ Object



99
100
101
102
# File 'lib/htmlbeautifier/builder.rb', line 99

def text(t)
  emit(t.strip)
  whitespace if t =~ /\s$/
end

#whitespace(*x) ⇒ Object



40
41
42
43
# File 'lib/htmlbeautifier/builder.rb', line 40

def whitespace(*x)
  emit "\n"
  @new_line = true
end