# Title: Simple Code Blocks for Jekyll # Author: Brandon Mathis http://brandonmathis.com # Description: Write codeblocks with semantic HTML5
and
elements and optional syntax highlighting — all with a simple, intuitive interface. # # Syntax: # {% codeblock [title] [url] [link text] %} # code snippet # {% endcodeblock %} # # For syntax highlighting, put a file extension somewhere in the title. examples: # {% codeblock file.sh %} # code snippet # {% endcodeblock %} # # {% codeblock Time to be Awesome! (awesome.rb) %} # code snippet # {% endcodeblock %} # # Example: # # {% codeblock Got pain? painreleif.sh http://site.com/painreleief.sh Download it! %} # $ rm -rf ~/PAIN # {% endcodeblock %} # # Output: # #
#
Got pain? painrelief.sh Download it! #

# -- nicely escaped highlighted code --
# 
#
# # Example 2 (no syntax highlighting): # # {% codeblock %} # Ooooh, sarcasm... How original! # {% endcodeblock %} # #
#
<sarcasm> Ooooh, sarcasm... How original!</sarcasm>
#
# require './plugins/pygments_code' require './plugins/raw' module Jekyll class CodeBlock < Liquid::Block include HighlightCode include TemplateWrapper CaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i Caption = /(\S[\S\s]*)/ def initialize(tag_name, markup, tokens) @title = nil @caption = nil @filetype = nil @highlight = true if markup =~ /\s*lang:([\w-]+)/i @filetype = $1 markup = markup.sub(/\s*lang:([\w-]+)/i,'') end if markup =~ CaptionUrlTitle @file = $1 @caption = "
#{$1}#{$3 || 'link'}
" elsif markup =~ Caption @file = $1 @caption = "
#{$1}
\n" end if @file =~ /\S[\S\s]*\w+\.(\w+)/ && @filetype.nil? @filetype = $1 end super end def render(context) output = super code = super source = "
" source += @caption if @caption if @filetype source += " #{highlight(code, @filetype)}
" else source += "#{tableize_code(code.lstrip.rstrip.gsub(/" end source = safe_wrap(source) source = context['pygments_prefix'] + source if context['pygments_prefix'] source = source + context['pygments_suffix'] if context['pygments_suffix'] source end end end Liquid::Template.register_tag('codeblock', Jekyll::CodeBlock)