module Octopress::Filters
Constants
- VERSION
Attributes
Public Instance Methods
# File lib/octopress-filters.rb, line 25 def baseurl Octopress::Filters.site.config['baseurl'] || Octopress::Filters.site.config['root'] end
Convert url input into a standard canonical url by expanding urls and removing url fragments ending with ‘index.`
# File lib/octopress-filters.rb, line 81 def canonical_url(input) full_url(input).sub(/index\.\w+$/i, '') end
Escapes HTML content for XML
# File lib/octopress-filters.rb, line 38 def cdata_escape(input) input.gsub(/<!\[CDATA\[/, '<![CDATA[').gsub(/\]\]>/, ']]>') end
Formats a string for use as a css classname, removing illegal characters
# File lib/octopress-filters.rb, line 53 def classify(input) input.gsub(/ /,'-').gsub(/[^\w-]/,'').downcase end
Remove empty lines
# File lib/octopress-filters.rb, line 60 def compact_newlines(input) input.gsub(/\n{2,}/, "\n").gsub(/^ +\n/,"") end
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
# File lib/octopress-filters.rb, line 124 def expand_url(input, url=nil) url ||= root url = if input.start_with?("http", url) input else File.join(url, input) end smart_slash(url) end
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
# File lib/octopress-filters.rb, line 151 def expand_urls(input, url=nil) url ||= root input.gsub /(\s+(href|src|poster)\s*=\s*["|'])(\/[^\/][^"'>]*)/ do $1 + expand_url($3, url) end end
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 =>
# File lib/octopress-filters.rb, line 113 def full_url(input) expand_url(input, site_url) end
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 =>
# File lib/octopress-filters.rb, line 92 def full_urls(input) expand_urls(strip_baseurls(input), site_url) end
Join newlines
# File lib/octopress-filters.rb, line 70 def join_lines(input, separator='') compact_newlines(input).strip.gsub(/\s*\n\s*/, separator) end
Join url fragments
# File lib/octopress-filters.rb, line 65 def join_url(*input) smart_slash(File.join(input)) end
Removes protocol, the first trailing slash and everything after
e.g. https://foo.com/bar -> foo.com
# File lib/octopress-filters.rb, line 165 def main_url(input) strip_url_protocol(input).split('/').first end
Returns the site’s baseurl or ‘/’ if the config isn’t set
# File lib/octopress-filters.rb, line 21 def root baseurl.nil? ? '/' : File.join('/', baseurl) end
# 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
Ensure a trailing slash if a url ends with a directory
# File lib/octopress-filters.rb, line 137 def smart_slash(input) if !(input =~ /\.\w+$/) input = File.join(input, '/') end input end
# File lib/octopress-filters.rb, line 48 def smartquotes(input) RubyPants.new(input).to_html end
If a baseurl has been manually added to a url, this ensures it isn’t added twice
# 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
# File lib/octopress-filters.rb, line 158 def strip_url_protocol(input) input.sub(/\w+?:\/\//,'') end
Returns a title cased string based on John Gruber’s title case Info: daringfireball.net/2008/08/title_case_update
# File lib/octopress-filters.rb, line 44 def titlecase(input) input.titlecase end
Prevent orphans in text by inserting a non-breaking space between the two last words of a string.
# File lib/octopress-filters.rb, line 75 def unorphan(input) input.sub(/\s+(\S+)\s*$/, ' \1') end