Sha256: 06f4672f00b2fef592b2478cdb8a546276d26561482cba4fa53f62e6fa05a423
Contents?: true
Size: 1.57 KB
Versions: 2
Compression:
Stored size: 1.57 KB
Contents
module Jekyll class HighlightBlock < Liquid::Block include Liquid::StandardFilters # we need a language, but the linenos argument is optional. SYNTAX = /(\w+)\s?(:?linenos)?\s?/ def initialize(tag_name, markup, tokens) super if markup =~ SYNTAX @lang = $1 if defined? $2 # additional options to pass to Albino. @options = { 'O' => 'linenos=inline' } else @options = {} end else raise SyntaxError.new("Syntax Error in 'highlight' - Valid syntax: highlight <lang> [linenos]") end end def render(context) if context.registers[:site].pygments render_pygments(context, super.join) else render_codehighlighter(context, super.join) end end def render_pygments(context, code) output = add_code_tags(Albino.new(code, @lang).to_s(@options), @lang) case context["content_type"] when "markdown" then "\n" + output + "\n" when "textile" then "<notextile>" + output + "</notextile>" else output end end def render_codehighlighter(context, code) #The div is required because RDiscount blows ass <<-HTML <div> <pre> <code class='#{@lang}'>#{h(code).strip}</code> </pre> </div> HTML end def add_code_tags(code, lang) # Add nested <code> tags to code blocks code = code.sub(/<pre>/,'<pre><code class="' + lang + '">') code = code.sub(/<\/pre>/,"</code></pre>") end end end Liquid::Template.register_tag('highlight', Jekyll::HighlightBlock)
Version data entries
2 entries across 2 versions & 2 rubygems
Version | Path |
---|---|
jberkel-jekyll-0.5.7 | lib/jekyll/tags/highlight.rb |
nirvdrum-jekyll-0.7.0 | lib/jekyll/tags/highlight.rb |