2011-09-07 19:32:57 -04:00
|
|
|
require './plugins/pygments_code'
|
|
|
|
|
|
|
|
module BacktickCodeBlock
|
|
|
|
include HighlightCode
|
2012-12-26 18:31:51 -05:00
|
|
|
AllOptions = /([^\s]+)\s+(.+?)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i
|
2011-09-07 19:32:57 -04:00
|
|
|
LangCaption = /([^\s]+)\s*(.+)?/i
|
2013-01-03 12:36:51 -05:00
|
|
|
def render_code_block(input, ext)
|
|
|
|
escape = ext ? ext.match(/textile/) != nil : false
|
2012-05-11 19:59:03 -04:00
|
|
|
input.encode!("UTF-8")
|
2012-05-27 03:30:17 -04:00
|
|
|
input.gsub /^`{3}(.+?)`{3}/m do
|
|
|
|
str = $1.to_s
|
|
|
|
str.gsub /([^\n]+)?\n(.+?)\Z/m do
|
2012-05-28 05:29:18 -04:00
|
|
|
markup = $1 || ''
|
2012-05-27 03:30:17 -04:00
|
|
|
code = $2.to_s
|
2012-05-28 05:29:18 -04:00
|
|
|
|
2012-12-24 16:59:48 -05:00
|
|
|
opts = parse_markup(markup)
|
|
|
|
@options = {
|
|
|
|
lang: opts[:lang],
|
|
|
|
title: opts[:title],
|
|
|
|
lineos: opts[:lineos],
|
|
|
|
marks: opts[:marks],
|
|
|
|
url: opts[:url],
|
|
|
|
link_text: opts[:link_text] || 'link',
|
|
|
|
start: opts[:start] || 1,
|
2013-01-03 12:36:51 -05:00
|
|
|
escape: opts[:escape] || escape
|
2012-12-24 16:59:48 -05:00
|
|
|
}
|
2012-12-23 01:38:01 -05:00
|
|
|
markup = clean_markup(markup)
|
2012-05-28 05:29:18 -04:00
|
|
|
|
|
|
|
if markup =~ AllOptions
|
2012-12-24 16:59:48 -05:00
|
|
|
@options[:lang] ||= $1
|
|
|
|
@options[:title] ||= $2
|
|
|
|
@options[:url] ||= $3
|
|
|
|
@options[:link_text] ||= $4
|
2012-05-28 05:29:18 -04:00
|
|
|
elsif markup =~ LangCaption
|
2012-12-24 16:59:48 -05:00
|
|
|
@options[:lang] ||= $1
|
|
|
|
@options[:title] ||= $2
|
2011-09-07 19:32:57 -04:00
|
|
|
else
|
2012-12-24 16:59:48 -05:00
|
|
|
@options[:lang] ||= 'plain'
|
2011-09-07 19:32:57 -04:00
|
|
|
end
|
2013-03-09 19:45:05 -05:00
|
|
|
|
|
|
|
begin
|
|
|
|
highlight(code, @options)
|
|
|
|
rescue
|
2013-03-09 20:06:02 -05:00
|
|
|
highlight_failed('backtick_code_block', "``` [lang]\n code\n ```", code, @options[:lang])
|
2013-03-09 19:45:05 -05:00
|
|
|
end
|
2011-09-07 19:32:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|