2011-07-17 17:25:27 -04:00
# 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.
#
2011-07-20 12:11:52 -04:00
# Syntax:
# {% codeblock [title] [url] [link text] %}
# code snippet
# {% endcodeblock %}
2011-07-17 17:25:27 -04:00
#
# For syntax highlighting, put a file extension somewhere in the title. examples:
# {% codeblock file.sh %}
2011-07-20 12:11:52 -04:00
# code snippet
# {% endcodeblock %}
#
2011-07-17 17:25:27 -04:00
# {% codeblock Time to be Awesome! (awesome.rb) %}
2011-07-20 12:11:52 -04:00
# code snippet
# {% endcodeblock %}
2011-07-17 17:25:27 -04:00
#
# Example:
#
# {% codeblock Got pain? painreleif.sh http://site.com/painreleief.sh Download it! %}
# $ rm -rf ~/PAIN
# {% endcodeblock %}
#
# Output:
#
2011-09-18 15:27:42 -04:00
# <figure class='code'>
2011-07-17 17:25:27 -04:00
# <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 %}
#
2011-09-18 15:27:42 -04:00
# <figure class='code'>
2011-07-17 17:27:18 -04:00
# <pre><code><sarcasm> Ooooh, sarcasm... How original!</sarcasm></code></pre>
2011-07-17 17:25:27 -04:00
# </figure>
#
2011-07-26 23:36:42 -04:00
require './plugins/pygments_code'
2011-09-07 19:32:57 -04:00
require './plugins/raw'
2011-07-26 23:36:42 -04:00
2011-07-17 17:25:27 -04:00
module Jekyll
class CodeBlock < Liquid :: Block
2011-07-26 23:36:42 -04:00
include HighlightCode
2011-07-17 17:25:27 -04:00
CaptionUrlTitle = / ( \ S[ \ S \ s]*) \ s+(https?: \/ \/ )( \ S+) \ s+(.+) /i
CaptionUrl = / ( \ S[ \ S \ s]*) \ s+(https?: \/ \/ )( \ S+) /i
Caption = / ( \ S[ \ S \ s]*) /
def initialize ( tag_name , markup , tokens )
@caption = nil
2012-05-27 03:30:17 -04:00
@url = nil
@lang = nil
@start = 1
@linenos = true
2011-08-21 10:31:14 -04:00
if markup =~ / \ s*lang:( \ w+) /i
2012-05-27 03:30:17 -04:00
@lang = $1
2011-08-21 10:31:14 -04:00
markup = markup . sub ( / lang: \ w+ /i , '' )
2011-08-20 20:24:51 -04:00
end
2012-05-27 03:30:17 -04:00
if markup . strip =~ / \ s*linenos:false /i
@linenos = false
markup = markup . strip . sub ( / linenos:false /i , '' )
end
if markup =~ / \ s*start:( \ d+) /i
@start = $1 . to_i
markup = markup . sub ( / \ s*start: \ d+ /i , '' )
end
2011-07-17 17:25:27 -04:00
if markup =~ CaptionUrlTitle
2012-05-27 03:30:17 -04:00
@caption = $1
@url = $2 + $3
@anchor = $4
2011-07-17 17:25:27 -04:00
elsif markup =~ CaptionUrl
2012-05-27 03:30:17 -04:00
@caption = $1
@url = $2 + $3
2011-07-17 17:25:27 -04:00
elsif markup =~ Caption
2012-05-27 03:30:17 -04:00
@caption = $1
2011-07-17 17:25:27 -04:00
end
2012-05-27 03:30:17 -04:00
if @caption =~ / \ S[ \ S \ s]* \ w+ \ .( \ w+) / && @lang . nil?
@lang = $1
2011-07-17 17:25:27 -04:00
end
super
end
def render ( context )
2012-05-27 03:30:17 -04:00
code = super . strip
code = highlight ( code , @lang , { caption : @caption , url : @url , anchor : @anchor , start : @start , linenos : @linenos } )
code = context [ 'pygments_prefix' ] + code if context [ 'pygments_prefix' ]
code = code + context [ 'pygments_suffix' ] if context [ 'pygments_suffix' ]
code
2011-07-17 17:25:27 -04:00
end
end
end
Liquid :: Template . register_tag ( 'codeblock' , Jekyll :: CodeBlock )