- tableize_code(str, lang)
- end
-
+ 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 = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'})
+ #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
+ highlighted_code.to_s
+ rescue
+ puts $!,$@
end
- def tableize_code (str, lang = '')
- table = '
'
- code = ''
- str.lines.each_with_index do |line,index|
- table += "#{index+1}\n"
- code += "#{line}"
+ 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('<','<').gsub('>','>')
+ elsif lang.include? "-raw"
+ output = "``` #{$1.sub('-raw', '')}\n"
+ output += code
+ output += "\n```\n"
+ else
+ code = pygments(code, lang).match(/(.+)<\/pre>/m)[1].gsub(/ *$/, '') #strip out divs
end
- table += " | #{code}
|
"
+
+ code = tableize_code(code, lang, { linenos: linenos, start: start })
+ caption = captionize(caption, url, anchor) if caption
+
+ figure = "
"
+ figure = safe_wrap(figure) if wrap
+ figure
+ end
+
+ def captionize (caption, url, anchor)
+ figcaption = "
#{caption}"
+ figcaption += " #{anchor || 'link'}" if url
+ figcaption += ""
+ end
+
+ def tableize_code (code, lang, options = {})
+ start = options[:start]
+ lines = options[:linenos] || true
+ table = "
"
+ table += number_lines(start, code.lines.count) if lines
+ table += ""
+ table += code.gsub /^((.+)?(\n?))/, '\1'
+ table +="
|
"
+ end
+
+ def number_lines (start, count)
+ start ||= 1
+ lines = "
"
+ count.times do |index|
+ lines += "#{index + start}\n"
+ end
+ lines += " | "
end
end