Class: Nokogiri::XML::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/Html2Feedbooks/parser.rb

Instance Method Summary collapse

Instance Method Details

#between(a, b, excl = false) ⇒ Object



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
# File 'lib/Html2Feedbooks/parser.rb', line 278

def between(a,b,excl=false)

  #from nokogiri
  offset=(excl ? -1 : 0)
  ary = []
  ele1=at(a) rescue at_xpath(a)
  ele2=at(b) rescue at_xpath(b)
  
  if ele1 and ele2
    # let's quickly take care of siblings
    if ele1.parent == ele2.parent
      
      ary = ele1.parent.children[ele1.node_position..(ele2.node_position+offset)]
    else
      # find common parent
      ele1_p=ele1.ancestors
      ele2_p=ele2.ancestors
      common_parent = ele1_p.zip(ele2_p).select { |p1, p2| p1 == p2 }.flatten.first

      child = nil
      if ele1 == common_parent
        child = ele2
      elsif ele2 == common_parent
        child = ele1
      end

      if child
        ary = common_parent.children[0..(child.node_position+offset)]
      end
    end
  end

  return Nokogiri::XML::NodeSet.new(ele1.document,ary)
end

#deep_between(i, j) ⇒ Object



315
316
317
318
319
320
321
322
323
324
# File 'lib/Html2Feedbooks/parser.rb', line 315

def deep_between(i,j)
  unless j.nil? || self.at_xpath(j).nil?
    tm=self.at_xpath(i)
    prec=tm.deep_preceding
    r=Nokogiri::XML::NodeSet.new(tm.document,[*self.at(j).deep_preceding.find_all{|el| !(prec.include?el || el==tm)}])
  else
    r=self.at(i).deep_following unless self.at(i).nil?
  end
  Nokogiri::XML::NodeSet.new(self.document,[*select_end(r,i)])
end

#deep_followingObject



369
370
371
372
373
# File 'lib/Html2Feedbooks/parser.rb', line 369

def deep_following()
  ret=following
  ret+=parent.deep_following if respond_to?(:parent)  && !parent.is_a?(Nokogiri::XML::Document)
  ret
end

#deep_precedingObject



363
364
365
366
367
368
# File 'lib/Html2Feedbooks/parser.rb', line 363

def deep_preceding()
  ret=Nokogiri::XML::NodeSet.new(self.document,[])
  ret+=parent.deep_preceding if respond_to?(:parent)  && !parent.is_a?(Nokogiri::XML::Document)
  ret+=preceding
  ret
end

#followingObject



359
360
361
# File 'lib/Html2Feedbooks/parser.rb', line 359

def following
  self.parent.children[node_position+1..-1]
end

#in_search?(expr) ⇒ Boolean

Returns:

  • (Boolean)


251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/Html2Feedbooks/parser.rb', line 251

def in_search?(expr)
  if expr !~ /[^a-z0-9]/
    return self.name.downcase()==expr.downcase()  
  end

  se_in=self.root
  se_in=self.parent if self.respond_to?(:parent)
  if expr[0..1]=='/'
    se_in=self.root
  end
  set=se_in.search(expr) rescue se_in.xpath(expr)
  set.each do |el|
    return true if el==self
  end
  #    puts self.name+" "+expr
  return false
end

#node_positionObject



273
274
275
276
# File 'lib/Html2Feedbooks/parser.rb', line 273

def node_position
  return @node_position if @node_position
  @node_position=parent.children.index(self)
end

#precedingObject



355
356
357
# File 'lib/Html2Feedbooks/parser.rb', line 355

def preceding
  self.parent.children[0...node_position]
end

#rootObject



269
270
271
# File 'lib/Html2Feedbooks/parser.rb', line 269

def root
  self.document.root
end

#select_end(tab, expr) ⇒ Object



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
# File 'lib/Html2Feedbooks/parser.rb', line 326

def select_end(tab,expr)

  s=[]
  f=false
  idx=-1
  i=0
  tab.each do |e|
    nxp=expr.gsub(e.path,'.')
    set=e.search(nxp) rescue e.xpath(nxp)
    if set.size > 0
      idx=i
      #if e.search(i).size > 0
      if e.children.find{|ee| ee.path==expr }
        e.children.each do |ee|
          s << ee if f
          f=true if ee.path==expr
        end
      else
        s=select_end(e.children,expr)
      end
      break
    else
      i+=1
    end
    break if idx>0
  end
  return s+tab[(idx+1)..-1]
end