Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

51 lines (46 sloc) 1.447 kb
require './plugins/pygments_code'
module BacktickCodeBlock
include HighlightCode
AllOptions = /([^\s]+)\s+(.+?)(https?:\/\/\S+)\s*(.+)?/i
LangCaption = /([^\s]+)\s*(.+)?/i
def render_code_block(input)
@options = nil
@caption = nil
@lang = nil
@url = nil
@title = nil
begin
input.gsub /^(\s*)`{3} *([^\n]+)?\n(.+?)\n\s*`{3}/m do
indentation = $1.delete("\n")
@options = $2 || ''
str = $3
if @options =~ AllOptions
@lang = $1
@caption = "<figcaption><span>#{$2}</span><a href='#{$3}'>#{$4 || 'link'}</a></figcaption>"
elsif @options =~ LangCaption
@lang = $1
@caption = "<figcaption><span>#{$2}</span></figcaption>"
end
if str.match(/\A {4}/)
str = str.gsub /^ {4}/, ''
end
figure = if @lang.nil? || @lang == 'plain'
code = tableize_code(str.gsub('<','&lt;').gsub('>','&gt;'))
"<figure class='code'>#{@caption}#{code}</figure>"
else
if @lang.include? "-raw"
raw = "``` #{@options.sub('-raw', '')}\n"
raw += str
raw += "\n```\n"
else
code = highlight(str, @lang)
"<figure class='code'>#{@caption}#{code}</figure>"
end
end
"\n" + indentation + figure
end
rescue
raise "Exception on code sample: #{input[0..150]}"
end
end
end
Jump to Line
Something went wrong with that request. Please try again.