Created
November 20, 2015 19:52
-
-
Save singpolyma/38d945d0e7a7dccb160d to your computer and use it in GitHub Desktop.
Revisions
-
singpolyma created this gist
Nov 20, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ require 'nokogiri' def content_text(nodes) nodes.map do |el| if el.text? || el.attributes['class'].to_s.match(/\b(?:h\-card|vcard|h\-x\-username)\b/) || el.attributes['rel'].to_s.match(/\btag\b/) el.text elsif el.name == 'a' href = el.attributes['href'].to_s if el.text.strip == '' '' elsif el.text.match(/[\/\.]/) && href.gsub(/[^\w\-_\/]/, '').include?(el.text.gsub(/[^\w\-_\/]/, '')) href else el.text + " #{href}" end elsif el.name == 'img' el.attributes['src'].to_s else content_text(el.children) end end.join end content_text(Nokogiri::HTML.fragment(item[:content].to_s).children)