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
22
# File 'lib/htmlbeautifier/builder.rb', line 15

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

Instance Method Details

#close_block_element(e) ⇒ Object



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

def close_block_element(e)
  outdent
  emit e
  new_line
end

#close_element(e) ⇒ Object



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

def close_element(e)
  outdent
  emit e
end

#close_ie_cc(e) ⇒ Object



109
110
111
112
113
# File 'lib/htmlbeautifier/builder.rb', line 109

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



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

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



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

def emit(s)
  if @new_line && !@empty
    @output << ("\n" + @tab * @level)
  end
  @output << s
  @new_line = false
  @empty = false
end

#foreign_block(opening, code, closing) ⇒ Object



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

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[/^ +/]

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

    outdent
  end
  emit closing
end

#indentObject



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

def indent
  @level += 1
end

#new_line(*_args) ⇒ Object



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

def new_line(*_args)
  @new_line = true
end

#open_block_element(e) ⇒ Object



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

def open_block_element(e)
  new_line
  emit e
  indent
end

#open_element(e) ⇒ Object



104
105
106
107
# File 'lib/htmlbeautifier/builder.rb', line 104

def open_element(e)
  emit e
  indent
end

#open_ie_cc(e) ⇒ Object



115
116
117
118
119
# File 'lib/htmlbeautifier/builder.rb', line 115

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

#outdentObject



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

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

#preformatted_block(opening, content, closing) ⇒ Object



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

def preformatted_block(opening, content, closing)
  new_line
  emit opening
  emit content
  emit closing
  new_line
end

#standalone_element(e) ⇒ Object



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

def standalone_element(e)
  emit e
  new_line if e =~ /^<br[^\w]/
end

#text(t) ⇒ Object



121
122
123
124
# File 'lib/htmlbeautifier/builder.rb', line 121

def text(t)
  emit t.chomp
  new_line if t.end_with? $/
end