Class: Jekyll::Tags::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 three forms: name, name=value, or name=“<quoted list>”

<quoted list> is a space-separated list of numbers

%r!^([a-zA-Z0-9.+#_-]+)((\s+\w+(=(\w+|"([0-9]+\s)*[0-9]+"))?)*)$!

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ HighlightBlock

Returns a new instance of HighlightBlock.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jekyll/tags/highlight.rb', line 15

def initialize(tag_name, markup, tokens)
  super
  if markup.strip =~ SYNTAX
    @lang = Regexp.last_match(1).downcase
    @highlight_options = parse_options(Regexp.last_match(2))
  else
    raise SyntaxError, <<-MSG
Syntax Error in tag 'highlight' while parsing the following markup:

  #{markup}

Valid syntax: highlight <lang> [linenos]
MSG
  end
end

Instance Method Details

#render(context) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jekyll/tags/highlight.rb', line 31

def render(context)
  prefix = context["highlighter_prefix"] || ""
  suffix = context["highlighter_suffix"] || ""
  code = super.to_s.gsub(%r!\A(\n|\r)+|(\n|\r)+\z!, "")

  is_safe = !!context.registers[:site].safe

  output =
    case context.registers[:site].highlighter
    when "pygments"
      render_pygments(code, is_safe)
    when "rouge"
      render_rouge(code)
    else
      render_codehighlighter(code)
    end

  rendered_output = add_code_tag(output)
  prefix + rendered_output + suffix
end

#sanitized_opts(opts, is_safe) ⇒ Object



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

def sanitized_opts(opts, is_safe)
  if is_safe
    Hash[[
      [:startinline, opts.fetch(:startinline, nil)],
      [:hl_lines,    opts.fetch(:hl_lines, nil)],
      [:linenos,     opts.fetch(:linenos, nil)],
      [:encoding,    opts.fetch(:encoding, "utf-8")],
      [:cssclass,    opts.fetch(:cssclass, nil)],
    ].reject { |f| f.last.nil? }]
  else
    opts
  end
end