Class: MaRuKu::In::Markdown::SpanLevelParser::HTMLHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/maruku/input/html_helper.rb

Overview

This class helps me read and sanitize HTML blocks

Constant Summary collapse

Tag =
%r{^<(/)?(\w+)\s*([^>]*?)>}m
PartialTag =
%r{^<.*}m
CData =
%r{^\s*<!\[CDATA\[}m
CDataEnd =
%r{\]\]>}m
EverythingElse =
%r{^[^<]+}m
CommentStart =
%r{^<!--}x
CommentEnd =
%r{-->}
TO_SANITIZE =
['img','hr','br']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHTMLHelper

Returns a new instance of HTMLHelper.



21
22
23
24
25
26
27
# File 'lib/maruku/input/html_helper.rb', line 21

def initialize
  @rest = ""
  @tag_stack = []
  @m = nil
  @already = ""
  self.state = :inside_element
end

Instance Attribute Details

#first_tagObject (readonly)

Returns the value of attribute first_tag.



15
16
17
# File 'lib/maruku/input/html_helper.rb', line 15

def first_tag
  @first_tag
end

#restObject (readonly)

Returns the value of attribute rest.



15
16
17
# File 'lib/maruku/input/html_helper.rb', line 15

def rest
  @rest
end

#stateObject

:inside_element, :inside_tag, :inside_comment, :inside_cdata, :inside_script_style



29
30
31
# File 'lib/maruku/input/html_helper.rb', line 29

def state
  @state
end

Instance Method Details

#eat_this(line) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/maruku/input/html_helper.rb', line 31

def eat_this(line)
  @rest = line + @rest
  things_read = 0
  until @rest.empty?
    case self.state
    when :inside_comment
      if @m = CommentEnd.match(@rest)
        my_debug "#{@state}: Comment End: #{@m.to_s.inspect}"
        @already << @m.pre_match << @m.to_s
        @rest = @m.post_match
        self.state = :inside_element
      else
        @already << @rest
        @rest = ""
        self.state = :inside_comment
      end
    when :inside_element
      if @m = CommentStart.match(@rest)
        my_debug "#{@state}: Comment: #{@m.to_s.inspect}"
        things_read += 1
        @already << @m.pre_match << @m.to_s
        @rest = @m.post_match
        self.state = :inside_comment
      elsif @m = Tag.match(@rest)
        my_debug "#{@state}: Tag: #{@m.to_s.inspect}"
        things_read += 1
        self.state = :inside_element
        handle_tag
      elsif @m = CData.match(@rest)
        my_debug "#{@state}: CDATA: #{@m.to_s.inspect}"
        @already << @m.pre_match << @m.to_s
        @rest = @m.post_match
        self.state = :inside_cdata
      elsif @m = PartialTag.match(@rest)
        my_debug "#{@state}: PartialTag: #{@m.to_s.inspect}"
        @already << @m.pre_match
        @rest = @m.post_match
        @partial_tag = @m.to_s
        self.state = :inside_tag
      elsif @m = EverythingElse.match(@rest)
        my_debug "#{@state}: Everything: #{@m.to_s.inspect}"
        @already << @m.pre_match << @m.to_s
        @rest = @m.post_match
        self.state = :inside_element
      else
        error "Malformed HTML: not complete: #{@rest.inspect}"
      end
    when :inside_tag
      if @m = /^[^>]*>/.match(@rest)
        my_debug "#{@state}: matched #{@m.to_s.inspect}"
        @partial_tag << @m.to_s
        my_debug "#{@state}: matched TOTAL: #{@partial_tag.to_s.inspect}"
        @rest = @partial_tag + @m.post_match
        @partial_tag = nil
        self.state = :inside_element
      else
        @partial_tag << @rest
        @rest = ""
        self.state = :inside_tag
      end
    when :inside_cdata
      if @m = CDataEnd.match(@rest)
        my_debug "#{@state}: matched #{@m.to_s.inspect}"
        @already << @m.pre_match << @m.to_s
        @rest = @m.post_match
        self.state = %(script style).include?(@tag_stack.last) ? :inside_script_style : :inside_element
      else
        @already << @rest
        @rest = ""
        self.state = :inside_cdata
      end
    when :inside_script_style
      if @m = CData.match(@rest)
        if @already.rstrip.end_with?('<![CDATA[')
          @already << @m.pre_match
          @rest = @m.post_match
        else
          my_debug "#{@state}: CDATA: #{@m.to_s.inspect}"
          @already << @m.pre_match << @m.to_s
          @rest = @m.post_match
          self.state = :inside_cdata
        end
      elsif @m = Tag.match(@rest)
        is_closing = !!@m[1]
        tag = @m[2]
        if is_closing && tag == @tag_stack.last
          my_debug "#{@state}: matched #{@m.to_s.inspect}"
          @already << @m.pre_match
          @rest = @m.post_match
          # This is necessary to properly parse
          # script tags
          @already << "]]>" unless @already.rstrip.end_with?("]]>")
          self.state = :inside_element
          handle_tag false # don't double-add pre_match
        else
          @already << @rest
          @rest = ""
        end
      elsif @m = EverythingElse.match(@rest)
        my_debug "#{@state}: Everything: #{@m.to_s.inspect}"
        @already << @m.pre_match << @m.to_s
        @rest = @m.post_match
      else
        @already << @rest
        @rest = ""
      end
    else
      raise "Bug bug: state = #{self.state.inspect}"
    end # not inside comment

    break if is_finished? && things_read > 0
  end
end

#error(s) ⇒ Object

Raises:



198
199
200
# File 'lib/maruku/input/html_helper.rb', line 198

def error(s)
  raise "Error: #{s} \n" + inspect, caller
end

#handle_tag(add_pre_match = true) ⇒ Object



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
# File 'lib/maruku/input/html_helper.rb', line 145

def handle_tag(add_pre_match = true)
  @already << @m.pre_match if add_pre_match
  @rest = @m.post_match

  is_closing = !!@m[1]
  tag = @m[2]
  @first_tag ||= tag
  attributes = @m[3].to_s

  is_single = false
  if attributes[-1, 1] == '/'
    attributes = attributes[0, attributes.size - 1]
    is_single = true
  end

  my_debug "Attributes: #{attributes.inspect}"
  my_debug "READ TAG #{@m.to_s.inspect} tag = #{tag} closing? #{is_closing} single = #{is_single}"

  if TO_SANITIZE.include? tag
    attributes.strip!
    #   puts "Attributes: #{attributes.inspect}"
    if attributes.size > 0
      @already <<  '<%s %s />' % [tag, attributes]
    else
      @already <<  '<%s />' % [tag]
    end
  elsif is_closing
    if @tag_stack.empty?
      error "Malformed: closing tag #{tag.inspect} in empty list"
    end
    if @tag_stack.last != tag
      error "Malformed: tag <#{tag}> closes <#{@tag_stack.last}>"
    end

    @already << @m.to_s
    @tag_stack.pop
  else
    @already << @m.to_s

    if not is_single
      @tag_stack.push(tag)
      my_debug "Pushing #{tag.inspect} when read #{@m.to_s.inspect}"
    end

    if %w(script style).include?(@tag_stack.last)
      # This is necessary to properly parse
      # script tags
      @already << "<![CDATA["
      self.state = :inside_script_style
    end
  end
end

#inspectObject



202
203
204
205
206
207
208
209
210
# File 'lib/maruku/input/html_helper.rb', line 202

def inspect
  "HTML READER\n state=#{self.state} " +
    "match=#{@m.to_s.inspect}\n" +
    "Tag stack = #{@tag_stack.inspect} \n" +
    "Before:\n" +
    @already.gsub(/^/, '|') + "\n" +
    "After:\n" +
    @rest.gsub(/^/, '|') + "\n"
end

#is_finished?Boolean

Returns:

  • (Boolean)


216
217
218
# File 'lib/maruku/input/html_helper.rb', line 216

def is_finished?
  (self.state == :inside_element) and @tag_stack.empty?
end

#my_debug(s) ⇒ Object



17
18
19
# File 'lib/maruku/input/html_helper.rb', line 17

def my_debug(s)
  #    puts "---" * 10 + "\n" + inspect + "\t>>>\t" + s
end

#stuff_you_readObject



212
213
214
# File 'lib/maruku/input/html_helper.rb', line 212

def stuff_you_read
  @already
end