Created
November 20, 2015 19:52
-
-
Save singpolyma/38d945d0e7a7dccb160d to your computer and use it in GitHub Desktop.
Example algorithm to textify HTML of tweetish posts
This file contains 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 characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment