android.moparisthebest.org/plugins/pygments_code.rb
Brandon Mathis e16844af29 Improvements to Code embedding features
1. Removed lots of duplication
2. Added warnings and fixed some dim regex
3. New: set a start line number for any code snippet
4. New: set a start, end, or range for include_code
5. New: added option to disable line numbers linenos:false

Fixes #478 #484
2012-05-27 02:30:17 -05:00

85 lines
2.8 KiB
Ruby

#require 'albino'
require './plugins/raw'
require 'pygments'
require 'fileutils'
require 'digest/md5'
PYGMENTS_CACHE_DIR = File.expand_path('../../.pygments-cache', __FILE__)
FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
module HighlightCode
include TemplateWrapper
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'})
File.open(path, 'w') {|f| f.print(highlighted_code) } if path
end
highlighted_code.to_s
rescue
puts $!,$@
end
def highlight(code, lang, options = {})
lang = 'ruby' if lang == 'ru'
lang = 'objc' if lang == 'm'
lang = 'perl' if lang == 'pl'
lang = 'yaml' if lang == 'yml'
lang = 'coffeescript' if lang == 'coffee'
lang = 'plain' if lang == '' or lang.nil?
caption = options[:caption] || nil
url = options[:url] || nil
anchor = options[:anchor] || nil
wrap = options[:wrap] || true
linenos = options[:linenos]
start = options[:start]
if lang == 'plain'
# Escape html tags
code = code.gsub('<','&lt;').gsub('>','&gt;')
elsif lang.include? "-raw"
output = "``` #{$1.sub('-raw', '')}\n"
output += code
output += "\n```\n"
else
code = pygments(code, lang).match(/<pre>(.+)<\/pre>/m)[1].gsub(/ *$/, '') #strip out divs <div class="highlight">
end
code = tableize_code(code, lang, { linenos: linenos, start: start })
caption = captionize(caption, url, anchor) if caption
figure = "<figure class='code'>#{caption}#{code}</figure>"
figure = safe_wrap(figure) if wrap
figure
end
def captionize (caption, url, anchor)
figcaption = "<figcaption><span>#{caption}</span>"
figcaption += "<a href='#{url}' title='Download code'> #{anchor || 'link'}</a>" if url
figcaption += "</figcaption>"
end
def tableize_code (code, lang, options = {})
start = options[:start]
lines = options[:linenos].nil? ? true : options[:linenos]
table = "<div class='highlight'><table>"
table += number_lines(start, code.lines.count) if lines
table += "<td class='code'><pre><code class='#{lang}'>"
table += code.gsub /^((.+)?(\n?))/, '<span class=\'line\'>\1</span>'
table +="</code></pre></td></tr></table></div>"
end
def number_lines (start, count)
start ||= 1
lines = "<td class='gutter'><pre class='line-numbers'>"
count.times do |index|
lines += "<span class='line-number'>#{index + start}</span>\n"
end
lines += "</pre></td>"
end
end