Class: XOXO::Parser

Inherits:
Object show all
Defined in:
lib/facets/xoxo.rb

Overview

:nodoc:

Constant Summary collapse

CONTAINER_TAGS =
%w{dl ol ul}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xoxo) ⇒ Parser

Returns a new instance of Parser.



143
144
145
146
147
148
149
150
# File 'lib/facets/xoxo.rb', line 143

def initialize(xoxo)
  @parser = REXML::Parsers::PullParser.new(xoxo)

  @textstack = ['']
  @xostack = []
  @structs = []
  @tags = []
end

Instance Attribute Details

#structsObject (readonly)

Returns the value of attribute structs.



141
142
143
# File 'lib/facets/xoxo.rb', line 141

def structs
  @structs
end

Instance Method Details

#parseObject



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
# File 'lib/facets/xoxo.rb', line 152

def parse
  while @parser.has_next?
    res = @parser.pull

    if res.start_element?
      @tags << res[0]

      case res[0]
      when "a"
        attrs = normalize_attrs res[1]
        attrs['url'] = attrs['href']
        attrs.delete 'href'
        push attrs
        @textstack << ''

      when "dl"
        push({})

      when "ol", "ul"
        push []

      when "li", "dt", "dd"
        @textstack << ''

      end
    elsif res.end_element?
      @tags.pop

      case res[0]
      when "a"
        val = @textstack.pop
        unless val.empty?
          val = ''  if @xostack.last['title'] == val
          val = ''  if @xostack.last['url'] == val
          @xostack.last['text'] = val  unless val.empty?
        end
        @xostack.pop

      when "dl", "ol", "ul"
        @xostack.pop

      when "li"
        val = @textstack.pop
        while @structs.last != @xostack.last
          val = @structs.pop
          @xostack.last << val
        end
        @xostack.last << val  if val.kind_of? String

      when "dt"
        # skip

      when "dd"
        val = @textstack.pop
        key = @textstack.pop

        val = @structs.pop  if @structs.last != @xostack.last
        @xostack.last[key] = val

      end
    elsif res.text?
      unless @tags.empty? || CONTAINER_TAGS.include?(@tags.last)
        @textstack.last << res[0]
      end
    end
  end

  self
end