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 .bundle
.DS_Store .DS_Store
.sass-cache .sass-cache
.gist_cache _gist_cache
_cache _code_cache
_assets _assets
public public
source/_stash source/_stash

View File

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

View File

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

View File

@ -1,9 +1,11 @@
# Nicked from Brandon Tilly # A Liquid tag for Jekyll sites that allows embedding Gists and showing code for non-JavaScript enabled browsers and readers.
# Gist https://gist.github.com/803483 # by: Brandon Tilly
# Source URL: https://gist.github.com/1027674
# Post http://brandontilley.com/2011/01/31/gist-tag-for-jekyll.html # 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 'digest/md5'
require 'net/https' require 'net/https'
require 'uri' require 'uri'
@ -12,60 +14,70 @@ module Jekyll
class GistTag < Liquid::Tag class GistTag < Liquid::Tag
def initialize(tag_name, text, token) def initialize(tag_name, text, token)
super super
system('mkdir -p .gist_cache')
@text = text @text = text
@cache = true @cache_disabled = false
@cache_folder = File.expand_path "../.gist_cache", File.dirname(__FILE__) @cache_folder = File.expand_path "../_gist_cache", File.dirname(__FILE__)
FileUtils.mkdir_p @cache_folder
end end
def render(context) def render(context)
return "" unless @text =~ /([\d]*) (.*)/ if parts = @text.match(/([\d]*) (.*)/)
gist, file = parts[1].strip, parts[2].strip
gist, file = $1.strip, $2.strip script_url = script_url_for gist, file
script_url = "https://gist.github.com/#{gist}.js?file=#{file}"
code = get_cached_gist(gist, file) || get_gist_from_web(gist, file) code = get_cached_gist(gist, file) || get_gist_from_web(gist, file)
code = code.gsub "<", "&lt;" html_output_for script_url, code
string = "<script src='#{script_url}'></script>" else
string += "<noscript><pre><code>#{code}</code></pre></noscript>" ""
return string 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 end
def get_gist_url_for(gist, file) def get_gist_url_for(gist, file)
"https://gist.github.com/raw/#{gist}/#{file}" "https://raw.github.com/gist/#{gist}/#{file}"
end end
def cache_gist(gist, file, data) def cache(gist, file, data)
file = get_cache_file_for gist, file cache_file = get_cache_file_for gist, file
File.open(file, "w+") do |f| File.open(cache_file, "w") do |io|
f.write(data) io.write data
end end
end end
def get_cached_gist(gist, file) def get_cached_gist(gist, file)
return nil if @cache == false return nil if @cache_disabled
file = get_cache_file_for gist, file cache_file = get_cache_file_for gist, file
return nil unless File.exist?(file) File.read cache_file if File.exist? cache_file
return File.new(file).readlines.join
end end
def get_cache_file_for(gist, file) def get_cache_file_for(gist, file)
gist.gsub! /[^a-zA-Z0-9\-_\.]/, '' bad_chars = /[^a-zA-Z0-9\-_.]/
file.gsub! /[^a-zA-Z0-9\-_\.]/, '' gist = gist.gsub bad_chars, ''
file = file.gsub bad_chars, ''
md5 = Digest::MD5.hexdigest "#{gist}-#{file}" md5 = Digest::MD5.hexdigest "#{gist}-#{file}"
File.join @cache_folder, "#{gist}-#{file}-#{md5}.cache" File.join @cache_folder, "#{gist}-#{file}-#{md5}.cache"
end end
def get_gist_from_web(gist, file) def get_gist_from_web(gist, file)
gist_url = get_gist_url_for(gist, file) gist_url = get_gist_url_for gist, file
raw_uri = URI.parse(gist_url) raw_uri = URI.parse gist_url
https = Net::HTTP.new(raw_uri.host, raw_uri.port) https = Net::HTTP.new raw_uri.host, raw_uri.port
https.use_ssl = true https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE https.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(raw_uri.request_uri) request = Net::HTTP::Get.new raw_uri.request_uri
data = https.request(request) data = https.request request
data = data.body data = data.body
cache_gist(gist, file, data) unless @cache == false cache gist, file, data unless @cache_disabled
data data
end end
end end
@ -73,11 +85,10 @@ module Jekyll
class GistTagNoCache < GistTag class GistTagNoCache < GistTag
def initialize(tag_name, text, token) def initialize(tag_name, text, token)
super super
@cache = false @cache_disabled = true
end end
end end
end end
Liquid::Template.register_tag('gist', Jekyll::GistTag) Liquid::Template.register_tag('gist', Jekyll::GistTag)
Liquid::Template.register_tag('gistnocache', Jekyll::GistTagNoCache) 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 'fileutils'
require 'digest/md5' 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) FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
Jekyll::HighlightBlock.class_eval do Jekyll::HighlightBlock.class_eval do