Class: Html2Text
- Inherits:
-
Object
- Object
- Html2Text
- Defined in:
- lib/html2text.rb,
lib/html2text/version.rb
Constant Summary collapse
- VERSION =
"0.2.0"
Instance Attribute Summary collapse
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
Class Method Summary collapse
Instance Method Summary collapse
- #convert ⇒ Object
- #image_text(node) ⇒ Object
-
#initialize(doc) ⇒ Html2Text
constructor
A new instance of Html2Text.
- #iterate_over(node) ⇒ Object
- #next_node_name(node) ⇒ Object
- #prefix_whitespace(node) ⇒ Object
- #remove_leading_and_trailing_whitespace(text) ⇒ Object
- #remove_unnecessary_empty_lines(text) ⇒ Object
- #suffix_whitespace(node) ⇒ Object
- #trimmed_whitespace(text) ⇒ Object
-
#wrap_link(node, output) ⇒ Object
links are returned in [text](link) format.
Constructor Details
#initialize(doc) ⇒ Html2Text
Returns a new instance of Html2Text.
6 7 8 |
# File 'lib/html2text.rb', line 6 def initialize(doc) @doc = doc end |
Instance Attribute Details
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
4 5 6 |
# File 'lib/html2text.rb', line 4 def doc @doc end |
Class Method Details
.convert(html) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/html2text.rb', line 10 def self.convert(html) html = fix_newlines(replace_entities(html)) doc = Nokogiri::HTML(html) Html2Text.new(doc).convert end |
.fix_newlines(text) ⇒ Object
17 18 19 |
# File 'lib/html2text.rb', line 17 def self.fix_newlines(text) text.gsub("\r\n", "\n").gsub("\r", "\n") end |
.replace_entities(text) ⇒ Object
21 22 23 |
# File 'lib/html2text.rb', line 21 def self.replace_entities(text) text.gsub(" ", " ").gsub("\u00a0", " ") end |
Instance Method Details
#convert ⇒ Object
25 26 27 28 29 30 |
# File 'lib/html2text.rb', line 25 def convert output = iterate_over(doc) output = remove_leading_and_trailing_whitespace(output) output = remove_unnecessary_empty_lines(output) output.strip end |
#image_text(node) ⇒ Object
168 169 170 171 172 173 174 175 176 |
# File 'lib/html2text.rb', line 168 def image_text(node) if node.attribute("title") "[" + node.attribute("title").to_s + "]" elsif node.attribute("alt") "[" + node.attribute("alt").to_s + "]" else "" end end |
#iterate_over(node) ⇒ Object
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 |
# File 'lib/html2text.rb', line 57 def iterate_over(node) return trimmed_whitespace(node.text) if node.text? if ["style", "head", "title", "meta", "script"].include?(node.name.downcase) return "" end output = [] output << prefix_whitespace(node) output += node.children.map do |child| iterate_over(child) end output << suffix_whitespace(node) output = output.compact.join("") || "" if node.name.downcase == "a" output = wrap_link(node, output) end if node.name.downcase == "img" output = image_text(node) end output end |
#next_node_name(node) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/html2text.rb', line 45 def next_node_name(node) next_node = node.next_sibling while next_node != nil break if next_node.element? next_node = next_node.next_sibling end if next_node && next_node.element? next_node.name.downcase end end |
#prefix_whitespace(node) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/html2text.rb', line 84 def prefix_whitespace(node) case node.name.downcase when "hr" "---------------------------------------------------------------\n" when "h1", "h2", "h3", "h4", "h5", "h6", "ol", "ul" "\n" when "tr", "p", "div" "\n" when "td", "th" "\t" when "li" "- " end end |
#remove_leading_and_trailing_whitespace(text) ⇒ Object
32 33 34 |
# File 'lib/html2text.rb', line 32 def remove_leading_and_trailing_whitespace(text) text.gsub(/[ \t]*\n[ \t]*/im, "\n").gsub(/ *\t */im, "\t") end |
#remove_unnecessary_empty_lines(text) ⇒ Object
36 37 38 |
# File 'lib/html2text.rb', line 36 def remove_unnecessary_empty_lines(text) text.gsub(/\n\n\n*/im, "\n\n") end |
#suffix_whitespace(node) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/html2text.rb', line 103 def suffix_whitespace(node) case node.name.downcase when "h1", "h2", "h3", "h4", "h5", "h6" # add another line "\n" when "p", "br" "\n" if next_node_name(node) != "div" when "li" "\n" when "div" # add one line only if the next child isn't a div "\n" if next_node_name(node) != "div" && next_node_name(node) != nil end end |
#trimmed_whitespace(text) ⇒ Object
40 41 42 43 |
# File 'lib/html2text.rb', line 40 def trimmed_whitespace(text) # Replace whitespace characters with a space (equivalent to \s) text.gsub(/[\t\n\f\r ]+/im, " ") end |
#wrap_link(node, output) ⇒ Object
links are returned in [text](link) format
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 |
# File 'lib/html2text.rb', line 122 def wrap_link(node, output) href = node.attribute("href") name = node.attribute("name") output = output.strip # remove double [[ ]]s from linking images if output[0] == "[" && output[-1] == "]" output = output[1, output.length - 2] # for linking images, the title of the <a> overrides the title of the <img> if node.attribute("title") output = node.attribute("title").to_s end end # if there is no link text, but a title attr if output.empty? && node.attribute("title") output = node.attribute("title").to_s end if href.nil? if !name.nil? output = "[#{output}]" end else href = href.to_s if href != output && href != "mailto:#{output}" && href != "http://#{output}" && href != "https://#{output}" if output.empty? output = href else output = "[#{output}](#{href})" end end end case next_node_name(node) when "h1", "h2", "h3", "h4", "h5", "h6" output += "\n" end output end |