2011-04-18 09:19:30 +05:30
|
|
|
#custom filters for Octopress
|
2011-07-27 09:07:31 +05:30
|
|
|
require './plugins/pygments_code'
|
2011-04-18 09:19:30 +05:30
|
|
|
|
|
|
|
module OctopressFilters
|
2011-07-27 09:07:31 +05:30
|
|
|
include HighlightCode
|
2011-06-28 01:29:21 +05:30
|
|
|
# Used on the blog index to split posts on the <!--more--> marker
|
2011-07-23 10:46:40 +05:30
|
|
|
def excerpt(input)
|
2011-06-28 01:29:21 +05:30
|
|
|
if input.index(/<!--\s*more\s*-->/i)
|
|
|
|
input.split(/<!--\s*more\s*-->/i)[0]
|
2011-06-20 00:44:01 +05:30
|
|
|
else
|
|
|
|
input
|
|
|
|
end
|
|
|
|
end
|
2011-06-28 01:29:21 +05:30
|
|
|
|
|
|
|
# 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-18 09:19:30 +05:30
|
|
|
else
|
|
|
|
input
|
|
|
|
end
|
|
|
|
end
|
2011-06-28 01:29:21 +05:30
|
|
|
|
2011-07-27 09:07:31 +05:30
|
|
|
# for Github style codeblocks eg.
|
|
|
|
# ``` ruby
|
|
|
|
# code snippet
|
|
|
|
# ```
|
|
|
|
def backtick_codeblock(input)
|
2011-07-29 00:14:18 +05:30
|
|
|
# Markdown support
|
|
|
|
input = input.gsub /<p>`{3}\s(\w+)<\/p>\s*<pre><code>\s*(.+?)\s*<\/code><\/pre>\s*<p>`{3}<\/p>/m do
|
2011-07-27 09:07:31 +05:30
|
|
|
lang = $1
|
2011-07-27 09:26:14 +05:30
|
|
|
str = $2.gsub('<','<').gsub('>','>')
|
2011-07-27 09:07:31 +05:30
|
|
|
highlight(str, lang)
|
|
|
|
end
|
2011-07-29 00:14:18 +05:30
|
|
|
|
|
|
|
# Textile support
|
|
|
|
input = input.gsub /<p>`{3}\s(\w+)<br\s*\/>\n(.+?)`{3}<\/p>/m do
|
|
|
|
lang = $1
|
2011-07-29 00:51:34 +05:30
|
|
|
str = $2.gsub('<','<').gsub('>','>').gsub(/^\s{4}/, '').gsub(/(<br\s\/>)?$/, '')
|
2011-07-29 00:14:18 +05:30
|
|
|
highlight(str, lang)
|
|
|
|
end
|
|
|
|
|
2011-07-29 00:51:34 +05:30
|
|
|
# Regular HTML support
|
2011-07-29 00:14:18 +05:30
|
|
|
input.gsub /^`{3}\s(\w+)\n(.+?)\n`{3}/m do
|
|
|
|
lang = $1
|
|
|
|
str = $2.gsub(/^\s{4}/, '')
|
|
|
|
highlight(str, lang)
|
|
|
|
end
|
2011-07-27 09:07:31 +05:30
|
|
|
end
|
|
|
|
|
2011-06-28 01:29:21 +05:30
|
|
|
# Replaces relative urls with full urls
|
2011-07-22 09:20:32 +05:30
|
|
|
def expand_urls(input, url='')
|
|
|
|
url ||= '/'
|
2011-04-18 09:19:30 +05:30
|
|
|
input.gsub /(\s+(href|src)\s*=\s*["|']{1})(\/[^\"'>]+)/ do
|
|
|
|
$1+url+$3
|
|
|
|
end
|
|
|
|
end
|
2011-06-28 01:29:21 +05:30
|
|
|
|
2011-07-22 09:20:32 +05:30
|
|
|
# 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-28 01:29:21 +05:30
|
|
|
input.gsub /(https?:\/\/)(\S+)/ do
|
2011-05-16 04:03:00 +05:30
|
|
|
$2
|
|
|
|
end
|
|
|
|
end
|
2011-06-28 01:29:21 +05:30
|
|
|
|
|
|
|
# replaces primes with smartquotes using RubyPants
|
2011-04-18 09:19:30 +05:30
|
|
|
def smart_quotes(input)
|
|
|
|
require 'rubypants'
|
|
|
|
RubyPants.new(input).to_html
|
|
|
|
end
|
2011-06-28 01:29:21 +05:30
|
|
|
|
|
|
|
# Returns a title cased string based on John Gruber's title case http://daringfireball.net/2008/08/title_case_update
|
2011-04-18 09:19:30 +05:30
|
|
|
def titlecase(input)
|
|
|
|
input.titlecase
|
|
|
|
end
|
2011-06-28 01:29:21 +05:30
|
|
|
|
|
|
|
# Returns a datetime if the input is a string
|
2011-05-16 04:03:00 +05:30
|
|
|
def datetime(date)
|
2011-04-18 09:19:30 +05:30
|
|
|
if date.class == String
|
|
|
|
date = Time.parse(date)
|
|
|
|
end
|
2011-05-16 04:03:00 +05:30
|
|
|
date
|
|
|
|
end
|
2011-06-28 01:29:21 +05:30
|
|
|
|
|
|
|
# Returns an ordidinal date eg July 22 2007 -> July 22nd 2007
|
2011-05-16 04:03:00 +05:30
|
|
|
def ordinalize(date)
|
|
|
|
date = datetime(date)
|
2011-06-20 00:44:01 +05:30
|
|
|
"#{date.strftime('%b')} #{ordinal(date.strftime('%e').to_i)}, #{date.strftime('%Y')}"
|
2011-04-18 09:19:30 +05:30
|
|
|
end
|
2011-06-28 01:29:21 +05:30
|
|
|
|
|
|
|
# Returns an ordinal number. 13 -> 13th, 21 -> 21st etc.
|
2011-04-18 09:19:30 +05:30
|
|
|
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-25 02:47:35 +05:30
|
|
|
when 2; "#{number}<span>nd</span>"
|
2011-04-18 09:19:30 +05:30
|
|
|
when 3; "#{number}<span>rd</span>"
|
|
|
|
else "#{number}<span>th</span>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Liquid::Template.register_filter OctopressFilters
|
2011-07-27 09:26:14 +05:30
|
|
|
|