Class: XOXO::Parser

Inherits:
Object show all
Defined in:
lib/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.



129
130
131
132
133
134
135
136
# File 'lib/xoxo.rb', line 129

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.



127
128
129
# File 'lib/xoxo.rb', line 127

def structs
  @structs
end

Instance Method Details

#parseObject



138
139
140
141
142
143
144
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
197
198
199
200
201
202
203
204
205
206
# File 'lib/xoxo.rb', line 138

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