2011-07-17 21:25:27 +00: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 16:11:52 +00:00
# Syntax:
# {% codeblock [title] [url] [link text] %}
# code snippet
# {% endcodeblock %}
2011-07-17 21:25:27 +00:00
#
# For syntax highlighting, put a file extension somewhere in the title. examples:
# {% codeblock file.sh %}
2011-07-20 16:11:52 +00:00
# code snippet
# {% endcodeblock %}
#
2011-07-17 21:25:27 +00:00
# {% codeblock Time to be Awesome! (awesome.rb) %}
2011-07-20 16:11:52 +00:00
# code snippet
# {% endcodeblock %}
2011-07-17 21:25:27 +00:00
#
# Example:
#
# {% codeblock Got pain? painreleif.sh http://site.com/painreleief.sh Download it! %}
# $ rm -rf ~/PAIN
# {% endcodeblock %}
#
# Output:
#
# <figure role=code>
# <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 %}
#
# <figure role=code>
2011-07-17 21:27:18 +00:00
# <pre><code><sarcasm> Ooooh, sarcasm... How original!</sarcasm></code></pre>
2011-07-17 21:25:27 +00:00
# </figure>
#
2011-07-27 03:36:42 +00:00
require './plugins/pygments_code'
2011-07-17 21:25:27 +00:00
module Jekyll
class CodeBlock < Liquid :: Block
2011-07-27 03:36:42 +00:00
include HighlightCode
2011-07-17 21:25:27 +00: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 )
@title = nil
@caption = nil
@highlight = true
if markup =~ CaptionUrlTitle
@file = $1
2011-07-19 22:00:56 +00:00
@caption = " <figcaption><span> #{ $1 } </span><a href=' #{ $2 + $3 } '> #{ $4 } </a></figcaption> "
2011-07-17 21:25:27 +00:00
elsif markup =~ CaptionUrl
@file = $1
2011-07-19 22:00:56 +00:00
@caption = " <figcaption><span> #{ $1 } </span><a href=' #{ $2 + $3 } '>link</a></figcaption> "
2011-07-17 21:25:27 +00:00
elsif markup =~ Caption
@file = $1
@caption = " <figcaption><span> #{ $1 } </span></figcaption> \n "
end
2011-08-16 06:40:47 +00:00
if @file =~ / \ S[ \ S \ s]* \ w+ \ .( \ w+) /
2011-07-17 21:25:27 +00:00
@filetype = $1
end
super
end
def render ( context )
output = super
code = super . join
2011-07-19 22:00:56 +00:00
source = " <div><figure role=code> "
2011-07-17 21:25:27 +00:00
source += @caption if @caption
2011-07-28 14:02:49 +00:00
source = context [ 'pygments_prefix' ] + source if context [ 'pygments_prefix' ]
2011-07-17 21:25:27 +00:00
if @filetype
2011-07-26 00:34:52 +00:00
@filetype = 'objc' if @filetype == 'm'
@filetype = 'perl' if @filetype == 'pl'
2011-08-01 21:35:33 +00:00
@filetype = 'yaml' if @filetype == 'yml'
2011-07-27 03:36:42 +00:00
source += " #{ highlight ( code , @filetype ) } </figure></div> "
2011-07-17 21:25:27 +00:00
else
2011-08-16 06:40:47 +00:00
source += " #{ tableize_code ( code . lstrip . rstrip . gsub ( / < / , '<' ) ) } </figure></div> "
2011-07-17 21:25:27 +00:00
end
2011-07-28 14:02:49 +00:00
source = source + context [ 'pygments_suffix' ] if context [ 'pygments_suffix' ]
2011-07-17 21:25:27 +00:00
end
end
end
Liquid :: Template . register_tag ( 'codeblock' , Jekyll :: CodeBlock )