plugins/octopress_filters.rb
e4c2d579
 #custom filters for Octopress
3d2d1a8b
 require './plugins/backtick_code_block'
 require './plugins/post_filters'
 require './plugins/raw'
c2a68cc2
 require './plugins/date'
3d2d1a8b
 require 'rubypants'
e4c2d579
 
 module OctopressFilters
3d2d1a8b
   include BacktickCodeBlock
   include TemplateWrapper
   def pre_filter(input)
     input = render_code_block(input)
     input.gsub /(<figure.+?>.+?<\/figure>)/m do
       safe_wrap($1)
     end
   end
   def post_filter(input)
     input = unwrap(input)
     RubyPants.new(input).to_html
   end
 end
 
 module Jekyll
   class ContentFilters < PostFilter
     include OctopressFilters
     def pre_render(post)
       post.content = pre_filter(post.content)
     end
     def post_render(post)
       post.content = post_filter(post.content)
     end
   end
 end
 
 
 module OctopressLiquidFilters
c2a68cc2
   include Octopress::Date
 
353ccfd4
   # Used on the blog index to split posts on the <!--more--> marker
da38dbe5
   def excerpt(input)
353ccfd4
     if input.index(/<!--\s*more\s*-->/i)
       input.split(/<!--\s*more\s*-->/i)[0]
f77db800
     else
       input
     end
   end
596ec87c
 
   # Checks for excerpts (helpful for template conditionals)
   def has_excerpt(input)
     input =~ /<!--\s*more\s*-->/i ? true : false
   end
353ccfd4
 
   # Summary is used on the Archive pages to return the first block of content from a post.
   def summary(input)
     if input.index(/\n\n/)
       input.split(/\n\n/)[0]
e4c2d579
     else
       input
     end
   end
353ccfd4
 
0b5215c0
   # Extracts raw content DIV from template, used for page description as {{ content }}
   # contains complete sub-template code on main page level
   def raw_content(input)
3dbc379c
     /<div class="entry-content">(?<content>[\s\S]*?)<\/div>\s*<(footer|\/article)>/ =~ input
0b5215c0
     return (content.nil?) ? input : content
   end
 
6315527b
   # Escapes CDATA sections in post content
   def cdata_escape(input)
     input.gsub(/<!\[CDATA\[/, '&lt;![CDATA[').gsub(/\]\]>/, ']]&gt;')
   end
 
353ccfd4
   # Replaces relative urls with full urls
39d56bc9
   def expand_urls(input, url='')
     url ||= '/'
a5f87149
     input.gsub /(\s+(href|src)\s*=\s*["|']{1})(\/[^\"'>]*)/ do
e4c2d579
       $1+url+$3
     end
   end
353ccfd4
 
a81ef5e2
   # Improved version of Liquid's truncate:
   # - Doesn't cut in the middle of a word.
   # - Uses typographically correct ellipsis (…) insted of '...'
   def truncate(input, length)
     if input.length > length && input[0..(length-1)] =~ /(.+)\b.+$/im
       $1.strip + ' &hellip;'
     else
       input
     end
   end
 
   # Improved version of Liquid's truncatewords:
   # - Uses typographically correct ellipsis (…) insted of '...'
   def truncatewords(input, length)
     truncate = input.split(' ')
     if truncate.length > length
       truncate[0..length-1].join(' ').strip + ' &hellip;'
     else
       input
     end
   end
 
   # Condenses multiple spaces and tabs into a single space
   def condense_spaces(input)
     input.gsub(/\s{2,}/, ' ')
   end
 
39d56bc9
   # Removes trailing forward slash from a string for easily appending url segments
   def strip_slash(input)
     if input =~ /(.+)\/$|^\/$/
       input = $1
     end
     input
   end
 
   # Returns a url without the protocol (http://)
   def shorthand_url(input)
353ccfd4
     input.gsub /(https?:\/\/)(\S+)/ do
c7d5365f
       $2
     end
   end
353ccfd4
 
   # Returns a title cased string based on John Gruber's title case http://daringfireball.net/2008/08/title_case_update
e4c2d579
   def titlecase(input)
     input.titlecase
   end
353ccfd4
 
e4c2d579
 end
3d2d1a8b
 Liquid::Template.register_filter OctopressLiquidFilters
d0f56ef1