2011-09-08 05:02:57 +05:30
|
|
|
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 09:59:54 +05:30
|
|
|
@options = nil
|
2011-09-08 05:02:57 +05:30
|
|
|
@caption = nil
|
|
|
|
@lang = nil
|
|
|
|
@url = nil
|
|
|
|
@title = nil
|
2011-10-16 15:32:14 +05:30
|
|
|
input.gsub(/^`{3} *([^\n]+)?\n(.+?)\n`{3}/m) do
|
2011-09-08 09:59:54 +05:30
|
|
|
@options = $1 || ''
|
2011-09-08 05:02:57 +05:30
|
|
|
str = $2
|
|
|
|
|
2011-09-08 09:59:54 +05:30
|
|
|
if @options =~ AllOptions
|
2011-09-08 05:02:57 +05:30
|
|
|
@lang = $1
|
|
|
|
@caption = "<figcaption><span>#{$2}</span><a href='#{$3}'>#{$4 || 'link'}</a></figcaption>"
|
2011-09-08 09:59:54 +05:30
|
|
|
elsif @options =~ LangCaption
|
2011-09-08 05:02:57 +05:30
|
|
|
@lang = $1
|
|
|
|
@caption = "<figcaption><span>#{$2}</span></figcaption>"
|
|
|
|
end
|
|
|
|
|
2011-10-16 15:32:14 +05:30
|
|
|
if str.match(/\A( {4}|\t)/)
|
|
|
|
str = str.gsub(/^( {4}|\t)/, '')
|
2011-09-08 05:02:57 +05:30
|
|
|
end
|
|
|
|
if @lang.nil? || @lang == 'plain'
|
|
|
|
code = tableize_code(str.gsub('<','<').gsub('>','>'))
|
2011-09-18 17:25:35 +05:30
|
|
|
"<figure class='code'>#{@caption}#{code}</figure>"
|
2011-09-08 05:02:57 +05:30
|
|
|
else
|
|
|
|
if @lang.include? "-raw"
|
2011-09-08 09:59:54 +05:30
|
|
|
raw = "``` #{@options.sub('-raw', '')}\n"
|
2011-09-08 05:02:57 +05:30
|
|
|
raw += str
|
|
|
|
raw += "\n```\n"
|
|
|
|
else
|
|
|
|
code = highlight(str, @lang)
|
2011-09-18 17:25:35 +05:30
|
|
|
"<figure class='code'>#{@caption}#{code}</figure>"
|
2011-09-08 05:02:57 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|