fedora-migrator /vendor/gems/facets-2.4.5/lib/more/facets/tagiter.rb

Language Ruby Lines 367
MD5 Hash ebe0be3a25821968f0b77b11c9bd7306 Estimated Cost $5,300 (why?)
Repository https://bitbucket.org/mediashelf/fedora-migrator View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# = tagiterator.rb
#
# == Copyright (c) 2000 ?nyasu <nyasu@osk.3web.ne.jp>
#
#   Ruby License
#
#   This module is free software. You may use, modify, and/or redistribute this
#   software under the same terms as Ruby.
#
#   This program is distributed in the hope that it will be useful, but WITHOUT
#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#   FOR A PARTICULAR PURPOSE.
#
# == Author(s)
#
# * ?nyasu

# Author::    ?nyasu <nyasu@osk.3web.ne.jp>
# Copyright:: Copyright (c) 2000 ?nyasu
# License::   Ruby License

# = TagIterator (aka Tagiter)
#
# Simple but very useful HTML/XHTML cascading parser.
#
# Quickly iterate through tagged markup documents like HTML and XML.
# TagIterator is great for quick and dirty web scrapping.
#
# == Usage
#
#   # sample html
#   stext = <<-EOF
#   <body> This is a test...
#     <sub> S1 </sub> <sub> S2 </sub>
#     <DL>
#       <DT> A1
#       <DT> A2
#       <DT> A3
#     </DL>
#     <DL>
#       <DT> B1
#       <DT> B2
#       <DT> B3
#     </DL>
#     <NEST>
#       <P ALIGN="R">TOP</P>
#       <NEST>
#         <P>SECOND</P>
#         <OL>
#           <LI>C1
#           <LI>C2
#           <LI>C3
#           <LI>C4
#         </OL>
#       </NEST>
#       <OL>
#         <LI>D1
#         <LI>D2
#         <LI>D3
#         <LI>D4
#       </OL>
#     </NEST>
#   </body>
#   EOF
#
#   a = TagIterator.new(stext)
#   a.first("body") do |y|
#     y.nth("dl",2) do |dl|
#       dl.enumtag("dt") do |t|
#         puts t.text.strip
#       end
#     end
#     y.first("nest") do |n|
#       n.first("p") do |c|
#         print c.text, ' '
#         puts c.attributes.collect{ |k,v| "#{k}=#{v}" }
#       end.next("nest") do |m|
#         m.first("p") do |c|
#           puts c.text
#         end.next("ol") do |o|
#           o.enumtag("li") do |i| puts i.text.strip end
#         end
#       end.next("ol") do |o|
#         o.enumtag("li") do |i| puts i.text.strip end
#       end
#     end
#   end
#   a.each_block("sub") do |y|
#     puts y.text.strip
#   end
#
# _produces_
#
#   B1
#   B2
#   B3
#   TOP align=R
#   SECOND
#   C1
#   C2
#   C3
#   C4
#   D1
#   D2
#   D3
#   D4
#   S1
#   S2
#

class TagIterator

  attr :text
  attr :option, true
  attr :tag
  attr :attributes

  private

  def initialize(text,tag=nil,attributes={})
    raise RuntimeError,"Only String accepted" unless text.is_a?(String)
    @text=text
    @option="pi"
    @tag=tag
    @attributes=attributes
    def @attributes.[](aname)
      super aname.downcase
    end
  end

  def find_element(element,st=0)
    rex=Regexp.new('<(\s|\n)*'+element+'(\s|\n|>)',@option)
    @text.index(rex,st)
  end

  def parse_attribute(attstr)
    k={}; r={}; 
    attstr.scan(/(\w+)=(\S+)/) do |pt| k[ pt[0] ] = pt[1] end
    attstr.scan(/(\w+)="([^"]*)"/) do |pt| k[ pt[0] ] = pt[1] end
    k.each do |key,val| r[key.downcase]=val end
    r
  end

  def find_opentag(tag,st=0)
    s=find_element(tag,st)
    return nil unless s

    r=@text.index('>',s)
    return r+1,@text[s+1..r-1]
  end

  def find_closetag(tag,st,opentag=nil)
    if opentag then
      p=find_element(tag,st)
      q,d = find_opentag(opentag,st)
    else
      p=find_element('/\s*'+tag,st)
      q,d = find_opentag(tag,st)
    end
    p-=1 if p 

    if p and q then if p > q then    # tag nested
      p=find_closetag(tag,find_closetag(tag,q,opentag)+2,opentag)
    end end

    return p
  end

  def find_closeenumtag(tag,st=0)
    rex=Regexp.new('<\s*'+tag,@option)
    s=@text.index(rex,st)
    s-=1 if s
    s
  end
  alias_method :find_openenumtag, :find_opentag

  public

  def nth(tag,n,closetag=nil)
    raise RuntimeError,"nth: number not specified" unless n
    t=0
    e=s=0   # for their scope
    d=nil

    1.upto(n) do |i|
      s,d = find_opentag(tag,t)
      raise RuntimeError,"tag(#{tag}) not found at(#{i})" unless s

      if closetag then
        e=find_closetag(closetag,s,tag)
      else
        e=find_closetag(tag,s)
      end
      e=-1 unless e
      t=@text.index('>',e+1)
      t=@text.length unless t
    end
    yield self.class.new(text[s..e],tag,parse_attribute(d))
    self.class.new(text[t+1..-1])
  end

  def first(tag,*arg) nth(tag,1,*arg) do |f| yield f end end
  alias_method :next, :first

  def each_block(tag,closetag=nil)
    t=0
    s,d =find_opentag(tag)
    raise RuntimeError,"tag(#{tag}) not found" unless s

    while s do
      if closetag then
        e=find_closetag(closetag,s,tag)
      else
        e=find_closetag(tag,s)
      end
      e=-1 unless e
      yield self.class.new(@text[s..e],tag,parse_attribute(d))
      if e>=0 then 
        t=@text.index('>',e+1)
        t=@text.length unless t
        s,d = find_opentag(tag,t)
      else
        s=false
      end
    end
    self.class.new(text[t+1..-1])
  end

  def collect(*arg)
    a=[]
    each_block(*arg) do |tt| a.push tt end
    a
  end

  def enumtag(tag)
    s,d = find_openenumtag(tag)
    while s do
      e=find_closeenumtag(tag,s+1)
      e=-1 unless e
      yield self.class.new(@text[s..e],tag,parse_attribute(d))
      s,d = find_openenumtag(tag,s)
    end
  end

  def enumcollect(tag)
    a=[]
    enumtag(tag) do |t| a.push t end
    a
  end

  def for_this
    yield self
  end

  def get_nth(*arg) r=nil; nth(*arg) do |bl| r=bl end; r; end

  def get_first(*arg) r=nil; first(*arg) do |bl| r=bl end; r; end

  def tagexist?(tag,st=0)
    s=find_element(tag,st)
    if s then true else false end
  end

  def tagnext
    s=@text.index("<")
    return nil unless s
    e=@text.index(">",s)
    return nil unless s
    @text[s..e].scan(/[^<>\s]+/)[0]
  end

  def nth_tailer(tag,n)
    nth(tag,n) do end
  end

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#

=begin testing

require 'test/unit'

class TC_TagIterator < Test::Unit::TestCase

  STEXT = <<-EOS
  <body> This is a test...
    <sub> S1 </sub> <sub> S2 </sub>
    <DL>
      <DT> A1
      <DT> A2
      <DT> A3
    </DL>
    <DL>
      <DT> B1
      <DT> B2
      <DT> B3
    </DL>
    <NEST>
      <P ALIGN="R">TOP</P>
      <NEST>
        <P>SECOND</P>
        <OL>
          <LI>C1
          <LI>C2
          <LI>C3
          <LI>C4
        </OL>
      </NEST>
      <OL>
        <LI>D1
        <LI>D2
        <LI>D3
        <LI>D4
      </OL>
    </NEST>
  </body>
  EOS

  def test_all
    assert_nothing_raised{ @a = TagIterator.new( STEXT ) }
    @f = []
    assert_nothing_raised {
      @a.first("body") do |y|
        y.nth("dl",2) do |dl|
          dl.enumtag("dt") do |t|
            @f << t.text.strip
          end
        end
        y.first("nest") do |n|
          n.first("p") do |c|
            @f << c.text
            @f.concat c.attributes.collect{ |k,v| "#{k}=#{v}" }
          end.next("nest") do |m|
            m.first("p") do |c|
              @f << c.text
            end.next("ol") do |o|
              o.enumtag("li") do |i| @f << i.text.strip end
            end
          end.next("ol") do |o|
            o.enumtag("li") do |i| @f << i.text.strip end
          end
        end
      end
      @a.each_block("sub") do |y|
        @f << y.text.strip
      end
    }
    o = [ "B1", "B2", "B3",
          "TOP", "align=R", "SECOND",
          "C1", "C2", "C3", "C4",
          "D1", "D2", "D3", "D4",
          "S1", "S2" ]
    assert_equal( o, @f )
  end

end

=end
Back to Top