1. Added new include_code tag to allow auhtors to insert files from the file system with syntax highligting and a download link

2. Improved the gist tag to properly insert the sources in <noscript> tags
3. Improved semantics in the blockquote plugin and DRYed things up.
4. Pygments caching now stores to the _code_cache directory by default
5. Added a configuration for the default include_code directory
6. Updated the .gitignore
This commit is contained in:
Brandon Mathis 2011-06-15 18:31:22 -04:00
parent fea1cc49d9
commit ab29d45ae8
6 changed files with 106 additions and 54 deletions

4
.gitignore vendored
View File

@ -1,8 +1,8 @@
.bundle
.DS_Store
.sass-cache
.gist_cache
_cache
_gist_cache
_code_cache
_assets
public
source/_stash

View File

@ -1,16 +1,17 @@
# Required configuration
source: source
destination: public
code_dir: downloads/code
port: 4000
url: http://yoursite.com
url: http://octopress.dev
title: My Octopress Blog
subtitle: A blogging framework for hackers.
author: Your Name
subscribe_rss: /atom.xml
subscribe_email: http://feedburner.com/asdfasdf
markdown: rdiscount
#markdown: rdiscount
pygments: true
recent_posts: 1
simple_search: http://google.com/search

View File

@ -1,21 +1,20 @@
#
# Author: Josediaz Gonzalez - https://github.com/josegonzalez
# Source URL: https://github.com/josegonzalez/josediazgonzalez.com/blob/master/_plugins/blockquote.rb
# Modified by Brandon Mathis removed pullquotes and added simple cite paramaters
# Author: Brandon Mathis
# Based on the work of: Josediaz Gonzalez - https://github.com/josegonzalez/josediazgonzalez.com/blob/master/_plugins/blockquote.rb
#
require './_plugins/titlecase.rb'
module Jekyll
# Outputs a string with a given attribution as a quote
#
# {% blockquote John Paul Jones %}
# Monkeys!
# {% blockquote Bobby Willis http://google.com/blah the search for bobby's mom %}
# Wheeee!
# {% endblockquote %}
# ...
# <blockquote>
# Monkeys!
# <br />
# John Paul Jones
# <p>Wheeee!</p>
# <footer>
# <strong>John Paul Jones</strong><cite><a href="http://google.com/blah">The Search For Bobby's Mom</a>
# </blockquote>
#
class Blockquote < Liquid::Block
@ -42,15 +41,16 @@ module Jekyll
def render(context)
output = super
if @by.nil?
'<blockquote><p>' + output.join + '</p></blockquote>'
elsif !@title.nil?
'<blockquote><p>' + output.join + '</p>' + '<p><strong>' + @by + '</strong>' + '<cite><a class="source" href="' + @source + '">' + @title + '</a></cite></p></blockquote>'
author = "<strong>#{@by}</strong>"
cite = "<cite><a class='source' href='#{@source}'>#{(@title || 'source')}</a></cite>"
reply = if @by.nil?
"<p>#{output.join.gsub(/\n\n/, '</p><p>')}</p>"
elsif !@source.nil?
'<blockquote><p>' + output.join + '</p>' + '<p><strong>' + @by + '</strong>' + '<cite><a class="source" href="' + @source + '">source</a></cite></p></blockquote>'
"<p>#{output.join.gsub(/\n\n/, '</p><p>')}</p><footer>#{author + cite}</footer>"
else
'<blockquote><p>' + output.join + '</p>' + '<p><strong>' + @by + '</strong></p></blockquote>'
"<p>#{output.join.gsub(/\n\n/, '</p><p>')}</p><footer>#{author}</footer>"
end
"<blockquote>#{reply}</blockquote>"
end
end
end

View File

@ -1,9 +1,11 @@
# Nicked from Brandon Tilly
# Gist https://gist.github.com/803483
# A Liquid tag for Jekyll sites that allows embedding Gists and showing code for non-JavaScript enabled browsers and readers.
# by: Brandon Tilly
# Source URL: https://gist.github.com/1027674
# 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
# Example usage: {% gist 1027674 gist_tag.rb %} //embeds a gist for this plugin
require 'cgi'
require 'digest/md5'
require 'net/https'
require 'uri'
@ -12,60 +14,70 @@ 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__)
@cache_disabled = false
@cache_folder = File.expand_path "../_gist_cache", File.dirname(__FILE__)
FileUtils.mkdir_p @cache_folder
end
def render(context)
return "" unless @text =~ /([\d]*) (.*)/
gist, file = $1.strip, $2.strip
script_url = "https://gist.github.com/#{gist}.js?file=#{file}"
if parts = @text.match(/([\d]*) (.*)/)
gist, file = parts[1].strip, parts[2].strip
script_url = script_url_for gist, file
code = get_cached_gist(gist, file) || get_gist_from_web(gist, file)
code = code.gsub "<", "&lt;"
string = "<script src='#{script_url}'></script>"
string += "<noscript><pre><code>#{code}</code></pre></noscript>"
return string
html_output_for script_url, code
else
""
end
end
def html_output_for(script_url, code)
code = CGI.escapeHTML code
<<-HTML
<script src='#{script_url}'></script>
<noscript><pre><code>#{code}</code></pre></noscript>
HTML
end
def script_url_for(gist_id, filename)
"https://gist.github.com/#{gist_id}.js?file=#{filename}"
end
def get_gist_url_for(gist, file)
"https://gist.github.com/raw/#{gist}/#{file}"
"https://raw.github.com/gist/#{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)
def cache(gist, file, data)
cache_file = get_cache_file_for gist, file
File.open(cache_file, "w") do |io|
io.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
return nil if @cache_disabled
cache_file = get_cache_file_for gist, file
File.read cache_file if File.exist? cache_file
end
def get_cache_file_for(gist, file)
gist.gsub! /[^a-zA-Z0-9\-_\.]/, ''
file.gsub! /[^a-zA-Z0-9\-_\.]/, ''
bad_chars = /[^a-zA-Z0-9\-_.]/
gist = gist.gsub bad_chars, ''
file = file.gsub bad_chars, ''
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)
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)
request = Net::HTTP::Get.new raw_uri.request_uri
data = https.request request
data = data.body
cache_gist(gist, file, data) unless @cache == false
cache gist, file, data unless @cache_disabled
data
end
end
@ -73,11 +85,10 @@ module Jekyll
class GistTagNoCache < GistTag
def initialize(tag_name, text, token)
super
@cache = false
@cache_disabled = true
end
end
end
Liquid::Template.register_tag('gist', Jekyll::GistTag)
Liquid::Template.register_tag('gistnocache', Jekyll::GistTagNoCache)

View File

@ -0,0 +1,40 @@
require 'pathname'
module Jekyll
class IncludeCodeTag < Liquid::Tag
def initialize(tag_name, file, tokens)
super
@file = file.strip
end
def render(context)
code_dir = (context.registers[:site].config['code_dir'] || 'downloads/code')
code_path = (Pathname.new(context.registers[:site].source) + code_dir).expand_path
file = code_path + @file
if File.symlink?(code_path)
return "Code directory '#{code_path}' cannot be a symlink"
end
unless file.file?
return "File #{file} could not be found"
end
Dir.chdir(code_path) do
code = file.read
file_type = file.extname
url = "#{context.registers[:site].config['url']}/#{code_dir}/#{@file}"
source = "<figure><figcaption><span>#{file.basename}</span><a href='#{url}'>download</a></figcaption>\n"
source += "{% highlight #{file_type} %}\n" + code + "\n{% endhighlight %}</figure>"
partial = Liquid::Template.parse(source)
context.stack do
partial.render(context)
end
end
end
end
end
Liquid::Template.register_tag('include_code', Jekyll::IncludeCodeTag)

View File

@ -6,7 +6,7 @@
require 'fileutils'
require 'digest/md5'
PYGMENTS_CACHE_DIR = File.expand_path('../../_cache', __FILE__)
PYGMENTS_CACHE_DIR = File.expand_path('../../_code_cache', __FILE__)
FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
Jekyll::HighlightBlock.class_eval do