2011-09-07 19:32:57 -04:00
|
|
|
require './plugins/pygments_code'
|
|
|
|
|
|
|
|
module BacktickCodeBlock
|
|
|
|
include HighlightCode
|
2012-12-26 18:18:29 -05:00
|
|
|
AllOptions = /([^\s]+)\s+(.+?)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i
|
2011-09-07 19:32:57 -04:00
|
|
|
LangCaption = /([^\s]+)\s*(.+)?/i
|
|
|
|
def render_code_block(input)
|
2011-09-08 00:29:54 -04:00
|
|
|
@options = nil
|
2011-09-07 19:32:57 -04:00
|
|
|
@caption = nil
|
|
|
|
@lang = nil
|
|
|
|
@url = nil
|
|
|
|
@title = nil
|
2011-10-16 06:02:14 -04:00
|
|
|
input.gsub(/^`{3} *([^\n]+)?\n(.+?)\n`{3}/m) do
|
2011-09-08 00:29:54 -04:00
|
|
|
@options = $1 || ''
|
2011-09-07 19:32:57 -04:00
|
|
|
str = $2
|
|
|
|
|
2011-09-08 00:29:54 -04:00
|
|
|
if @options =~ AllOptions
|
2011-09-07 19:32:57 -04:00
|
|
|
@lang = $1
|
|
|
|
@caption = "<figcaption><span>#{$2}</span><a href='#{$3}'>#{$4 || 'link'}</a></figcaption>"
|
2011-09-08 00:29:54 -04:00
|
|
|
elsif @options =~ LangCaption
|
2011-09-07 19:32:57 -04:00
|
|
|
@lang = $1
|
|
|
|
@caption = "<figcaption><span>#{$2}</span></figcaption>"
|
|
|
|
end
|
|
|
|
|
2011-10-16 06:02:14 -04:00
|
|
|
if str.match(/\A( {4}|\t)/)
|
|
|
|
str = str.gsub(/^( {4}|\t)/, '')
|
2011-09-07 19:32:57 -04:00
|
|
|
end
|
|
|
|
if @lang.nil? || @lang == 'plain'
|
|
|
|
code = tableize_code(str.gsub('<','<').gsub('>','>'))
|
2011-09-18 07:55:35 -04:00
|
|
|
"<figure class='code'>#{@caption}#{code}</figure>"
|
2011-09-07 19:32:57 -04:00
|
|
|
else
|
|
|
|
if @lang.include? "-raw"
|
2011-09-08 00:29:54 -04:00
|
|
|
raw = "``` #{@options.sub('-raw', '')}\n"
|
2011-09-07 19:32:57 -04:00
|
|
|
raw += str
|
|
|
|
raw += "\n```\n"
|
|
|
|
else
|
|
|
|
code = highlight(str, @lang)
|
2011-09-18 07:55:35 -04:00
|
|
|
"<figure class='code'>#{@caption}#{code}</figure>"
|
2011-09-07 19:32:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|