2011-07-18 02:55:27 +05:30
# 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 21:41:52 +05:30
# Syntax:
# {% codeblock [title] [url] [link text] %}
# code snippet
# {% endcodeblock %}
2011-07-18 02:55:27 +05:30
#
# For syntax highlighting, put a file extension somewhere in the title. examples:
# {% codeblock file.sh %}
2011-07-20 21:41:52 +05:30
# code snippet
# {% endcodeblock %}
#
2011-07-18 02:55:27 +05:30
# {% codeblock Time to be Awesome! (awesome.rb) %}
2011-07-20 21:41:52 +05:30
# code snippet
# {% endcodeblock %}
2011-07-18 02:55:27 +05:30
#
# Example:
#
# {% codeblock Got pain? painreleif.sh http://site.com/painreleief.sh Download it! %}
# $ rm -rf ~/PAIN
# {% endcodeblock %}
#
# Output:
#
2011-09-19 00:57:42 +05:30
# <figure class='code'>
2011-07-18 02:55:27 +05:30
# <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-19 00:57:42 +05:30
# <figure class='code'>
2011-07-18 02:57:18 +05:30
# <pre><code><sarcasm> Ooooh, sarcasm... How original!</sarcasm></code></pre>
2011-07-18 02:55:27 +05:30
# </figure>
#
2011-07-27 09:06:42 +05:30
require './plugins/pygments_code'
2011-09-08 05:02:57 +05:30
require './plugins/raw'
2011-07-27 09:06:42 +05:30
2011-07-18 02:55:27 +05:30
module Jekyll
class CodeBlock < Liquid :: Block
2011-07-27 09:06:42 +05:30
include HighlightCode
2011-09-08 05:02:57 +05:30
include TemplateWrapper
2011-07-18 02:55:27 +05:30
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
2011-08-21 05:54:51 +05:30
@filetype = nil
2011-07-18 02:55:27 +05:30
@highlight = true
2011-08-21 20:01:14 +05:30
if markup =~ / \ s*lang:( \ w+) /i
2011-08-21 05:54:51 +05:30
@filetype = $1
2011-08-21 20:01:14 +05:30
markup = markup . sub ( / lang: \ w+ /i , '' )
2011-08-21 05:54:51 +05:30
end
2011-07-18 02:55:27 +05:30
if markup =~ CaptionUrlTitle
@file = $1
2011-07-20 03:30:56 +05:30
@caption = " <figcaption><span> #{ $1 } </span><a href=' #{ $2 + $3 } '> #{ $4 } </a></figcaption> "
2011-07-18 02:55:27 +05:30
elsif markup =~ CaptionUrl
@file = $1
2011-07-20 03:30:56 +05:30
@caption = " <figcaption><span> #{ $1 } </span><a href=' #{ $2 + $3 } '>link</a></figcaption> "
2011-07-18 02:55:27 +05:30
elsif markup =~ Caption
@file = $1
@caption = " <figcaption><span> #{ $1 } </span></figcaption> \n "
end
2011-08-21 05:54:51 +05:30
if @file =~ / \ S[ \ S \ s]* \ w+ \ .( \ w+) / && @filetype . nil?
2011-07-18 02:55:27 +05:30
@filetype = $1
end
super
end
def render ( context )
output = super
2012-05-24 20:58:32 +05:30
code = super
2011-09-19 00:57:42 +05:30
source = " <figure class='code'> "
2011-07-18 02:55:27 +05:30
source += @caption if @caption
if @filetype
2011-09-08 05:02:57 +05:30
source += " #{ highlight ( code , @filetype ) } </figure> "
2011-07-18 02:55:27 +05:30
else
2011-09-08 05:02:57 +05:30
source += " #{ tableize_code ( code . lstrip . rstrip . gsub ( / < / , '<' ) ) } </figure> "
2011-07-18 02:55:27 +05:30
end
2011-09-08 05:02:57 +05:30
source = safe_wrap ( source )
source = context [ 'pygments_prefix' ] + source if context [ 'pygments_prefix' ]
2011-07-28 19:32:49 +05:30
source = source + context [ 'pygments_suffix' ] if context [ 'pygments_suffix' ]
2011-10-11 19:16:20 +05:30
source
2011-07-18 02:55:27 +05:30
end
end
end
Liquid :: Template . register_tag ( 'codeblock' , Jekyll :: CodeBlock )