From 28e99b3c38adbe8fdeee7c80ad3f0fc5ac3507a0 Mon Sep 17 00:00:00 2001 From: Brandon Mathis Date: Fri, 15 Jun 2012 12:50:34 -0500 Subject: [PATCH] Added plugins/config.rb for reading and writing _config.yml. Added option to use Albino and default Python Pygments by setting pygments:true in _config.yml --- Rakefile | 5 +++-- _config.yml | 2 +- plugins/config.rb | 19 +++++++++++++++++++ plugins/pygments_code.rb | 11 ++++++++--- 4 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 plugins/config.rb diff --git a/Rakefile b/Rakefile index 11a778b..2fa9106 100644 --- a/Rakefile +++ b/Rakefile @@ -192,8 +192,9 @@ end desc "Clean out caches: .pygments-cache, .gist-cache, .sass-cache" task :clean do - [".pygments-cache/**", ".gist-cache/**", ".sass-cache/**"].each { |dir| rm_rf Dir.glob(dir) } - rm "source/stylesheets/screen.css" + [".pygments-cache/**", ".gist-cache/**"].each { |dir| rm_rf Dir.glob(dir) } + rm "#{source_dir}/stylesheets/screen.css" if File.exists?("#{source_dir}/stylesheets/screen.css") + system "compass clean" puts "## Cleaned Sass, Pygments and Gist caches, removed generated stylesheets ##" end diff --git a/_config.yml b/_config.yml index 9595004..cc65879 100644 --- a/_config.yml +++ b/_config.yml @@ -35,7 +35,7 @@ plugins: plugins code_dir: downloads/code category_dir: blog/categories markdown: rdiscount -pygments: false # default python pygments have been replaced by pygments.rb +pygments: false # Jekyll's default Python Pygments have been replaced by pygments.rb. Set to true to use Albino + Pythong Pygments paginate: 10 # Posts per page on the blog index pagination_dir: blog # Directory base for pagination URLs eg. /blog/page/2/ diff --git a/plugins/config.rb b/plugins/config.rb new file mode 100644 index 0000000..c73aaac --- /dev/null +++ b/plugins/config.rb @@ -0,0 +1,19 @@ +# read and write to the _config.yml +require 'yaml' + +module SiteConfig + ConfigFile = File.expand_path "../_config.yml", File.dirname(__FILE__) + + def get_config (key) + config_data = YAML::load(File.open(ConfigFile)) + config_data[key] + end + def set_config (key, val) + old_val = get_config(key) + config = IO.read(ConfigFile) + config.sub!(/#{key}:\s*#{old_val}/, "#{key}: #{val}") + File.open(ConfigFile, 'w') do |f| + f.write config + end + end +end diff --git a/plugins/pygments_code.rb b/plugins/pygments_code.rb index ca6f6d6..45623d0 100644 --- a/plugins/pygments_code.rb +++ b/plugins/pygments_code.rb @@ -1,5 +1,6 @@ -#require 'albino' require './plugins/raw' +require './plugins/config' +require 'albino' require 'pygments' require 'fileutils' require 'digest/md5' @@ -9,13 +10,17 @@ FileUtils.mkdir_p(PYGMENTS_CACHE_DIR) 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) else - #highlighted_code = Albino.new(code, lang, :html) - highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'}) + 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 File.open(path, 'w') {|f| f.print(highlighted_code) } if path end highlighted_code.to_s