Module: TagMunging
- Included in:
- Html2Text
- Defined in:
- lib/tag_munging.rb
Overview
A utility module for recurring tag-specific tasks.
Instance Method Summary collapse
- #all_body_cells(table, &b) ⇒ Object
- #all_body_rows(table, &b) ⇒ Object
- #all_row_cells(tr, &b) ⇒ Object
- #new_tag(doc, tag, options = nil) ⇒ Object
- #remove_empty_rows(table) ⇒ Object
- #remove_empty_sub_tags(parent, tag) ⇒ Object
- #right_align_numbers(tags, symbol) ⇒ Object
- #tag_by_content(doc, tagname, content) ⇒ Object
Instance Method Details
#all_body_cells(table, &b) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/tag_munging.rb', line 49 def all_body_cells(table, &b) trs = table.xpath("./tbody/tr") debug('table contains ' << trs.size.to_s << ' data-rows') if self.respond_to?(:log) trs.each do |tr| all_row_cells(tr) do |td| yield(td, trs.index(td) ) end end end |
#all_body_rows(table, &b) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/tag_munging.rb', line 41 def all_body_rows(table, &b) trs = table.xpath("./tbody/tr") debug('table contains ' << trs.size.to_s << ' data-rows') if self.respond_to?(:log) trs.each do |tr| yield(tr, trs.index(tr) ) end end |
#all_row_cells(tr, &b) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/tag_munging.rb', line 34 def all_row_cells(tr, &b) tds = tr.xpath('./td') tds.each do |td| yield(td, tds.index(td) ) end end |
#new_tag(doc, tag, options = nil) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/tag_munging.rb', line 74 def new_tag(doc, tag, = nil) ntag = Nokogiri::XML::Node.new(tag, doc) if( && .respond_to?(:to_hash)) ntag.content = .delete(:content) if [:content] ntag.parent = .delete(:parent) if [:parent] .each do |k,v| ntag[k.to_s] = v end end return ntag end |
#remove_empty_rows(table) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/tag_munging.rb', line 25 def remove_empty_rows(table) all_body_rows(table) do |tr| cells = tr.xpath("./td").dup empty = true cells.each{|cell| empty = false if !cell.children.empty? } tr.remove if empty end end |
#remove_empty_sub_tags(parent, tag) ⇒ Object
59 60 61 62 |
# File 'lib/tag_munging.rb', line 59 def (parent, tag) = parent.xpath(".//" << tag) .each{|t| t.remove if t.content.chomp.strip.empty?} end |
#right_align_numbers(tags, symbol) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/tag_munging.rb', line 64 def right_align_numbers(, symbol) .each do |tag| if(tag.content.chomp.match(/^-?[^\D]\d*(\.?\d*)?[^\D]?$/) ) tag['class'] = (tag['class'] ? tag['class'] : '') << ' ' << 'right' cont = tag.content tag.content = cont << symbol if cont && symbol end end end |
#tag_by_content(doc, tagname, content) ⇒ Object
86 87 88 |
# File 'lib/tag_munging.rb', line 86 def tag_by_content(doc, tagname, content) return doc.xpath("//" << tagname.to_s).detect{|tag| tag.content.strip == content} end |