Module: Octopress::Filters
- Defined in:
- lib/octopress-filters.rb,
lib/octopress-filters/hooks.rb,
lib/octopress-filters/version.rb
Defined Under Namespace
Classes: PostHooks, SiteGrabber
Constant Summary collapse
- VERSION =
"1.4.0"
Instance Attribute Summary collapse
-
#site ⇒ Object
If filters are called before hooks, use Octopress.site if available.
Instance Method Summary collapse
- #baseurl ⇒ Object
-
#canonical_url(input) ⇒ Object
Convert url input into a standard canonical url by expanding urls and removing url fragments ending with ‘index.`.
-
#cdata_escape(input) ⇒ Object
Escapes HTML content for XML.
-
#classify(input) ⇒ Object
(also: #sluggify)
Formats a string for use as a css classname, removing illegal characters.
-
#compact_newlines(input) ⇒ Object
Remove empty lines.
-
#expand_url(input, url = nil) ⇒ Object
Prepends input with a url fragment.
-
#expand_urls(input, url = nil) ⇒ Object
Prepend all absolute urls with a url fragment.
-
#full_url(input) ⇒ Object
Prepend a url with the full site url.
-
#full_urls(input) ⇒ Object
Prepend all urls with the full site url.
-
#join_lines(input, separator = '') ⇒ Object
Join newlines.
-
#join_url(*input) ⇒ Object
Join url fragments.
-
#main_url(input) ⇒ Object
Removes protocol, the first trailing slash and everything after e.g.
-
#root ⇒ Object
Returns the site’s baseurl or ‘/’ if the config isn’t set.
- #site_url ⇒ Object
-
#smart_slash(input) ⇒ Object
Ensure a trailing slash if a url ends with a directory.
- #smartquotes(input) ⇒ Object
-
#strip_baseurls(input) ⇒ Object
If a baseurl has been manually added to a url, this ensures it isn’t added twice.
- #strip_url_protocol(input) ⇒ Object
-
#titlecase(input) ⇒ Object
Returns a title cased string based on John Gruber’s title case Info: daringfireball.net/2008/08/title_case_update.
-
#unorphan(input) ⇒ Object
Prevent orphans in text by inserting a non-breaking space between the two last words of a string.
Instance Attribute Details
#site ⇒ Object
If filters are called before hooks, use Octopress.site if available
11 12 13 |
# File 'lib/octopress-filters.rb', line 11 def site @site end |
Instance Method Details
#baseurl ⇒ Object
25 26 27 |
# File 'lib/octopress-filters.rb', line 25 def baseurl Octopress::Filters.site.config['baseurl'] || Octopress::Filters.site.config['root'] end |
#canonical_url(input) ⇒ Object
Convert url input into a standard canonical url by expanding urls and removing url fragments ending with ‘index.`
81 82 83 |
# File 'lib/octopress-filters.rb', line 81 def canonical_url(input) full_url(input).sub(/index\.\w+$/i, '') end |
#cdata_escape(input) ⇒ Object
Escapes HTML content for XML
38 39 40 |
# File 'lib/octopress-filters.rb', line 38 def cdata_escape(input) input.gsub(/<!\[CDATA\[/, '<![CDATA[').gsub(/\]\]>/, ']]>') end |
#classify(input) ⇒ Object Also known as: sluggify
Formats a string for use as a css classname, removing illegal characters
53 54 55 |
# File 'lib/octopress-filters.rb', line 53 def classify(input) input.gsub(/ /,'-').gsub(/[^\w-]/,'').downcase end |
#compact_newlines(input) ⇒ Object
Remove empty lines
60 61 62 |
# File 'lib/octopress-filters.rb', line 60 def compact_newlines(input) input.gsub(/\n{2,}/, "\n").gsub(/^ +\n/,"") end |
#expand_url(input, url = nil) ⇒ Object
Prepends input with a url fragment
input - An absolute url, e.g. /images/awesome.gif url - The fragment to prepend the input, e.g. /blog
Returns the modified url, e.g /blog
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/octopress-filters.rb', line 124 def (input, url=nil) url ||= root url = if input.start_with?("http", url) input else File.join(url, input) end smart_slash(url) end |
#expand_urls(input, url = nil) ⇒ Object
Prepend all absolute urls with a url fragment
input - The content of a page or post url - The fragment to prepend absolute urls
Returns input with modified urls
151 152 153 154 155 156 |
# File 'lib/octopress-filters.rb', line 151 def (input, url=nil) url ||= root input.gsub /(\s+(href|src|poster)\s*=\s*["|'])(\/[^\/][^"'>]*)/ do $1 + ($3, url) end end |
#full_url(input) ⇒ Object
Prepend a url with the full site url
input - a url
Returns input with all urls expanded to include the full site url e.g. /images/awesome.gif => 
113 114 115 |
# File 'lib/octopress-filters.rb', line 113 def full_url(input) (input, site_url) end |
#full_urls(input) ⇒ Object
Prepend all urls with the full site url
input - The content of a page or post
Returns input with all urls expanded to include the full site url e.g. /images/awesome.gif => 
92 93 94 |
# File 'lib/octopress-filters.rb', line 92 def full_urls(input) (strip_baseurls(input), site_url) end |
#join_lines(input, separator = '') ⇒ Object
Join newlines
70 71 72 |
# File 'lib/octopress-filters.rb', line 70 def join_lines(input, separator='') compact_newlines(input).strip.gsub(/\s*\n\s*/, separator) end |
#join_url(*input) ⇒ Object
Join url fragments
65 66 67 |
# File 'lib/octopress-filters.rb', line 65 def join_url(*input) smart_slash(File.join(input)) end |
#main_url(input) ⇒ Object
Removes protocol, the first trailing slash and everything after
e.g. https://foo.com/ -> foo.com
165 166 167 |
# File 'lib/octopress-filters.rb', line 165 def main_url(input) strip_url_protocol(input).split('/').first end |
#root ⇒ Object
Returns the site’s baseurl or ‘/’ if the config isn’t set
21 22 23 |
# File 'lib/octopress-filters.rb', line 21 def root baseurl.nil? ? '/' : File.join('/', baseurl) end |
#site_url ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/octopress-filters.rb', line 29 def site_url @url ||= begin File.join(Octopress::Filters.site.config['url'], root) rescue raise IOError.new "Please add your site's published url to your _config.yml, eg url: http://example.com" end end |
#smart_slash(input) ⇒ Object
Ensure a trailing slash if a url ends with a directory
137 138 139 140 141 142 |
# File 'lib/octopress-filters.rb', line 137 def smart_slash(input) if !(input =~ /\.\w+$/) input = File.join(input, '/') end input end |
#smartquotes(input) ⇒ Object
48 49 50 |
# File 'lib/octopress-filters.rb', line 48 def smartquotes(input) RubyPants.new(input).to_html end |
#strip_baseurls(input) ⇒ Object
If a baseurl has been manually added to a url, this ensures it isn’t added twice
98 99 100 101 102 103 104 |
# File 'lib/octopress-filters.rb', line 98 def strip_baseurls(input) if !baseurl.empty? input.gsub /(\s+(href|src|poster)\s*=\s*["|'])(\/#{baseurl})/, '\1' else input end end |
#strip_url_protocol(input) ⇒ Object
158 159 160 |
# File 'lib/octopress-filters.rb', line 158 def strip_url_protocol(input) input.sub(/\w+?:\/\//,'') end |
#titlecase(input) ⇒ Object
Returns a title cased string based on John Gruber’s title case Info: daringfireball.net/2008/08/title_case_update
44 45 46 |
# File 'lib/octopress-filters.rb', line 44 def titlecase(input) input.titlecase end |
#unorphan(input) ⇒ Object
Prevent orphans in text by inserting a non-breaking space between the two last words of a string.
75 76 77 |
# File 'lib/octopress-filters.rb', line 75 def unorphan(input) input.sub(/\s+(\S+)\s*$/, ' \1') end |