Skip to content

Commit

Permalink
Added plugins/config.rb for reading and writing _config.yml. Added op…
Browse files Browse the repository at this point in the history
…tion to use Albino and default Python Pygments by setting pygments:true in _config.yml
  • Loading branch information
imathis committed Jun 15, 2012
1 parent f3978d1 commit 9af3642
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Rakefile
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion _config.yml
Expand Up @@ -36,7 +36,7 @@ code_dir: downloads/code
category_dir: blog/categories
category_title_prefix: "Category: "
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/
Expand Down
19 changes: 19 additions & 0 deletions 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
11 changes: 8 additions & 3 deletions 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'
Expand All @@ -9,13 +10,17 @@

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
Expand Down

0 comments on commit 9af3642

Please sign in to comment.