2011-07-17 17:25:27 -04:00
# Title: Simple Code Blocks for Jekyll
# Author: Brandon Mathis http://brandonmathis.com
2012-12-23 01:44:40 -05:00
# Description: Write codeblocks with semantic HTML5 <figure> and <figcaption> elements and optional syntax highlighting — all with a simple, intuitive interface.
2011-07-17 17:25:27 -04:00
#
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'>
2012-12-23 01:44:40 -05:00
# <figcaption><span>Got pain? painrelief.sh</span> <a href="http://site.com/painrelief.sh">Download it!</a>
2011-07-17 17:25:27 -04:00
# <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
2012-12-26 18:31:51 -05:00
TitleUrlLinkText = / ( \ S[ \ S \ s]*) \ s+(https?: \/ \/ \ S+| \/ \ S+) \ s*(.+)? /i
2012-12-23 01:38:01 -05:00
Title = / ( \ S[ \ S \ s]*) /
2011-07-17 17:25:27 -04:00
def initialize ( tag_name , markup , tokens )
2012-12-24 16:59:48 -05:00
opts = parse_markup ( markup )
@options = {
lang : opts [ :lang ] ,
title : opts [ :title ] ,
lineos : opts [ :lineos ] ,
marks : opts [ :marks ] ,
url : opts [ :url ] ,
link_text : opts [ :link_text ] || 'link' ,
start : opts [ :start ] || 1 ,
}
2012-12-23 01:38:01 -05:00
markup = clean_markup ( markup )
2012-05-28 05:29:18 -04:00
2012-12-23 01:38:01 -05:00
if markup =~ TitleUrlLinkText
2012-12-24 16:59:48 -05:00
@options [ :title ] || = $1
2012-12-26 18:31:51 -05:00
@options [ :url ] || = $2
@options [ :link_text ] || = $3
2012-12-23 01:38:01 -05:00
elsif markup =~ Title
2012-12-24 16:59:48 -05:00
@options [ :title ] || = $1
2011-07-17 17:25:27 -04:00
end
2012-12-23 01:38:01 -05:00
# grab lang from filename in title
2012-12-24 16:59:48 -05:00
if @options [ :title ] =~ / \ S[ \ S \ s]* \ w+ \ .( \ w+) / && @options [ :lang ] . nil?
@options [ :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
2012-12-24 16:59:48 -05:00
code = highlight ( code , @options )
2012-05-27 03:30:17 -04:00
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 )