From 7b81aab5ac225fb12a100d9eb463340f322704fd Mon Sep 17 00:00:00 2001 From: Brandon Mathis Date: Tue, 26 Jul 2011 23:36:42 -0400 Subject: 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 --- plugins/code_block.rb | 5 ++++- plugins/include_code.rb | 6 ++++-- plugins/pygments_cache_patch.rb | 30 ------------------------------ plugins/pygments_code.rb | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 33 deletions(-) delete mode 100644 plugins/pygments_cache_patch.rb create mode 100644 plugins/pygments_code.rb (limited to 'plugins') diff --git a/plugins/code_block.rb b/plugins/code_block.rb index 4cf2817b..af64e031 100644 --- a/plugins/code_block.rb +++ b/plugins/code_block.rb @@ -41,9 +41,12 @@ #
<sarcasm> Ooooh, sarcasm... How original!</sarcasm>
# # +require './plugins/pygments_code' + module Jekyll class CodeBlock < Liquid::Block + include HighlightCode CaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)\s+(.+)/i CaptionUrl = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)/i Caption = /(\S[\S\s]*)/ @@ -75,7 +78,7 @@ module Jekyll if @filetype @filetype = 'objc' if @filetype == 'm' @filetype = 'perl' if @filetype == 'pl' - source += "{% highlight #{@filetype} %}\n" + code + "\n{% endhighlight %}" + source += " #{highlight(code, @filetype)}" else source += "
" + code.lstrip.rstrip.gsub(/
" end diff --git a/plugins/include_code.rb b/plugins/include_code.rb index b0258a4c..93db78a3 100644 --- a/plugins/include_code.rb +++ b/plugins/include_code.rb @@ -20,11 +20,13 @@ # will output a figcaption with the title: Example 2 (test.js) # +require './plugins/pygments_code' require 'pathname' module Jekyll class IncludeCodeTag < Liquid::Tag + include HighlightCode def initialize(tag_name, markup, tokens) @title = nil @file = nil @@ -50,13 +52,13 @@ module Jekyll Dir.chdir(code_path) do code = file.read - @filetype = file.extname + @filetype = file.extname.sub('.','') @filetype = 'objc' if @filetype == 'm' @filetype = 'perl' if @filetype == 'pl' title = @title ? "#{@title} (#{file.basename})" : file.basename url = "#{context.registers[:site].config['url']}/#{code_dir}/#{@file}" source = "
#{title} download
\n" - source += "{% highlight #{@filetype} %}\n" + code + "\n{% endhighlight %}
" + source += " #{highlight(code, @filetype)}" partial = Liquid::Template.parse(source) context.stack do partial.render(context) diff --git a/plugins/pygments_cache_patch.rb b/plugins/pygments_cache_patch.rb deleted file mode 100644 index 09c09840..00000000 --- a/plugins/pygments_cache_patch.rb +++ /dev/null @@ -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 diff --git a/plugins/pygments_code.rb b/plugins/pygments_code.rb new file mode 100644 index 00000000..05b4fb57 --- /dev/null +++ b/plugins/pygments_code.rb @@ -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>/m)[1].to_s.gsub(/\s*$/, '') #strip out divs 
+ table = '
'
+    code = ''
+    str.lines.each_with_index do |line,index|
+      table += "#{index+1}\n"
+      code  += "
#{line}
" + end + table += "
#{code}
" + 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 -- cgit v1.2.1