Class: ContentUrls
- Inherits:
-
Object
- Object
- ContentUrls
- Defined in:
- lib/content_urls.rb,
lib/content_urls/version.rb,
lib/content_urls/parsers/css_parser.rb,
lib/content_urls/parsers/html_parser.rb,
lib/content_urls/parsers/java_script_parser.rb more...
Overview
ContentUrls
parses various file types (HTML, CSS, JavaScript, …) for URLs and provides methods for iterating through URLs and changing URLs.
Defined Under Namespace
Modules: Version Classes: CssParser, HtmlParser, JavaScriptParser
Class Method Summary collapse
-
.rewrite_each_url(content, type, &block) ⇒ Object
Rewrites each URL in the content by calling the supplied block with each URL.
-
.to_absolute(url, base_url) ⇒ Object
Convert a relative URL to an absolute URL using base_url (for example, the content’s original location or an HTML document’s href attribute of the base tag).
-
.urls(content, type) ⇒ Array
Returns the URLs found in the content.
Class Method Details
permalink .rewrite_each_url(content, type, &block) ⇒ Object
Rewrites each URL in the content by calling the supplied block with each URL.
49 50 51 52 53 54 55 56 57 |
# File 'lib/content_urls.rb', line 49 def self.rewrite_each_url(content, type, &block) if (parser = get_parser(type)) parser.rewrite_each_url(content) do |url| replacement = yield url (replacement.nil? ? url : replacement) end end content end |
permalink .to_absolute(url, base_url) ⇒ Object
Convert a relative URL to an absolute URL using base_url (for example, the content’s original location or an HTML document’s href attribute of the base tag).
65 66 67 68 69 70 71 72 |
# File 'lib/content_urls.rb', line 65 def self.to_absolute(url, base_url) return nil if url.nil? url = URI.encode(URI.decode(url.to_s.gsub(/#[a-zA-Z0-9_-]*$/,''))) # remove anchor absolute = URI(base_url).merge(url) absolute.path = '/' if absolute.path.empty? absolute.to_s end |
permalink .urls(content, type) ⇒ Array
Returns the URLs found in the content.
29 30 31 32 33 34 35 |
# File 'lib/content_urls.rb', line 29 def self.urls(content, type) urls = [] if (parser = get_parser(type)) parser.urls(content).each { |url| urls << url } end urls end |