plugins/code_block.rb
fe8150d7
 # Title: Simple Code Blocks for Jekyll
 # Author: Brandon Mathis http://brandonmathis.com
 # Description: Write codeblocks with semantic HTML5 <figure> and <figcaption> elements and optional syntax highlighting — all with a simple, intuitive interface.
 #
0843c02f
 # Syntax:
 # {% codeblock [title] [url] [link text] %}
 # code snippet
 # {% endcodeblock %}
fe8150d7
 #
 # For syntax highlighting, put a file extension somewhere in the title. examples:
 # {% codeblock file.sh %}
0843c02f
 # code snippet
 # {% endcodeblock %}
 #
fe8150d7
 # {% codeblock Time to be Awesome! (awesome.rb) %}
0843c02f
 # code snippet
 # {% endcodeblock %}
fe8150d7
 #
 # Example:
 #
 # {% codeblock Got pain? painreleif.sh http://site.com/painreleief.sh Download it! %}
 # $ rm -rf ~/PAIN
 # {% endcodeblock %}
 #
 # Output:
 #
44e4a991
 # <figure class='code'>
fe8150d7
 # <figcaption><span>Got pain? painrelief.sh</span> <a href="http://site.com/painrelief.sh">Download it!</a>
 # <div class="highlight"><pre><code class="sh">
 # -- nicely escaped highlighted code --
 # </code></pre></div>
 # </figure>
 #
 # Example 2 (no syntax highlighting):
 #
 # {% codeblock %}
 # <sarcasm>Ooooh, sarcasm... How original!</sarcasm>
 # {% endcodeblock %}
 #
44e4a991
 # <figure class='code'>
e17bb6eb
 # <pre><code>&lt;sarcasm> Ooooh, sarcasm... How original!&lt;/sarcasm></code></pre>
fe8150d7
 # </figure>
 #
7b81aab5
 require './plugins/pygments_code'
3d2d1a8b
 require './plugins/raw'
7b81aab5
 
fe8150d7
 module Jekyll
 
   class CodeBlock < Liquid::Block
7b81aab5
     include HighlightCode
3d2d1a8b
     include TemplateWrapper
c18de558
     CaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i
fe8150d7
     Caption = /(\S[\S\s]*)/
     def initialize(tag_name, markup, tokens)
       @title = nil
       @caption = nil
83d87f98
       @filetype = nil
fe8150d7
       @highlight = true
089af26b
       if markup =~ /\s*lang:(\w+)/i
83d87f98
         @filetype = $1
089af26b
         markup = markup.sub(/lang:\w+/i,'')
83d87f98
       end
fe8150d7
       if markup =~ CaptionUrlTitle
         @file = $1
55fa9b3d
         @caption = "<figcaption><span>#{$1}</span><a href='#{$2}'>#{$3 || 'link'}</a></figcaption>"
fe8150d7
       elsif markup =~ Caption
         @file = $1
         @caption = "<figcaption><span>#{$1}</span></figcaption>\n"
       end
83d87f98
       if @file =~ /\S[\S\s]*\w+\.(\w+)/ && @filetype.nil?
fe8150d7
         @filetype = $1
       end
       super
     end
 
     def render(context)
       output = super
e53b26ad
       code = super
44e4a991
       source = "<figure class='code'>"
fe8150d7
       source += @caption if @caption
       if @filetype
3d2d1a8b
         source += " #{highlight(code, @filetype)}</figure>"
fe8150d7
       else
3d2d1a8b
         source += "#{tableize_code(code.lstrip.rstrip.gsub(/</,'&lt;'))}</figure>"
fe8150d7
       end
3d2d1a8b
       source = safe_wrap(source)
       source = context['pygments_prefix'] + source if context['pygments_prefix']
2d8a17cd
       source = source + context['pygments_suffix'] if context['pygments_suffix']
9909f16d
       source
fe8150d7
     end
   end
 end
 
 Liquid::Template.register_tag('codeblock', Jekyll::CodeBlock)