aboutsummaryrefslogtreecommitdiff
path: root/.themes/classic/plugins/pygments_cache_patch.rb
diff options
context:
space:
mode:
authorBrandon Mathis <brandon@imathis.com>2011-07-11 11:17:15 -0400
committerBrandon Mathis <brandon@imathis.com>2011-07-11 11:17:15 -0400
commitee7b9dd9581a048cee17e89181cba94254033fb2 (patch)
tree42c7d47ebfcaf9faf68a302a39a4815ef2e8f5ec /.themes/classic/plugins/pygments_cache_patch.rb
parentd4139e394ec9f70acfcb35f25340bdb541d0d7ee (diff)
downloadmy_new_personal_website-ee7b9dd9581a048cee17e89181cba94254033fb2.tar.xz
my_new_personal_website-ee7b9dd9581a048cee17e89181cba94254033fb2.zip
Changed _plugins folder to plugins and updated rake tasks accordingly
Diffstat (limited to '.themes/classic/plugins/pygments_cache_patch.rb')
-rw-r--r--.themes/classic/plugins/pygments_cache_patch.rb30
1 files changed, 30 insertions, 0 deletions
diff --git a/.themes/classic/plugins/pygments_cache_patch.rb b/.themes/classic/plugins/pygments_cache_patch.rb
new file mode 100644
index 00000000..09c09840
--- /dev/null
+++ b/.themes/classic/plugins/pygments_cache_patch.rb
@@ -0,0 +1,30 @@
+#
+# 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