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.3.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#siteObject

Returns the value of attribute site.



8
9
10
# File 'lib/octopress-filters.rb', line 8

def site
  @site
end

Instance Method Details

#baseurlObject



16
17
18
# File 'lib/octopress-filters.rb', line 16

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.`



72
73
74
# File 'lib/octopress-filters.rb', line 72

def canonical_url(input)
  full_url(input).downcase.sub(/index\.\w+$/i, '')
end

#cdata_escape(input) ⇒ Object

Escapes HTML content for XML



29
30
31
# File 'lib/octopress-filters.rb', line 29

def cdata_escape(input)
  input.gsub(/<!\[CDATA\[/, '&lt;![CDATA[').gsub(/\]\]>/, ']]&gt;')
end

#classify(input) ⇒ Object Also known as: sluggify

Formats a string for use as a css classname, removing illegal characters



44
45
46
# File 'lib/octopress-filters.rb', line 44

def classify(input)
  input.gsub(/ /,'-').gsub(/[^\w-]/,'').downcase
end

#compact_newlines(input) ⇒ Object

Remove empty lines



51
52
53
# File 'lib/octopress-filters.rb', line 51

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



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/octopress-filters.rb', line 115

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) ⇒ 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



142
143
144
145
146
147
# File 'lib/octopress-filters.rb', line 142

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) ⇒ 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 =>



104
105
106
# File 'lib/octopress-filters.rb', line 104

def full_url(input)
  expand_url(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 =>



83
84
85
# File 'lib/octopress-filters.rb', line 83

def full_urls(input)
  expand_urls(strip_baseurls(input), site_url)
end

#join_lines(input, separator = '') ⇒ Object

Join newlines



61
62
63
# File 'lib/octopress-filters.rb', line 61

def join_lines(input, separator='')
  compact_newlines(input).strip.gsub(/\s*\n\s*/, separator)
end

#join_url(*input) ⇒ Object

Join url fragments



56
57
58
# File 'lib/octopress-filters.rb', line 56

def join_url(*input)
  smart_slash(File.join(input))
end

#rootObject

Returns the site’s baseurl or ‘/’ if the config isn’t set



12
13
14
# File 'lib/octopress-filters.rb', line 12

def root
  baseurl.nil? ? '/' : File.join('/', baseurl)
end

#site_urlObject



20
21
22
23
24
25
26
# File 'lib/octopress-filters.rb', line 20

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



128
129
130
131
132
133
# File 'lib/octopress-filters.rb', line 128

def smart_slash(input)
  if !(input =~ /\.\w+$/)
    input = File.join(input, '/')
  end
  input
end

#smartquotes(input) ⇒ Object



39
40
41
# File 'lib/octopress-filters.rb', line 39

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



89
90
91
92
93
94
95
# File 'lib/octopress-filters.rb', line 89

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



149
150
151
# File 'lib/octopress-filters.rb', line 149

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



35
36
37
# File 'lib/octopress-filters.rb', line 35

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.



66
67
68
# File 'lib/octopress-filters.rb', line 66

def unorphan(input)
  input.sub(/\s+(\S+)\s*$/, '&nbsp;\1')
end