Class: Jekyll::HighlightBlock

Inherits:
Liquid::Block
  • Object
show all
Includes:
Liquid::StandardFilters
Defined in:
lib/jekyll/tags/highlight.rb

Constant Summary collapse

SYNTAX =

The regular expression syntax checker. Start with the language specifier. Follow that by zero or more space separated options that take one of two forms:

  1. name

  2. name=value

/^([a-zA-Z0-9.+#-]+)((\s+\w+(=\w+)?)*)$/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ HighlightBlock

Returns a new instance of HighlightBlock.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jekyll/tags/highlight.rb', line 14

def initialize(tag_name, markup, tokens)
  super
  if markup.strip =~ SYNTAX
    @lang = $1
    if defined?($2) && $2 != ''
      tmp_options = {}
      $2.split.each do |opt|
        key, value = opt.split('=')
        if value.nil?
          if key == 'linenos'
            value = 'inline'
          else
            value = true
          end
        end
        tmp_options[key] = value
      end
      tmp_options = tmp_options.to_a.sort.collect { |opt| opt.join('=') }
      # additional options to pass to Albino
      @options = { 'O' => tmp_options.join(',') }
    else
      @options = {}
    end
  else
    raise SyntaxError.new("Syntax Error in 'highlight' - Valid syntax: highlight <lang> [linenos]")
  end
end

Instance Method Details

#add_code_tags(code, lang) ⇒ Object



66
67
68
69
70
# File 'lib/jekyll/tags/highlight.rb', line 66

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

#render(context) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/jekyll/tags/highlight.rb', line 42

def render(context)
  if context.registers[:site].pygments
    render_pygments(context, super)
  else
    render_codehighlighter(context, super)
  end
end

#render_codehighlighter(context, code) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/jekyll/tags/highlight.rb', line 57

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

#render_pygments(context, code) ⇒ Object



50
51
52
53
54
55
# File 'lib/jekyll/tags/highlight.rb', line 50

def render_pygments(context, code)
  output = add_code_tags(Albino.new(code, @lang).to_s(@options), @lang)
  output = context["pygments_prefix"] + output if context["pygments_prefix"]
  output = output + context["pygments_suffix"] if context["pygments_suffix"]
  output
end