added support for pygments.rb removing dependency on pygments, added support for caching highlighted code from pygments.rb and added default line numbering. Javascript auto line numbering now only occurs for embedded gists

main
Brandon Mathis 14 years ago
parent 727a149227
commit 7b81aab5ac

@ -48,8 +48,7 @@ function testFeatures() {
function addCodeLineNumbers(){ function addCodeLineNumbers(){
if (navigator.appName == 'Microsoft Internet Explorer') { return } if (navigator.appName == 'Microsoft Internet Explorer') { return }
$('div.highlight pre code').each(function(el){ addDivLines(el); }); $('div.gist-highlight').each(function(code){
$('div.highlight, div.gist-highlight').each(function(code){
var tableStart = '<table cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter">'; var tableStart = '<table cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter">';
var lineNumbers = '<pre class="line-numbers">'; var lineNumbers = '<pre class="line-numbers">';
var tableMiddle = '</pre></td><td class="code" width="100%">'; var tableMiddle = '</pre></td><td class="code" width="100%">';
@ -62,16 +61,6 @@ function addCodeLineNumbers(){
$(code).html(table); $(code).html(table);
}); });
} }
function addDivLines(el){
var content = $(el).html();
var lines = content.replace(/\s*$/g, '').split(/\n/);
var count = lines.length;
$(lines).each(function(line, index){
if(line == '') line = ' ';
lines[index] = '<div class="line">' + line + '</div>';
});
$(el).html(lines.join(''));
}
function flashVideoFallback(){ function flashVideoFallback(){
var flashplayerlocation = "/assets/jwplayer/player.swf", var flashplayerlocation = "/assets/jwplayer/player.swf",

@ -4,6 +4,7 @@ gem 'rake'
gem 'rack' gem 'rack'
gem 'jekyll' gem 'jekyll'
gem 'rdiscount' gem 'rdiscount'
gem 'pygments.rb'
gem 'RedCloth' gem 'RedCloth'
gem 'haml', '>= 3.1' gem 'haml', '>= 3.1'
gem 'compass', '>= 0.11' gem 'compass', '>= 0.11'

@ -4,6 +4,7 @@ GEM
RedCloth (4.2.7) RedCloth (4.2.7)
albino (1.3.3) albino (1.3.3)
posix-spawn (>= 0.3.6) posix-spawn (>= 0.3.6)
blankslate (2.1.2.4)
chunky_png (1.2.0) chunky_png (1.2.0)
classifier (1.3.3) classifier (1.3.3)
fast-stemmer (>= 1.0.0) fast-stemmer (>= 1.0.0)
@ -13,6 +14,7 @@ GEM
sass (~> 3.1) sass (~> 3.1)
directory_watcher (1.4.0) directory_watcher (1.4.0)
fast-stemmer (1.0.0) fast-stemmer (1.0.0)
ffi (1.0.9)
fssm (0.2.7) fssm (0.2.7)
haml (3.1.2) haml (3.1.2)
jekyll (0.11.0) jekyll (0.11.0)
@ -27,12 +29,17 @@ GEM
maruku (0.6.0) maruku (0.6.0)
syntax (>= 1.0.0) syntax (>= 1.0.0)
posix-spawn (0.3.6) posix-spawn (0.3.6)
rack (1.3.1) pygments.rb (0.1.2)
rubypython (>= 0.5.1)
rack (1.3.2)
rake (0.9.2) rake (0.9.2)
rb-fsevent (0.4.1) rb-fsevent (0.4.1)
rdiscount (1.6.8) rdiscount (1.6.8)
rubypants (0.2.0) rubypants (0.2.0)
sass (3.1.4) rubypython (0.5.1)
blankslate (>= 2.1.2.3)
ffi (~> 1.0.7)
sass (3.1.5)
syntax (1.0.0) syntax (1.0.0)
PLATFORMS PLATFORMS
@ -43,6 +50,7 @@ DEPENDENCIES
compass (>= 0.11) compass (>= 0.11)
haml (>= 3.1) haml (>= 3.1)
jekyll jekyll
pygments.rb
rack rack
rake rake
rb-fsevent rb-fsevent

@ -28,7 +28,7 @@ plugins: plugins
code_dir: downloads/code code_dir: downloads/code
category_dir: blog/categories category_dir: blog/categories
markdown: rdiscount markdown: rdiscount
pygments: true pygments: false
paginate: 10 # Posts per page on the blog index paginate: 10 # Posts per page on the blog index
recent_posts: 5 # Posts in the sidebar Recent Posts section recent_posts: 5 # Posts in the sidebar Recent Posts section

@ -41,9 +41,12 @@
# <pre><code>&lt;sarcasm> Ooooh, sarcasm... How original!&lt;/sarcasm></code></pre> # <pre><code>&lt;sarcasm> Ooooh, sarcasm... How original!&lt;/sarcasm></code></pre>
# </figure> # </figure>
# #
require './plugins/pygments_code'
module Jekyll module Jekyll
class CodeBlock < Liquid::Block class CodeBlock < Liquid::Block
include HighlightCode
CaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)\s+(.+)/i CaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)\s+(.+)/i
CaptionUrl = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)/i CaptionUrl = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)/i
Caption = /(\S[\S\s]*)/ Caption = /(\S[\S\s]*)/
@ -75,7 +78,7 @@ module Jekyll
if @filetype if @filetype
@filetype = 'objc' if @filetype == 'm' @filetype = 'objc' if @filetype == 'm'
@filetype = 'perl' if @filetype == 'pl' @filetype = 'perl' if @filetype == 'pl'
source += "{% highlight #{@filetype} %}\n" + code + "\n{% endhighlight %}</figure></div>" source += " #{highlight(code, @filetype)}</figure></div>"
else else
source += "<pre><code>" + code.lstrip.rstrip.gsub(/</,'&lt;') + "</code></pre></figure></div>" source += "<pre><code>" + code.lstrip.rstrip.gsub(/</,'&lt;') + "</code></pre></figure></div>"
end end

@ -20,11 +20,13 @@
# will output a figcaption with the title: Example 2 (test.js) # will output a figcaption with the title: Example 2 (test.js)
# #
require './plugins/pygments_code'
require 'pathname' require 'pathname'
module Jekyll module Jekyll
class IncludeCodeTag < Liquid::Tag class IncludeCodeTag < Liquid::Tag
include HighlightCode
def initialize(tag_name, markup, tokens) def initialize(tag_name, markup, tokens)
@title = nil @title = nil
@file = nil @file = nil
@ -50,13 +52,13 @@ module Jekyll
Dir.chdir(code_path) do Dir.chdir(code_path) do
code = file.read code = file.read
@filetype = file.extname @filetype = file.extname.sub('.','')
@filetype = 'objc' if @filetype == 'm' @filetype = 'objc' if @filetype == 'm'
@filetype = 'perl' if @filetype == 'pl' @filetype = 'perl' if @filetype == 'pl'
title = @title ? "#{@title} (#{file.basename})" : file.basename title = @title ? "#{@title} (#{file.basename})" : file.basename
url = "#{context.registers[:site].config['url']}/#{code_dir}/#{@file}" url = "#{context.registers[:site].config['url']}/#{code_dir}/#{@file}"
source = "<div><figure role=code><figcaption><span>#{title}</span> <a href='#{url}'>download</a></figcaption>\n" source = "<div><figure role=code><figcaption><span>#{title}</span> <a href='#{url}'>download</a></figcaption>\n"
source += "{% highlight #{@filetype} %}\n" + code + "\n{% endhighlight %}</figure></div>" source += " #{highlight(code, @filetype)}</figure></div>"
partial = Liquid::Template.parse(source) partial = Liquid::Template.parse(source)
context.stack do context.stack do
partial.render(context) partial.render(context)

@ -1,30 +0,0 @@
#
# Author: Raimonds Simanovskis, http://blog.rayapps.com/
# Source URL: https://github.com/rsim/blog.rayapps.com/blob/master/_plugins/pygments_cache_patch.rb
#
require 'fileutils'
require 'digest/md5'
PYGMENTS_CACHE_DIR = File.expand_path('../../_code_cache', __FILE__)
FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
Jekyll::HighlightBlock.class_eval do
def render_pygments(context, code)
if defined?(PYGMENTS_CACHE_DIR)
path = File.join(PYGMENTS_CACHE_DIR, "#{@lang}-#{Digest::MD5.hexdigest(code)}.html")
if File.exist?(path)
highlighted_code = File.read(path)
else
highlighted_code = Albino.new(code, @lang).to_s(@options)
File.open(path, 'w') {|f| f.print(highlighted_code) }
end
else
highlighted_code = Albino.new(code, @lang).to_s(@options)
end
output = add_code_tags(highlighted_code, @lang)
output = context["pygments_prefix"] + output if context["pygments_prefix"]
output = output + context["pygments_suffix"] if context["pygments_suffix"]
output
end
end

@ -0,0 +1,34 @@
require 'pygments'
require 'fileutils'
require 'digest/md5'
PYGMENTS_CACHE_DIR = File.expand_path('../../_code_cache', __FILE__)
FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
module HighlightCode
def highlight(str, lang)
str = pygments(str, lang).match(/<pre>(.+)<\/pre>/m)[1].to_s.gsub(/\s*$/, '') #strip out divs <div class="highlight">
table = '<div class="highlight"><table cellpadding="0" cellspacing="0"><tr><td class="gutter"><pre class="line-numbers">'
code = ''
str.lines.each_with_index do |line,index|
table += "<span class='line'>#{index+1}</span>\n"
code += "<div class='line'>#{line}</div>"
end
table += "</pre></td><td class='code' width='100%'><pre><code class='#{lang}'>#{code}</code></pre></td></tr></table></div>"
end
def pygments(code, lang)
if defined?(PYGMENTS_CACHE_DIR)
path = File.join(PYGMENTS_CACHE_DIR, "#{lang}-#{Digest::MD5.hexdigest(code)}.html")
if File.exist?(path)
highlighted_code = File.read(path)
else
highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html')
File.open(path, 'w') {|f| f.print(highlighted_code) }
end
else
highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html')
end
highlighted_code
end
end
Loading…
Cancel
Save