2012-05-27 03:30:17 -04:00
|
|
|
#require 'albino'
|
|
|
|
require './plugins/raw'
|
2011-07-26 23:36:42 -04:00
|
|
|
require 'pygments'
|
|
|
|
require 'fileutils'
|
|
|
|
require 'digest/md5'
|
|
|
|
|
2011-09-18 07:45:25 -04:00
|
|
|
PYGMENTS_CACHE_DIR = File.expand_path('../../.pygments-cache', __FILE__)
|
2011-07-26 23:36:42 -04:00
|
|
|
FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
|
|
|
|
|
|
|
|
module HighlightCode
|
2012-05-27 03:30:17 -04:00
|
|
|
include TemplateWrapper
|
2011-07-26 23:36:42 -04:00
|
|
|
def pygments(code, lang)
|
2012-05-24 11:18:44 -04:00
|
|
|
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)
|
2011-07-26 23:36:42 -04:00
|
|
|
else
|
2012-05-27 03:30:17 -04:00
|
|
|
#highlighted_code = Albino.new(code, lang, :html)
|
|
|
|
highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'})
|
2012-05-24 11:18:44 -04:00
|
|
|
File.open(path, 'w') {|f| f.print(highlighted_code) } if path
|
2011-07-26 23:36:42 -04:00
|
|
|
end
|
2012-05-27 03:30:17 -04:00
|
|
|
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'
|
2012-05-28 05:29:18 -04:00
|
|
|
lang = 'plain' if lang == '' or lang.nil? or !lang
|
2012-05-27 03:30:17 -04:00
|
|
|
|
2012-05-28 05:29:18 -04:00
|
|
|
caption = options[:caption] || nil
|
|
|
|
url = options[:url] || nil
|
|
|
|
anchor = options[:anchor] || nil
|
|
|
|
wrap = options[:wrap] || true
|
|
|
|
marks = options[:marks]
|
2012-05-27 03:30:17 -04:00
|
|
|
linenos = options[:linenos]
|
|
|
|
start = options[:start]
|
|
|
|
|
|
|
|
if lang == 'plain'
|
|
|
|
# Escape html tags
|
|
|
|
code = code.gsub('<','<').gsub('>','>')
|
|
|
|
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
|
|
|
|
|
2012-05-28 05:29:18 -04:00
|
|
|
code = tableize_code(code, lang, { linenos: linenos, start: start, marks: marks })
|
2012-05-27 03:30:17 -04:00
|
|
|
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]
|
2012-05-28 05:29:18 -04:00
|
|
|
marks = options[:marks] || []
|
2012-05-27 03:30:17 -04:00
|
|
|
table = "<div class='highlight'><table>"
|
2012-05-28 05:29:18 -04:00
|
|
|
table += number_lines(start, code.lines.count, marks) if lines
|
2012-05-27 03:30:17 -04:00
|
|
|
table += "<td class='code'><pre><code class='#{lang}'>"
|
2012-05-28 05:29:18 -04:00
|
|
|
if marks.size
|
|
|
|
code.lines.each_with_index do |line,index|
|
|
|
|
table += "<span class='line#{' marked' if marks.include? index + start}'>#{line}</span>"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
table += code.gsub /^((.+)?(\n?))/, '<span class=\'line\'>\1</span>'
|
|
|
|
end
|
2012-05-27 03:30:17 -04:00
|
|
|
table +="</code></pre></td></tr></table></div>"
|
2011-07-26 23:36:42 -04:00
|
|
|
end
|
2012-05-24 11:18:44 -04:00
|
|
|
|
2012-05-28 05:29:18 -04:00
|
|
|
def number_lines (start, count, marks)
|
2012-05-27 03:30:17 -04:00
|
|
|
start ||= 1
|
|
|
|
lines = "<td class='gutter'><pre class='line-numbers'>"
|
|
|
|
count.times do |index|
|
2012-05-28 05:29:18 -04:00
|
|
|
lines += "<span class='line-number#{' marked' if marks.include? index + start}'>#{index + start}</span>\n"
|
2011-08-16 02:40:47 -04:00
|
|
|
end
|
2012-05-27 03:30:17 -04:00
|
|
|
lines += "</pre></td>"
|
2011-08-16 02:40:47 -04:00
|
|
|
end
|
2012-05-28 05:29:18 -04:00
|
|
|
|
|
|
|
def get_lang (input)
|
|
|
|
lang = nil
|
|
|
|
if input =~ /\s*lang:(\w+)/i
|
|
|
|
lang = $1
|
|
|
|
end
|
|
|
|
lang
|
|
|
|
end
|
|
|
|
|
|
|
|
def replace_lang (input)
|
|
|
|
input.sub /\s*lang:\w+/i, ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_marks (input)
|
|
|
|
# Matches pattern for line marks and returns array of line numbers to mark
|
|
|
|
# Example input mark:1,5-10,2
|
|
|
|
# Outputs: [1,2,5,6,7,8,9,10]
|
|
|
|
marks = []
|
|
|
|
if input =~ /\s*mark:(\d\S*)/i
|
|
|
|
marks = $1.gsub /(\d+)-(\d+)/ do
|
|
|
|
($1.to_i..$2.to_i).to_a.join(',')
|
|
|
|
end
|
|
|
|
marks = marks.split(',').collect {|s| s.to_i}.sort
|
|
|
|
end
|
|
|
|
marks
|
|
|
|
end
|
|
|
|
|
|
|
|
def replace_marks (input)
|
|
|
|
input.sub(/\s*mark:\d\S*/i,'')
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_linenos (input)
|
|
|
|
linenos = true
|
|
|
|
if input =~ /\s*linenos:false/i
|
|
|
|
linenos = false
|
|
|
|
end
|
|
|
|
linenos
|
|
|
|
end
|
|
|
|
|
|
|
|
def replace_linenos (input)
|
|
|
|
input.sub(/\s*linenos:false/i,'')
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_start (input)
|
|
|
|
start = 1
|
|
|
|
if input =~ /\s*start:(\d+)/i
|
|
|
|
start = $1.to_i
|
|
|
|
end
|
|
|
|
start
|
|
|
|
end
|
|
|
|
|
|
|
|
def replace_start (input)
|
|
|
|
input.sub(/\s*start:\d+/i,'')
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_end (input)
|
|
|
|
endline = nil
|
|
|
|
if input =~ /\s*end:(\d+)/i
|
|
|
|
endline = $1.to_i
|
|
|
|
end
|
|
|
|
endline
|
|
|
|
end
|
|
|
|
|
|
|
|
def replace_end (input)
|
|
|
|
input.sub(/\s*end:\d+/i,'')
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_range (input, start, endline)
|
|
|
|
if input =~ /\s*range:(\d+),(\d+)/i
|
|
|
|
start = $1.to_i
|
|
|
|
eneline = $2.to_i
|
|
|
|
end
|
|
|
|
{start: start, end: endline}
|
|
|
|
end
|
|
|
|
def replace_range (input)
|
|
|
|
input.sub(/\s*range:\d+,\d+/i,'')
|
|
|
|
end
|
2011-07-26 23:36:42 -04:00
|
|
|
end
|