mirror of
https://github.com/moparisthebest/android.moparisthebest.org
synced 2024-12-25 17:08:48 -05:00
42 lines
1.1 KiB
Ruby
42 lines
1.1 KiB
Ruby
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
|
|
input.encode!("UTF-8")
|
|
input.gsub(/^`{3} *([^\n]+)?\n([\S\s]+?)\n`{3}/m) do
|
|
@options = $1 || ''
|
|
str = $2
|
|
|
|
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 @lang.nil? || @lang == 'plain'
|
|
code = tableize_code(str.gsub('<','<').gsub('>','>'))
|
|
"<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
|
|
end
|
|
end
|
|
end
|