Last active
August 29, 2015 14:17
-
-
Save bchase/7e6d514ccf07da3ce9c4 to your computer and use it in GitHub Desktop.
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
class MarkdownText < String | |
def lines | |
split("\n").map { |l| MarkdownLine.new l } | |
end | |
def urls_to_links! | |
replace lines.map { |line| | |
line.solo_url? ? line.to_url_link : line | |
}.join("\n") | |
end | |
require 'uri' | |
require 'active_support/core_ext/string' | |
class MarkdownLine < String | |
def solo_url? | |
return false if blank? | |
!!URI.parse(self) | |
rescue | |
false | |
end | |
def to_url_link | |
"* [#{title_for_url(self)}](#{self})" | |
end | |
def title_for_url(url) | |
html = %x[ curl -L #{url} ] | |
title = html[ %r{<title([^>]+)?>(.+)</title>}, 2 ] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment