e4c2d579 |
#custom filters for Octopress |
6e2beaa9 |
require './plugins/pygments_code' |
e4c2d579 |
module OctopressFilters |
6e2beaa9 |
include HighlightCode |
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 |
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 |
|
6e2beaa9 |
# for Github style codeblocks eg.
# ``` ruby
# code snippet
# ```
def backtick_codeblock(input) |
ef4a42f9 |
code = nil |
7209aaf6 |
# Markdown support |
49a67785 |
input = input.gsub /<p>`{3}\s*(\w+)?<\/p>\s*<pre><code>\s*(.+?)\s*<\/code><\/pre>\s*<p>`{3}<\/p>/m do |
6e2beaa9 |
lang = $1 |
49a67785 |
if lang != '' |
ff1dba19 |
str = $2.gsub('<','<').gsub('>','>').gsub('&','&') |
ef4a42f9 |
code = highlight(str, lang)
"<figure role=code>#{code}</figure>" |
49a67785 |
else |
ef4a42f9 |
code = tableize_code($2)
"<figure role=code>#{code}</figure>" |
49a67785 |
end |
6e2beaa9 |
end |
7209aaf6 |
|
ff1dba19 |
# Textile warning |
49a67785 |
input = input.gsub /<p>`{3}\s*(\w+)?<br\s*\/>\n(.+?)`{3}<\/p>/m do |
7209aaf6 |
lang = $1 |
ff1dba19 |
"<pre><code>Back tick code blocks are not supported for Textile.\nTry HTML or Markdown instead or use the codeblock tag.\n\n{% codeblock #{lang} %}\nYour code snippet\n{% endcodeblock %}</code></pre>" |
7209aaf6 |
end
|
a516c773 |
# Regular HTML support |
49a67785 |
input.gsub /^`{3}\s*(\w+)?\n(.+?)\n`{3}/m do |
7209aaf6 |
lang = $1
str = $2.gsub(/^\s{4}/, '') |
49a67785 |
if lang != '' |
ef4a42f9 |
code = highlight(str, lang)
"<figure role=code>#{code}</figure>" |
49a67785 |
else |
ef4a42f9 |
code = tableize_code($2.gsub('<','<').gsub('>','>'))
"<figure role=code>#{code}</figure>" |
49a67785 |
end |
7209aaf6 |
end |
6e2beaa9 |
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 |
|
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 |
# replaces primes with smartquotes using RubyPants |
e4c2d579 |
def smart_quotes(input)
require 'rubypants'
RubyPants.new(input).to_html
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 |
# Returns a datetime if the input is a string |
c7d5365f |
def datetime(date) |
e4c2d579 |
if date.class == String
date = Time.parse(date)
end |
c7d5365f |
date
end |
353ccfd4 |
# Returns an ordidinal date eg July 22 2007 -> July 22nd 2007 |
c7d5365f |
def ordinalize(date)
date = datetime(date) |
f77db800 |
"#{date.strftime('%b')} #{ordinal(date.strftime('%e').to_i)}, #{date.strftime('%Y')}" |
e4c2d579 |
end |
353ccfd4 |
# Returns an ordinal number. 13 -> 13th, 21 -> 21st etc. |
e4c2d579 |
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>" |
ef3ff431 |
when 2; "#{number}<span>nd</span>" |
e4c2d579 |
when 3; "#{number}<span>rd</span>"
else "#{number}<span>th</span>"
end
end
end
end
Liquid::Template.register_filter OctopressFilters |
d0f56ef1 |
|