2011-04-17 23:49:30 -04:00
|
|
|
#custom filters for Octopress
|
2011-09-07 19:32:57 -04:00
|
|
|
require './plugins/backtick_code_block'
|
|
|
|
require './plugins/post_filters'
|
|
|
|
require './plugins/raw'
|
|
|
|
require 'rubypants'
|
2011-04-17 23:49:30 -04:00
|
|
|
|
|
|
|
module OctopressFilters
|
2011-09-07 19:32:57 -04:00
|
|
|
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
|
2011-06-27 15:59:21 -04:00
|
|
|
# Used on the blog index to split posts on the <!--more--> marker
|
2011-07-23 01:16:40 -04:00
|
|
|
def excerpt(input)
|
2011-06-27 15:59:21 -04:00
|
|
|
if input.index(/<!--\s*more\s*-->/i)
|
|
|
|
input.split(/<!--\s*more\s*-->/i)[0]
|
2011-06-19 15:14:01 -04:00
|
|
|
else
|
|
|
|
input
|
|
|
|
end
|
|
|
|
end
|
2011-08-20 16:02:37 -04:00
|
|
|
|
|
|
|
# Checks for excerpts (helpful for template conditionals)
|
|
|
|
def has_excerpt(input)
|
|
|
|
input =~ /<!--\s*more\s*-->/i ? true : false
|
|
|
|
end
|
2011-06-27 15:59:21 -04:00
|
|
|
|
|
|
|
# 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]
|
2011-04-17 23:49:30 -04:00
|
|
|
else
|
|
|
|
input
|
|
|
|
end
|
|
|
|
end
|
2011-06-27 15:59:21 -04:00
|
|
|
|
2011-09-21 04:08:38 -04:00
|
|
|
# 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)
|
2011-09-21 05:41:55 -04:00
|
|
|
/<div class="entry-content">(?<content>[\s\S]*?)<\/div>\s*<(footer|\/article)>/ =~ input
|
2011-09-21 04:08:38 -04:00
|
|
|
return (content.nil?) ? input : content
|
|
|
|
end
|
|
|
|
|
2011-10-04 13:18:40 -04:00
|
|
|
# Escapes CDATA sections in post content
|
|
|
|
def cdata_escape(input)
|
|
|
|
input.gsub(/<!\[CDATA\[/, '<![CDATA[').gsub(/\]\]>/, ']]>')
|
|
|
|
end
|
|
|
|
|
2011-06-27 15:59:21 -04:00
|
|
|
# Replaces relative urls with full urls
|
2011-07-21 23:50:32 -04:00
|
|
|
def expand_urls(input, url='')
|
|
|
|
url ||= '/'
|
2011-08-16 00:41:57 -04:00
|
|
|
input.gsub /(\s+(href|src)\s*=\s*["|']{1})(\/[^\"'>]*)/ do
|
2011-04-17 23:49:30 -04:00
|
|
|
$1+url+$3
|
|
|
|
end
|
|
|
|
end
|
2011-06-27 15:59:21 -04:00
|
|
|
|
2011-07-21 23:50:32 -04:00
|
|
|
# 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)
|
2011-06-27 15:59:21 -04:00
|
|
|
input.gsub /(https?:\/\/)(\S+)/ do
|
2011-05-15 18:33:00 -04:00
|
|
|
$2
|
|
|
|
end
|
|
|
|
end
|
2011-06-27 15:59:21 -04:00
|
|
|
|
|
|
|
# Returns a title cased string based on John Gruber's title case http://daringfireball.net/2008/08/title_case_update
|
2011-04-17 23:49:30 -04:00
|
|
|
def titlecase(input)
|
|
|
|
input.titlecase
|
|
|
|
end
|
2011-06-27 15:59:21 -04:00
|
|
|
|
|
|
|
# Returns a datetime if the input is a string
|
2011-05-15 18:33:00 -04:00
|
|
|
def datetime(date)
|
2011-04-17 23:49:30 -04:00
|
|
|
if date.class == String
|
|
|
|
date = Time.parse(date)
|
|
|
|
end
|
2011-05-15 18:33:00 -04:00
|
|
|
date
|
|
|
|
end
|
2011-06-27 15:59:21 -04:00
|
|
|
|
|
|
|
# Returns an ordidinal date eg July 22 2007 -> July 22nd 2007
|
2011-05-15 18:33:00 -04:00
|
|
|
def ordinalize(date)
|
|
|
|
date = datetime(date)
|
2011-06-19 15:14:01 -04:00
|
|
|
"#{date.strftime('%b')} #{ordinal(date.strftime('%e').to_i)}, #{date.strftime('%Y')}"
|
2011-04-17 23:49:30 -04:00
|
|
|
end
|
2011-06-27 15:59:21 -04:00
|
|
|
|
|
|
|
# Returns an ordinal number. 13 -> 13th, 21 -> 21st etc.
|
2011-04-17 23:49:30 -04:00
|
|
|
def ordinal(number)
|
|
|
|
if (11..13).include?(number.to_i % 100)
|
|
|
|
"#{number}<span>th</span>"
|
|
|
|
else
|
|
|
|
case number.to_i % 10
|
|
|
|
when 1; "#{number}<span>st</span>"
|
2011-06-24 17:17:35 -04:00
|
|
|
when 2; "#{number}<span>nd</span>"
|
2011-04-17 23:49:30 -04:00
|
|
|
when 3; "#{number}<span>rd</span>"
|
|
|
|
else "#{number}<span>th</span>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-09-07 19:32:57 -04:00
|
|
|
Liquid::Template.register_filter OctopressLiquidFilters
|
2011-07-26 23:56:14 -04:00
|
|
|
|