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

This commit is contained in:
Brandon Mathis 2012-06-15 12:50:34 -05:00
parent 0ce6655e02
commit 28e99b3c38
4 changed files with 31 additions and 6 deletions

View File

@ -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

View File

@ -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/

19
plugins/config.rb Normal file
View File

@ -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

View File

@ -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)
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