Skip to content

Commit

Permalink
Option to cache Pygments output for faster re-rendering.
Browse files Browse the repository at this point in the history
Re-rendering ~200 posts with highlighting in many of them went from ~90 seconds to ~3 seconds with caching.
  • Loading branch information
henrik committed Apr 13, 2009
1 parent 5ec47ed commit e4c7dcf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.textile
Expand Up @@ -141,6 +141,12 @@ use that to make your code blocks look pretty. To activate Pygments support
during the conversion:

$ jekyll --pygments

Highlighting a blog post can take a second or two, and with a lot of posts,
it adds up. You can tell Jekyll to cache pygments output in a directory for
faster re-rendering:

$ jekyll --pygments-cache [PATH]

h3. Markdown Processor

Expand Down
4 changes: 4 additions & 0 deletions bin/jekyll
Expand Up @@ -43,6 +43,10 @@ opts = OptionParser.new do |opts|
opts.on("--pygments", "Use pygments to highlight code") do
options['pygments'] = true
end

opts.on("--pygments-cache", "Path to cache pygments output in, for faster re-rendering") do |path|
options['pygments_cache'] = path
end

opts.on("--rdiscount", "Use rdiscount gem for Markdown") do
options['markdown'] = 'rdiscount'
Expand Down
9 changes: 8 additions & 1 deletion lib/jekyll/site.rb
Expand Up @@ -2,7 +2,7 @@ module Jekyll

class Site
attr_accessor :config, :layouts, :posts, :collated_posts, :categories
attr_accessor :source, :dest, :lsi, :pygments, :permalink_style, :permalink_date,
attr_accessor :source, :dest, :lsi, :pygments, :pygments_cache, :permalink_style, :permalink_date,
:sass, :post_defaults

# Initialize the site
Expand All @@ -16,6 +16,7 @@ def initialize(config)
self.dest = config['destination']
self.lsi = config['lsi']
self.pygments = config['pygments']
self.pygments_cache = config['pygments_cache']
self.permalink_style = config['permalink'].to_sym
self.permalink_date = config['permalink_date'] && config['permalink_date'].sub(%r{\A/?(.*)/?\Z}, '/\1/')
self.post_defaults = config['post_defaults'] || {}
Expand Down Expand Up @@ -55,6 +56,12 @@ def setup
puts 'You must have the haml gem installed first'
end
end

if self.pygments_cache
require 'fileutils'
FileUtils.mkdir_p(pygments_cache)
require 'digest/md5'
end

# Set the Markdown interpreter (and Maruku self.config, if necessary)
case self.config['markdown']
Expand Down
18 changes: 15 additions & 3 deletions lib/jekyll/tags/highlight.rb
Expand Up @@ -30,12 +30,24 @@ def render(context)
end

def render_pygments(context, code)
if cache_dir = context.registers[:site].pygments_cache
path = File.join(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

if context["content_type"] == :markdown
return "\n" + Albino.new(code, @lang).to_s(@options) + "\n"
return "\n" + highlighted_code + "\n"
elsif context["content_type"] == :textile
return "<notextile>" + Albino.new(code, @lang).to_s(@options) + "</notextile>"
return "<notextile>" + highlighted_code + "</notextile>"
else
return Albino.new(code, @lang).to_s(@options)
return highlighted_code
end
end

Expand Down

0 comments on commit e4c7dcf

Please sign in to comment.