+ Liquid::Template.register_tag('code', LiquidCode)
+ end
+
+ class LiquidCodeBlock < Liquid::Block
+ Syntax = /((?:as)\s+(#{Liquid::QuotedFragment}+))?/o
+
+ def initialize(tag_name, markup, tokens)
+ Liquid::Tag.instance_method(:initialize).bind(self).call(tag_name, markup, tokens)
+
+ if markup =~ Syntax
+ @language = $2
+ @attributes = {}
+ else
+ raise SyntaxError.new("Error in tag 'code' - Valid syntax: codeblock as '[language]'")
+ end
+ end
+
+ def render(context)
+ require 'coderay'
+
+ partial = super
+ html = ''
+
+ if partial[0] == '\n'
+ partial = partial[1..-1]
+ end
+
+ # be explicit about errors
+ context.exception_renderer = lambda do |exc|
+ exc.is_a?(Liquid::InternalError) ? "Liquid error: #{exc.cause.message}" : exc
+ end
+
+ context.stack do
+ html = CodeRay.scan(partial, @language).div
+ end
+
+ "<notextile>#{html}</notextile>"
+ end
+
+ Liquid::Template.register_tag('codeblock', LiquidCodeBlock)