From ed186b18a23731f23c9f5e657251c24940a2642f Mon Sep 17 00:00:00 2001 From: Brandon Mathis Date: Mon, 24 Dec 2012 00:23:48 -0600 Subject: [PATCH] More code snippet refactoring - Each code snippet's html is now cached (before only pygments output was cached and html was being processed each time) - Caching MD5 is based on code snippets as well as options. - Fixed an issue where gist tags weren't specifying a default start line Note: The caching change shaved 800 milliseconds off of the render time for the docs. For code heavy sites this should be a nice change. --- plugins/gist_tag.rb | 3 ++- plugins/pygments_code.rb | 51 ++++++++++++++++++---------------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/plugins/gist_tag.rb b/plugins/gist_tag.rb index 1d1521e..bd3ed76 100644 --- a/plugins/gist_tag.rb +++ b/plugins/gist_tag.rb @@ -40,7 +40,8 @@ module Jekyll code = get_cached_gist(gist, file) || get_gist_from_web(gist, file) length = code.lines.count - @end ||= length + @start ||= 1 + @end ||= length return "#{file} is #{length} lines long, cannot begin at line #{@start}" if @start > length return "#{file} is #{length} lines long, cannot read beyond line #{@end}" if @end > length if @start > 1 or @end < length diff --git a/plugins/pygments_code.rb b/plugins/pygments_code.rb index dfcc083..4e0791e 100644 --- a/plugins/pygments_code.rb +++ b/plugins/pygments_code.rb @@ -12,18 +12,12 @@ module HighlightCode include TemplateWrapper include SiteConfig def pygments(code, lang) - path = File.join(PYGMENTS_CACHE_DIR, "#{lang}-#{Digest::MD5.hexdigest(code)}.html") if defined?(PYGMENTS_CACHE_DIR) - if File.exist?(path) - highlighted_code = File.read(path) + if get_config('pygments') + highlighted_code = Albino.new(code, lang, :html) else - if get_config('pygments') - highlighted_code = Albino.new(code, lang, :html) - else - highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'}) - end - highlighted_code = highlighted_code.gsub(/{{/, '{{').gsub(/{%/, '{%') - File.open(path, 'w') {|f| f.print(highlighted_code) } if path + highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'}) end + highlighted_code = highlighted_code.gsub(/{{/, '{{').gsub(/{%/, '{%') highlighted_code.to_s rescue puts $!,$@ @@ -46,23 +40,24 @@ module HighlightCode linenos = options[:linenos] start = options[:start] || 1 - if lang == 'plain' - # Escape html tags - code = code.gsub('<','<') - elsif lang.include? "-raw" - output = "``` #{$1.sub('-raw', '')}\n" - output += code - output += "\n```\n" + path = File.join(PYGMENTS_CACHE_DIR, "#{lang}-#{Digest::MD5.hexdigest(options.to_s + code)}.html") if defined?(PYGMENTS_CACHE_DIR) + + if File.exist?(path) + code = File.read(path) else - code = pygments(code, lang).match(/
(.+)<\/pre>/m)[1].gsub(/ *$/, '') #strip out divs 
+ if lang == 'plain' + # Escape html tags + code = code.gsub('<','<') + else + code = pygments(code, lang).match(/
(.+)<\/pre>/m)[1].gsub(/ *$/, '') #strip out divs 
+ end + code = tableize_code(code, lang, {linenos: linenos, start: start, marks: marks }) + title = captionize(title, url, link_text) if title + code = "
#{title}#{code}
" + File.open(path, 'w') {|f| f.print(code) } if path end - - code = tableize_code(code, lang, { linenos: linenos, start: start, marks: marks }) - title = captionize(title, url, link_text) if title - - figure = "
#{title}#{code}
" - figure = safe_wrap(figure) if wrap - figure + code = safe_wrap(code) if wrap + code end def captionize (caption, url, link_text) @@ -75,7 +70,7 @@ module HighlightCode start = options[:start] || 1 lines = options[:linenos] || true marks = options[:marks] || [] - table = "" + table = "
" table += number_lines(start, code.lines.count, marks) if lines table += "
"
     code.lines.each_with_index do |line,index|
@@ -88,7 +83,7 @@ module HighlightCode
       line = line.strip.empty? ? ' ' : line
       table += "
#{line}
" end - table +="
" + table +="
" end def number_lines (start, count, marks) @@ -112,7 +107,7 @@ module HighlightCode linenos = input.match(/\s*linenos:(\w+)/i) marks = get_marks(input) url = input.match(/\s*url:\s*(("(.+?)")|('(.+?)')|(\S+))/i) - link_text = input.match(/\s*link_text:\s*(("(.+?)")|('(.+?)')|(\S+))/i) + link_text = input.match(/\s*link[-_]text:\s*(("(.+?)")|('(.+?)')|(\S+))/i) start = input.match(/\s*start:(\d+)/i) endline = input.match(/\s*end:(\d+)/i)