module Octopress::Filters

Constants

VERSION

Attributes

site[RW]

Public Instance Methods

baseurl() click to toggle source
# File lib/octopress-filters.rb, line 25
def baseurl
  Octopress::Filters.site.config['baseurl'] || Octopress::Filters.site.config['root']
end
canonical_url(input) click to toggle source

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
cdata_escape(input) click to toggle source

Escapes HTML content for XML

# File lib/octopress-filters.rb, line 38
def cdata_escape(input)
  input.gsub(/<!\[CDATA\[/, '&lt;![CDATA[').gsub(/\]\]>/, ']]&gt;')
end
classify(input) click to toggle source

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
Also aliased as: sluggify
compact_newlines(input) click to toggle source

Remove empty lines

# File lib/octopress-filters.rb, line 60
def compact_newlines(input)
  input.gsub(/\n{2,}/, "\n").gsub(/^ +\n/,"")
end
expand_url(input, url=nil) click to toggle source

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
expand_urls(input, url=nil) click to toggle source

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
full_url(input) click to toggle source

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
full_urls(input) click to toggle source

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_lines(input, separator='') click to toggle source

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(*input) click to toggle source

Join url fragments

# File lib/octopress-filters.rb, line 65
def join_url(*input)
  smart_slash(File.join(input))
end
main_url(input) click to toggle source

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
root() click to toggle source

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
site_url() click to toggle source
# 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
sluggify(input)
Alias for: classify
smart_slash(input) click to toggle source

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
smartquotes(input) click to toggle source
# File lib/octopress-filters.rb, line 48
def smartquotes(input)
  RubyPants.new(input).to_html
end
strip_baseurls(input) click to toggle source

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
strip_url_protocol(input) click to toggle source
# File lib/octopress-filters.rb, line 158
def strip_url_protocol(input)
  input.sub(/\w+?:\/\//,'')
end
titlecase(input) click to toggle source

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
unorphan(input) click to toggle source

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*$/, '&nbsp;\1')
end