Class: Bridgetown::Tags::HighlightBlock

Inherits:
Liquid::Block
  • Object
show all
Includes:
Liquid::StandardFilters
Defined in:
bridgetown-core/lib/bridgetown-core/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=”"

is a space-separated list of numbers
%r!^([a-zA-Z0-9.+#_-]+)((\s+\w+(=(\w+|"([0-9]+\s)*[0-9]+"))?)*)$!
LEADING_OR_TRAILING_LINE_TERMINATORS =
%r!\A(\n|\r)+|(\n|\r)+\z!

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 'bridgetown-core/lib/bridgetown-core/tags/highlight.rb', line 15

def initialize(tag_name, markup, tokens)
  super
  unless markup.strip =~ SYNTAX
    raise SyntaxError, <<~MSG
      Syntax Error in tag 'highlight' while parsing the following markup:

      #{markup}

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

  @lang = Regexp.last_match(1).downcase
  @highlight_options = parse_options(Regexp.last_match(2))
end

Instance Method Details

#render(context) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'bridgetown-core/lib/bridgetown-core/tags/highlight.rb', line 33

def render(context)
  prefix = context["highlighter_prefix"] || ""
  suffix = context["highlighter_suffix"] || ""
  code = super.to_s.gsub(LEADING_OR_TRAILING_LINE_TERMINATORS, "")

  output =
    case context.registers[:site].config.highlighter
    when "rouge"
      render_rouge(code)
    else
      h(code).strip
    end

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