plugins/backtick_code_block.rb
3d2d1a8b
 require './plugins/pygments_code'
 
 module BacktickCodeBlock
   include HighlightCode
0ccd0679
   AllOptions = /([^\s]+)\s+(.+?)(https?:\/\/\S+|\/\S+)\s*(.+)?/i
3d2d1a8b
   LangCaption = /([^\s]+)\s*(.+)?/i
   def render_code_block(input)
a289c909
     @options = nil
3d2d1a8b
     @caption = nil
     @lang = nil
     @url = nil
     @title = nil
1772a8af
     input.gsub(/^`{3} *([^\n]+)?\n(.+?)\n`{3}/m) do
a289c909
       @options = $1 || ''
3d2d1a8b
       str = $2
 
a289c909
       if @options =~ AllOptions
3d2d1a8b
         @lang = $1
         @caption = "<figcaption><span>#{$2}</span><a href='#{$3}'>#{$4 || 'link'}</a></figcaption>"
a289c909
       elsif @options =~ LangCaption
3d2d1a8b
         @lang = $1
         @caption = "<figcaption><span>#{$2}</span></figcaption>"
       end
 
1772a8af
       if str.match(/\A( {4}|\t)/)
         str = str.gsub(/^( {4}|\t)/, '')
3d2d1a8b
       end
       if @lang.nil? || @lang == 'plain'
         code = tableize_code(str.gsub('<','&lt;').gsub('>','&gt;'))
347e855d
         "<figure class='code'>#{@caption}#{code}</figure>"
3d2d1a8b
       else
         if @lang.include? "-raw"
a289c909
           raw = "``` #{@options.sub('-raw', '')}\n"
3d2d1a8b
           raw += str
           raw += "\n```\n"
         else
           code = highlight(str, @lang)
347e855d
           "<figure class='code'>#{@caption}#{code}</figure>"
3d2d1a8b
         end
       end
     end
   end
 end