diff options
Diffstat (limited to '')
-rw-r--r-- | _plugins/custom_filters.rb | 46 | ||||
-rw-r--r-- | _plugins/generate_sitemap.rb | 132 | ||||
-rw-r--r-- | _plugins/gist_tag.rb | 83 | ||||
-rw-r--r-- | _plugins/haml.rb | 24 | ||||
-rw-r--r-- | _plugins/titlecase.rb | 36 |
5 files changed, 321 insertions, 0 deletions
diff --git a/_plugins/custom_filters.rb b/_plugins/custom_filters.rb new file mode 100644 index 00000000..2c58e677 --- /dev/null +++ b/_plugins/custom_filters.rb @@ -0,0 +1,46 @@ +#custom filters for Octopress + +module OctopressFilters + def exerpt(input, url, url_text="Reade more…", permalink_text=false) + if input.index(/<!--\s?more\s?-->/i) + input.split(/<!--\s?more\s?-->/i)[0] + "<p><a href='#{url}'>#{url_text}</a></p>" + elsif permalink_text + input + "<p><a href='#{url}'>#{permalink_text}</a></p>" + else + input + end + end + def full_urls(input, url='') + input.gsub /(\s+(href|src)\s*=\s*["|']{1})(\/[^\"'>]+)/ do + $1+url+$3 + end + end + def smart_quotes(input) + require 'rubypants' + RubyPants.new(input).to_html + end + def titlecase(input) + require 'titlecase' + input.titlecase + end + def ordinalize(date) + if date.class == String + date = Time.parse(date) + end + "#{date.strftime('%B')} #{ordinal(date.strftime('%e').to_i)}, #{date.strftime('%Y')}" + end + 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>" + when 3; "#{number}<span>rd</span>" + else "#{number}<span>th</span>" + end + end + end +end + +Liquid::Template.register_filter OctopressFilters diff --git a/_plugins/generate_sitemap.rb b/_plugins/generate_sitemap.rb new file mode 100644 index 00000000..488a992b --- /dev/null +++ b/_plugins/generate_sitemap.rb @@ -0,0 +1,132 @@ +# Jekyll sitemap page generator. +# http://recursive-design.com/projects/jekyll-plugins/ +# +# Version: 0.1.3 (201101061053) +# +# Copyright (c) 2010 Dave Perrett, http://recursive-design.com/ +# Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php) +# +# A generator that creates a sitemap.xml page for jekyll sites, suitable for submission to +# google etc. +# +# To use it, simply drop this script into the _plugins directory of your Jekyll site. +# +# When you compile your jekyll site, this plugin will loop through the list of pages in your +# site, and generate an entry in sitemap.xml for each one. + +require 'pathname' + +module Jekyll + + + # Monkey-patch an accessor for a page's containing folder, since + # we need it to generate the sitemap. + class Page + def subfolder + @dir + end + end + + + # Sub-class Jekyll::StaticFile to allow recovery from unimportant exception + # when writing the sitemap file. + class StaticSitemapFile < StaticFile + def write(dest) + super(dest) rescue ArgumentError + true + end + end + + + # Generates a sitemap.xml file containing URLs of all pages and posts. + class SitemapGenerator < Generator + safe true + priority :low + + # Domain that you are generating the sitemap for - update this to match your site. + BASE_URL = 'http://recursive-design.com' + + # Generates the sitemap.xml file. + # + # +site+ is the global Site object. + def generate(site) + # Create the destination folder if necessary. + site_folder = site.config['destination'] + unless File.directory?(site_folder) + p = Pathname.new(site_folder) + p.mkdir + end + + # Write the contents of sitemap.xml. + File.open(File.join(site_folder, 'sitemap.xml'), 'w') do |f| + f.write(generate_header()) + f.write(generate_content(site)) + f.write(generate_footer()) + f.close + end + + # Add a static file entry for the zip file, otherwise Site::cleanup will remove it. + site.static_files << Jekyll::StaticSitemapFile.new(site, site.dest, '/', 'sitemap.xml') + end + + private + + # Returns the XML header. + def generate_header + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">" + end + + # Returns a string containing the the XML entries. + # + # +site+ is the global Site object. + def generate_content(site) + result = '' + + # First, try to find any stand-alone pages. + site.pages.each{ |page| + path = page.subfolder + '/' + page.name + mod_date = File.mtime(site.source + path) + + # Remove the trailing 'index.html' if there is one, and just output the folder name. + if path=~/index.html$/ + path = path[0..-11] + end + + unless path =~/error/ + result += entry(path, mod_date) + end + } + + # Next, find all the posts. + posts = site.site_payload['site']['posts'] + for post in posts do + result += entry(post.id, post.date) + end + + result + end + + # Returns the XML footer. + def generate_footer + "\n</urlset>" + end + + # Creates an XML entry from the given path and date. + # + # +path+ is the URL path to the page. + # +date+ is the date the file was modified (in the case of regular pages), or published (for blog posts). + def entry(path, date) + # Force extensions to .html from markdown, textile. + path = path.gsub(/\.(markdown|textile)$/i, '.html') + " + <url> + <loc>#{BASE_URL}#{path}</loc> + <lastmod>#{date.strftime("%Y-%m-%d")}</lastmod> + </url>" + end + + end + +end + + diff --git a/_plugins/gist_tag.rb b/_plugins/gist_tag.rb new file mode 100644 index 00000000..1f37416e --- /dev/null +++ b/_plugins/gist_tag.rb @@ -0,0 +1,83 @@ +# Nicked from Brandon Tilly +# Gist https://gist.github.com/803483 +# Post http://brandontilley.com/2011/01/31/gist-tag-for-jekyll.html +# +# Example usage: {% gist 803483 gist_tag.rb %} //embeds a gist for this plugin + +require 'digest/md5' +require 'net/https' +require 'uri' + +module Jekyll + class GistTag < Liquid::Tag + def initialize(tag_name, text, token) + super + system('mkdir -p .gist_cache') + @text = text + @cache = true + @cache_folder = File.expand_path "../.gist_cache", File.dirname(__FILE__) + end + + def render(context) + return "" unless @text =~ /([\d]*) (.*)/ + + gist, file = $1.strip, $2.strip + script_url = "https://gist.github.com/#{gist}.js?file=#{file}" + + code = get_cached_gist(gist, file) || get_gist_from_web(gist, file) + code = code.gsub "<", "<" + string = "<script src='#{script_url}'></script>" + string += "<noscript><pre><code>#{code}</code></pre></noscript>" + return string + end + + def get_gist_url_for(gist, file) + "https://gist.github.com/raw/#{gist}/#{file}" + end + + def cache_gist(gist, file, data) + file = get_cache_file_for gist, file + File.open(file, "w+") do |f| + f.write(data) + end + end + + def get_cached_gist(gist, file) + return nil if @cache == false + file = get_cache_file_for gist, file + return nil unless File.exist?(file) + return File.new(file).readlines.join + end + + def get_cache_file_for(gist, file) + gist.gsub! /[^a-zA-Z0-9\-_\.]/, '' + file.gsub! /[^a-zA-Z0-9\-_\.]/, '' + md5 = Digest::MD5.hexdigest "#{gist}-#{file}" + File.join @cache_folder, "#{gist}-#{file}-#{md5}.cache" + end + + def get_gist_from_web(gist, file) + gist_url = get_gist_url_for(gist, file) + raw_uri = URI.parse(gist_url) + https = Net::HTTP.new(raw_uri.host, raw_uri.port) + https.use_ssl = true + https.verify_mode = OpenSSL::SSL::VERIFY_NONE + request = Net::HTTP::Get.new(raw_uri.request_uri) + data = https.request(request) + data = data.body + cache_gist(gist, file, data) unless @cache == false + data + end + end + + class GistTagNoCache < GistTag + def initialize(tag_name, text, token) + super + @cache = false + end + end +end + +Liquid::Template.register_tag('gist', Jekyll::GistTag) +Liquid::Template.register_tag('gistnocache', Jekyll::GistTagNoCache) + diff --git a/_plugins/haml.rb b/_plugins/haml.rb new file mode 100644 index 00000000..7e548dec --- /dev/null +++ b/_plugins/haml.rb @@ -0,0 +1,24 @@ +module Jekyll + require 'haml' + class HamlConverter < Converter + safe true + priority :low + + def matches(ext) + ext =~ /haml/i + end + + def output_ext(ext) + ".html" + end + + def convert(content) + begin + engine = Haml::Engine.new(content) + engine.render + rescue StandardError => e + puts "!!! HAML Error: " + e.message + end + end + end +end diff --git a/_plugins/titlecase.rb b/_plugins/titlecase.rb new file mode 100644 index 00000000..103bf702 --- /dev/null +++ b/_plugins/titlecase.rb @@ -0,0 +1,36 @@ +class String + def titlecase + small_words = %w(a an and as at but by en for if in of on or the to v v. via vs vs.) + + x = split(" ").map do |word| + # note: word could contain non-word characters! + # downcase all small_words, capitalize the rest + small_words.include?(word.gsub(/\W/, "").downcase) ? word.downcase! : word.smart_capitalize! + word + end + # capitalize first and last words + x.first.to_s.smart_capitalize! + x.last.to_s.smart_capitalize! + # small words after colons are capitalized + x.join(" ").gsub(/:\s?(\W*#{small_words.join("|")}\W*)\s/) { ": #{$1.smart_capitalize} " } + end + + def titlecase! + replace(titlecase) + end + + def smart_capitalize + # ignore any leading crazy characters and capitalize the first real character + if self =~ /^['"\(\[']*([a-z])/ + i = index($1) + x = self[i,self.length] + # word with capitals and periods mid-word are left alone + self[i,1] = self[i,1].upcase unless x =~ /[A-Z]/ or x =~ /\.\w+/ + end + self + end + + def smart_capitalize! + replace(smart_capitalize) + end +end |