Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Option to cache Pygments output for faster re-rendering.

Re-rendering ~200 posts with highlighting in many of them went from ~90 seconds to ~3 seconds with caching.
  • Loading branch information...
commit e4c7dcf6c3e12dad60022c1a9a8faa29d301107a 1 parent 5ec47ed
@henrik henrik authored
View
6 README.textile
@@ -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
View
4 bin/jekyll
@@ -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'
View
9 lib/jekyll/site.rb
@@ -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
@@ -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'] || {}
@@ -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']
View
18 lib/jekyll/tags/highlight.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.