2011-09-07 19:32:57 -04:00
|
|
|
require './plugins/pygments_code'
|
|
|
|
|
|
|
|
module BacktickCodeBlock
|
|
|
|
include HighlightCode
|
|
|
|
AllOptions = /([^\s]+)\s+(.+?)(https?:\/\/\S+)\s*(.+)?/i
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
if str.match(/\A {4}/)
|
|
|
|
str = str.gsub /^ {4}/, ''
|
|
|
|
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
|