require 'pygments' require 'fileutils' require 'digest/md5' PYGMENTS_CACHE_DIR = File.expand_path('../../.pygments-cache', __FILE__) FileUtils.mkdir_p(PYGMENTS_CACHE_DIR) module HighlightCode def highlight(str, lang) lang = 'ruby' if lang == 'ru' lang = 'objc' if lang == 'm' lang = 'perl' if lang == 'pl' lang = 'yaml' if lang == 'yml' str = pygments(str, lang).match(/
(.+)<\/pre>/m)[1].to_s.gsub(/ *$/, '') #strip out divs 
tableize_code(str, lang) end def pygments(code, lang) 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 begin highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'}) rescue MentosError raise "Pygments can't parse unknown language: #{lang}." end File.open(path, 'w') {|f| f.print(highlighted_code) } end else highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'}) end highlighted_code end def tableize_code (str, lang = '') table = '
'
    code = ''
    str.lines.each_with_index do |line,index|
      table += "#{index+1}\n"
      code  += "#{line}"
    end
    table += "
#{code}
" end end