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-09-07 19:32:57 -04:00
include TemplateWrapper
2012-12-26 18:18:29 -05:00
CaptionUrlTitle = / ( \ S[ \ S \ s]*) \ s+(https?: \/ \/ \ S+| \/ \ S+) \ s*(.+)? /i
2011-07-17 17:25:27 -04:00
Caption = / ( \ S[ \ S \ s]*) /
def initialize ( tag_name , markup , tokens )
@title = nil
@caption = nil
2011-08-20 20:24:51 -04:00
@filetype = nil
2011-07-17 17:25:27 -04:00
@highlight = true
2013-04-09 10:23:31 -04:00
if markup =~ / \ s*lang:( \ S+) /i
2011-08-20 20:24:51 -04:00
@filetype = $1
2013-04-09 10:23:31 -04:00
markup = markup . sub ( / \ s*lang:( \ S+) /i , '' )
2011-08-20 20:24:51 -04:00
end
2011-07-17 17:25:27 -04:00
if markup =~ CaptionUrlTitle
@file = $1
2012-12-26 18:33:48 -05:00
@caption = " <figcaption><span> #{ $1 } </span><a href=' #{ $2 } '> #{ $3 || 'link' } </a></figcaption> "
2011-07-17 17:25:27 -04:00
elsif markup =~ Caption
@file = $1
@caption = " <figcaption><span> #{ $1 } </span></figcaption> \n "
end
2011-08-20 20:24:51 -04:00
if @file =~ / \ S[ \ S \ s]* \ w+ \ .( \ w+) / && @filetype . nil?
2011-07-17 17:25:27 -04:00
@filetype = $1
end
super
end
def render ( context )
output = super
2012-05-24 11:28:32 -04:00
code = super
2011-09-18 15:27:42 -04:00
source = " <figure class='code'> "
2011-07-17 17:25:27 -04:00
source += @caption if @caption
if @filetype
2011-09-07 19:32:57 -04:00
source += " #{ highlight ( code , @filetype ) } </figure> "
2011-07-17 17:25:27 -04:00
else
2011-09-07 19:32:57 -04:00
source += " #{ tableize_code ( code . lstrip . rstrip . gsub ( / < / , '<' ) ) } </figure> "
2011-07-17 17:25:27 -04:00
end
2011-09-07 19:32:57 -04:00
source = safe_wrap ( source )
source = context [ 'pygments_prefix' ] + source if context [ 'pygments_prefix' ]
2011-07-28 10:02:49 -04:00
source = source + context [ 'pygments_suffix' ] if context [ 'pygments_suffix' ]
2011-10-11 09:46:20 -04:00
source
2011-07-17 17:25:27 -04:00
end
end
end
Liquid :: Template . register_tag ( 'codeblock' , Jekyll :: CodeBlock )