2012-05-27 03:30:17 -04:00
|
|
|
require './plugins/raw'
|
2012-06-15 13:50:34 -04:00
|
|
|
require './plugins/config'
|
2011-07-26 23:36:42 -04:00
|
|
|
require 'pygments'
|
|
|
|
require 'fileutils'
|
|
|
|
require 'digest/md5'
|
2013-01-14 00:03:55 -05:00
|
|
|
begin # Make it easy for folks to use rubypython if they like
|
|
|
|
require 'rubypython'
|
|
|
|
rescue LoadError # rubypython is not installed
|
|
|
|
end
|
2013-03-09 18:24:52 -05:00
|
|
|
require File.expand_path('../../lib/colors.rb', __FILE__)
|
2011-07-26 23:36:42 -04:00
|
|
|
|
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
|
2012-06-15 13:50:34 -04:00
|
|
|
include SiteConfig
|
2011-07-26 23:36:42 -04:00
|
|
|
def pygments(code, lang)
|
2013-03-09 19:44:32 -05:00
|
|
|
highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'})
|
|
|
|
highlighted_code = highlighted_code.gsub(/{{/, '{{').gsub(/{%/, '{%')
|
|
|
|
highlighted_code.to_s
|
2012-05-27 03:30:17 -04:00
|
|
|
end
|
|
|
|
|
2012-12-24 16:59:48 -05:00
|
|
|
def highlight(code, options = {})
|
|
|
|
lang = options[:lang]
|
2012-05-27 03:30:17 -04:00
|
|
|
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-12-19 16:29:26 -05:00
|
|
|
lang = 'csharp' if lang == 'cs'
|
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-12-24 14:51:05 -05:00
|
|
|
url = options[:url] || nil
|
|
|
|
title = options[:title] || (url ? ' ' : nil)
|
|
|
|
link_text = options[:link_text] || nil
|
2013-01-03 12:36:51 -05:00
|
|
|
escape = options[:escape] || false
|
2012-12-24 14:51:05 -05:00
|
|
|
marks = options[:marks]
|
|
|
|
linenos = options[:linenos]
|
|
|
|
start = options[:start] || 1
|
|
|
|
no_cache = options[:no_cache] || false
|
|
|
|
cache_path = options[:cache_path] || nil
|
2012-05-27 03:30:17 -04:00
|
|
|
|
2012-12-24 14:51:05 -05:00
|
|
|
# Attempt to retrieve cached code
|
|
|
|
cache = nil
|
|
|
|
unless no_cache
|
|
|
|
path = cache_path || get_cache_path(PYGMENTS_CACHE_DIR, lang, options.to_s + code)
|
|
|
|
cache = read_cache(path)
|
|
|
|
end
|
2012-12-24 01:23:48 -05:00
|
|
|
|
2012-12-24 14:51:05 -05:00
|
|
|
unless cache
|
2012-12-24 01:23:48 -05:00
|
|
|
if lang == 'plain'
|
|
|
|
# Escape html tags
|
|
|
|
code = code.gsub('<','<')
|
|
|
|
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, marks: marks })
|
|
|
|
title = captionize(title, url, link_text) if title
|
|
|
|
code = "<figure class='code'>#{title}#{code}</figure>"
|
2013-01-03 12:36:51 -05:00
|
|
|
code = safe_wrap(code) if escape
|
2012-12-24 14:51:05 -05:00
|
|
|
File.open(path, 'w') {|f| f.print(code) } unless no_cache
|
2012-05-27 03:30:17 -04:00
|
|
|
end
|
2013-01-03 12:36:51 -05:00
|
|
|
cache || code
|
2012-05-27 03:30:17 -04:00
|
|
|
end
|
|
|
|
|
2012-12-24 14:51:05 -05:00
|
|
|
def read_cache (path)
|
2013-01-03 12:36:51 -05:00
|
|
|
File.exist?(path) ? File.read(path) : nil unless path.nil?
|
2012-12-24 14:51:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_cache_path (dir, name, str)
|
|
|
|
File.join(dir, "#{name}-#{Digest::MD5.hexdigest(str)}.html")
|
|
|
|
end
|
|
|
|
|
2012-12-23 01:38:01 -05:00
|
|
|
def captionize (caption, url, link_text)
|
2012-12-21 00:50:42 -05:00
|
|
|
figcaption = "<figcaption>#{caption}"
|
2012-12-23 01:38:01 -05:00
|
|
|
figcaption += "<a href='#{url}'>#{(link_text || 'link').strip}</a>" if url
|
2012-05-27 03:30:17 -04:00
|
|
|
figcaption += "</figcaption>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def tableize_code (code, lang, options = {})
|
2012-12-23 01:38:01 -05:00
|
|
|
start = options[:start] || 1
|
|
|
|
lines = options[:linenos] || true
|
|
|
|
marks = options[:marks] || []
|
2012-12-24 01:23:48 -05: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-12-23 01:38:01 -05:00
|
|
|
table += "<td class='main #{'unnumbered' unless lines} #{lang}'><pre>"
|
|
|
|
code.lines.each_with_index do |line,index|
|
|
|
|
classes = 'line'
|
|
|
|
if marks.include? index + start
|
|
|
|
classes += ' marked'
|
|
|
|
classes += ' start' unless marks.include? index - 1 + start
|
|
|
|
classes += ' end' unless marks.include? index + 1 + start
|
2012-05-28 05:29:18 -04:00
|
|
|
end
|
2012-12-23 01:38:01 -05:00
|
|
|
line = line.strip.empty? ? ' ' : line
|
|
|
|
table += "<div class='#{classes}'>#{line}</div>"
|
2012-05-28 05:29:18 -04:00
|
|
|
end
|
2012-12-24 01:23:48 -05:00
|
|
|
table +="</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
|
2012-12-23 01:38:01 -05:00
|
|
|
lines = "<td class='line-numbers' aria-hidden='true'><pre>"
|
2012-05-27 03:30:17 -04:00
|
|
|
count.times do |index|
|
2012-12-21 00:38:03 -05:00
|
|
|
classes = 'line-number'
|
|
|
|
if marks.include? index + start
|
|
|
|
classes += ' marked'
|
|
|
|
classes += ' start' unless marks.include? index - 1 + start
|
|
|
|
classes += ' end' unless marks.include? index + 1 + start
|
|
|
|
end
|
2012-12-23 01:38:01 -05:00
|
|
|
lines += "<div data-line='#{index + start}' class='#{classes}'></div>"
|
2012-05-28 05:29:18 -04:00
|
|
|
end
|
2012-12-23 01:38:01 -05:00
|
|
|
lines += "</pre></td>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_markup (input)
|
|
|
|
lang = input.match(/\s*lang:(\w+)/i)
|
|
|
|
title = input.match(/\s*title:\s*(("(.+?)")|('(.+?)')|(\S+))/i)
|
|
|
|
linenos = input.match(/\s*linenos:(\w+)/i)
|
2013-01-03 12:36:51 -05:00
|
|
|
escape = input.match(/\s*escape:(\w+)/i)
|
2012-12-23 01:38:01 -05:00
|
|
|
marks = get_marks(input)
|
|
|
|
url = input.match(/\s*url:\s*(("(.+?)")|('(.+?)')|(\S+))/i)
|
2012-12-24 01:23:48 -05:00
|
|
|
link_text = input.match(/\s*link[-_]text:\s*(("(.+?)")|('(.+?)')|(\S+))/i)
|
2012-12-23 01:38:01 -05:00
|
|
|
start = input.match(/\s*start:(\d+)/i)
|
|
|
|
endline = input.match(/\s*end:(\d+)/i)
|
|
|
|
|
|
|
|
opts = {
|
|
|
|
lang: (lang.nil? ? nil : lang[1]),
|
|
|
|
title: (title.nil? ? nil : title[3] || title[5] || title[6]),
|
|
|
|
linenos: (linenos.nil? ? nil : linenos[1]),
|
2013-01-03 12:36:51 -05:00
|
|
|
escape: (escape.nil? ? nil : escape[1]),
|
2012-12-23 01:38:01 -05:00
|
|
|
marks: marks,
|
|
|
|
url: (url.nil? ? nil : url[3] || url[5] || url[6]),
|
|
|
|
start: (start.nil? ? nil : start[1].to_i),
|
|
|
|
end: (endline.nil? ? nil : endline[1].to_i),
|
2013-02-22 00:53:04 -05:00
|
|
|
link_text: (link_text.nil? ? nil : link_text[3] || link_text[5] || link_text[6])
|
2012-12-23 01:38:01 -05:00
|
|
|
}
|
2012-12-24 16:59:48 -05:00
|
|
|
opts.merge(parse_range(input, opts[:start], opts[:end]))
|
2012-12-23 01:38:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def clean_markup (input)
|
2013-01-03 12:36:51 -05:00
|
|
|
input.sub(/\s*lang:\s*\w+/i, ''
|
2012-12-23 01:38:01 -05:00
|
|
|
).sub(/\s*title:\s*(("(.+?)")|('(.+?)')|(\S+))/i, ''
|
2013-01-03 12:36:51 -05:00
|
|
|
).sub(/\s*url:\s*(\S+)/i, ''
|
2012-12-23 01:38:01 -05:00
|
|
|
).sub(/\s*link_text:\s*(("(.+?)")|('(.+?)')|(\S+))/i, ''
|
|
|
|
).sub(/\s*mark:\d\S*/i,''
|
2013-01-03 12:36:51 -05:00
|
|
|
).sub(/\s*linenos:\s*\w+/i,''
|
|
|
|
).sub(/\s*start:\s*\d+/i,''
|
|
|
|
).sub(/\s*end:\s*\d+/i,''
|
|
|
|
).sub(/\s*range:\s*\d+-\d+/i,'')
|
2012-05-28 05:29:18 -04:00
|
|
|
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 = []
|
2012-06-03 01:13:39 -04:00
|
|
|
if input =~ / *mark:(\d\S*)/i
|
2012-05-28 05:29:18 -04:00
|
|
|
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
|
|
|
|
|
2012-12-24 16:59:48 -05:00
|
|
|
def parse_range (input, start, endline)
|
2012-06-03 01:13:39 -04:00
|
|
|
if input =~ / *range:(\d+)-(\d+)/i
|
2012-05-28 05:29:18 -04:00
|
|
|
start = $1.to_i
|
2012-06-03 01:13:39 -04:00
|
|
|
endline = $2.to_i
|
2012-05-28 05:29:18 -04:00
|
|
|
end
|
|
|
|
{start: start, end: endline}
|
|
|
|
end
|
2013-02-22 00:53:04 -05:00
|
|
|
|
2012-12-24 16:59:48 -05:00
|
|
|
def get_range (code, start, endline)
|
|
|
|
length = code.lines.count
|
|
|
|
start ||= 1
|
|
|
|
endline ||= length
|
|
|
|
if start > 1 or endline < length
|
|
|
|
raise "#{filepath} is #{length} lines long, cannot begin at line #{start}" if start > length
|
|
|
|
raise "#{filepath} is #{length} lines long, cannot read beyond line #{endline}" if endline > length
|
|
|
|
code = code.split(/\n/).slice(start - 1, endline + 1 - start).join("\n")
|
|
|
|
end
|
|
|
|
code
|
|
|
|
end
|
|
|
|
|
2013-03-10 16:47:07 -04:00
|
|
|
def highlight_failed(error, syntax, markup, code, file = nil)
|
2013-03-09 20:06:02 -05:00
|
|
|
code_snippet = code.split("\n")[0..9].map{|l| " #{l}" }.join("\n")
|
2013-03-10 16:47:07 -04:00
|
|
|
fail_message = "\nPygments Error while parsing the following markup#{" in #{file}" if file}:\n\n".red
|
2013-03-10 01:03:30 -05:00
|
|
|
fail_message += " #{markup}\n#{code_snippet}\n"
|
|
|
|
fail_message += "#{" ..." if code.split("\n").size > 10}\n"
|
2013-03-10 16:47:07 -04:00
|
|
|
fail_message += "\nValid Syntax:\n\n#{syntax}\n".yellow
|
|
|
|
fail_message += "\nPygments Error:\n\n#{error.message}".red
|
2013-03-10 01:03:30 -05:00
|
|
|
$stderr.puts fail_message.chomp
|
2013-03-09 19:44:32 -05:00
|
|
|
raise ArgumentError
|
|
|
|
end
|
|
|
|
|
2011-07-26 23:36:42 -04:00
|
|
|
end
|