www.moparisthebest.com/plugins/octopress_filters.rb

85 lines
2.0 KiB
Ruby
Raw Normal View History

2011-04-17 23:49:30 -04:00
#custom filters for Octopress
module OctopressFilters
# Used on the blog index to split posts on the <!--more--> marker
def excerpt(input)
if input.index(/<!--\s*more\s*-->/i)
input.split(/<!--\s*more\s*-->/i)[0]
else
input
end
end
# 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
# Replaces relative urls with full urls
def expand_urls(input, url='')
url ||= '/'
2011-04-17 23:49:30 -04:00
input.gsub /(\s+(href|src)\s*=\s*["|']{1})(\/[^\"'>]+)/ do
$1+url+$3
end
end
# 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)
input.gsub /(https?:\/\/)(\S+)/ do
$2
end
end
# replaces primes with smartquotes using RubyPants
2011-04-17 23:49:30 -04:00
def smart_quotes(input)
require 'rubypants'
RubyPants.new(input).to_html
end
# 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
# Returns a datetime if the input is a string
def datetime(date)
2011-04-17 23:49:30 -04:00
if date.class == String
date = Time.parse(date)
end
date
end
# Returns an ordidinal date eg July 22 2007 -> July 22nd 2007
def ordinalize(date)
date = datetime(date)
"#{date.strftime('%b')} #{ordinal(date.strftime('%e').to_i)}, #{date.strftime('%Y')}"
2011-04-17 23:49:30 -04:00
end
# 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>"
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
Liquid::Template.register_filter OctopressFilters