Sha256: 4f95e14fff4b9e6b24cf53c0930984929da71cea6857191ec364ca0fa83aa321

Contents?: true

Size: 1.43 KB

Versions: 43

Compression:

Stored size: 1.43 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.to_s)
      else
        render_codehighlighter(context, super.to_s)
      end
    end

    def render_pygments(context, code)
      if context["content_type"] == "markdown"
        return "\n" + Albino.new(code, @lang).to_s(@options) + "\n"
      elsif context["content_type"] == "textile"
        return "<notextile>" + Albino.new(code, @lang).to_s(@options) + "</notextile>"
      else
        return Albino.new(code, @lang).to_s(@options)
      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
  end

end

Liquid::Template.register_tag('highlight', Jekyll::HighlightBlock)

Version data entries

43 entries across 43 versions & 17 rubygems

Version Path
Chrononaut-hyde-0.1.0 lib/jekyll/tags/highlight.rb
Chrononaut-hyde-0.1.1 lib/jekyll/tags/highlight.rb
PerfectlyNormal-jekyll-0.5.1 lib/jekyll/tags/highlight.rb
PerfectlyNormal-jekyll-0.5.3 lib/jekyll/tags/highlight.rb
codeslinger-jekyll-0.5.1 lib/jekyll/tags/highlight.rb
codeslinger-jekyll-0.5.2 lib/jekyll/tags/highlight.rb
codeslinger-jekyll-0.5.3 lib/jekyll/tags/highlight.rb
codeslinger-jekyll-0.5.4 lib/jekyll/tags/highlight.rb
jroes-jekyll-0.5.1 lib/jekyll/tags/highlight.rb
jroes-jekyll-0.5.2 lib/jekyll/tags/highlight.rb
jroes-jekyll-0.5.3 lib/jekyll/tags/highlight.rb
jroes-jekyll-0.5.4 lib/jekyll/tags/highlight.rb
mojombo-jekyll-0.5.1 lib/jekyll/tags/highlight.rb
mojombo-jekyll-0.5.2 lib/jekyll/tags/highlight.rb
mojombo-jekyll-0.5.3 lib/jekyll/tags/highlight.rb
nirvdrum-jekyll-0.6.0 lib/jekyll/tags/highlight.rb
pol-hyde-0.1.2 lib/jekyll/tags/highlight.rb
pol-hyde-0.1.3 lib/jekyll/tags/highlight.rb
qrush-jekyll-0.5.1 lib/jekyll/tags/highlight.rb
rfelix-jekyll-0.5.4 lib/jekyll/tags/highlight.rb