diff options
author | Brandon Mathis <brandon@imathis.com> | 2011-07-19 09:06:54 -0400 |
---|---|---|
committer | Brandon Mathis <brandon@imathis.com> | 2011-07-19 09:06:54 -0400 |
commit | 17c59fb1d1bf3e0c05137af4b4bd09ae271a2d31 (patch) | |
tree | a4b3b5d43173f9b02ec4b6401cb6e14f6e716a35 /plugins/code_block.rb | |
parent | 873a604e144c53cfc5465a790e43db5b7ebb429e (diff) | |
download | my_new_personal_website-17c59fb1d1bf3e0c05137af4b4bd09ae271a2d31.tar.xz my_new_personal_website-17c59fb1d1bf3e0c05137af4b4bd09ae271a2d31.zip |
Moved plugins to root directory. I'm ditching the idea of shipping plugins with themes until it's more obviously necessary. This way it's easier to merge and update plugins.
Diffstat (limited to 'plugins/code_block.rb')
-rw-r--r-- | plugins/code_block.rb | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/plugins/code_block.rb b/plugins/code_block.rb new file mode 100644 index 00000000..00762d8a --- /dev/null +++ b/plugins/code_block.rb @@ -0,0 +1,80 @@ +# 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. +# +# Syntax: {% codeblock [title] [url] [link text] %} +# +# For syntax highlighting, put a file extension somewhere in the title. examples: +# {% codeblock file.sh %} +# {% codeblock Time to be Awesome! (awesome.rb) %} +# +# 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> +# <pre><code><sarcasm> Ooooh, sarcasm... How original!</sarcasm></code></pre> +# </figure> +# +module Jekyll + + class CodeBlock < Liquid::Block + 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 + @caption = "<figcaption><span>#{$1}</span><a href='#{$2 + $3}'>#{$4}</a</figcaption>" + elsif markup =~ CaptionUrl + @file = $1 + @caption = "<figcaption><span>#{$1}</span><a href='#{$2 + $3}'>link</a</figcaption>" + elsif markup =~ Caption + @file = $1 + @caption = "<figcaption><span>#{$1}</span></figcaption>\n" + end + if @file =~ /\S[\S\s]*\.(\w+)/ + @filetype = $1 + end + super + end + + def render(context) + output = super + code = super.join + source = "<figure role=code>\n" + source += @caption if @caption + if @filetype + source += "{% highlight #{@filetype} %}\n" + code + "\n{% endhighlight %}\n</figure>" + else + source += "<pre><code>" + code.gsub!(/</,'<') + "</code></pre>\n</figure>" + end + partial = Liquid::Template.parse(source) + context.stack do + partial.render(context) + end + end + end +end + +Liquid::Template.register_tag('codeblock', Jekyll::CodeBlock) |