class Jekyll::Tags::HighlightBlock
Constants
- LEADING_OR_TRAILING_LINE_TERMINATORS
- OPTIONS_REGEX
- 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
Public Class Methods
new(tag_name, markup, tokens)
click to toggle source
Calls superclass method
# 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] [mark_lines="3 4 5"] See https://jekyllrb.com/docs/liquid/tags/#code-snippet-highlighting for more details. MSG end end
Public Instance Methods
render(context)
click to toggle source
Calls superclass method
# File lib/jekyll/tags/highlight.rb, line 35 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].highlighter when "rouge" render_rouge(code) when "pygments" render_pygments(code, context) else render_codehighlighter(code) end rendered_output = add_code_tag(output) prefix + rendered_output + suffix end
Private Instance Methods
add_code_tag(code)
click to toggle source
# File lib/jekyll/tags/highlight.rb, line 121 def add_code_tag(code) code_attrs = %(class="language-#{@lang.tr("+", "-")}" data-lang="#{@lang}") %(<figure class="highlight"><pre><code #{code_attrs}>#{code.chomp}</code></pre></figure>) end
line_highlighter_formatter(formatter)
click to toggle source
# File lib/jekyll/tags/highlight.rb, line 93 def line_highlighter_formatter(formatter) Rouge::Formatters::HTMLLineHighlighter.new( formatter, :highlight_lines => mark_lines ) end
mark_lines()
click to toggle source
# File lib/jekyll/tags/highlight.rb, line 100 def mark_lines value = @highlight_options[:mark_lines] return value.map(&:to_i) if value.is_a?(Array) raise SyntaxError, "Syntax Error for mark_lines declaration. Expected a " \ "double-quoted list of integers." end
parse_options(input)
click to toggle source
# File lib/jekyll/tags/highlight.rb, line 58 def parse_options(input) options = {} return options if input.empty? # Split along 3 possible forms -- key="<quoted list>", key=value, or key input.scan(OPTIONS_REGEX) do |opt| key, value = opt.split("=") # If a quoted list, convert to array if value&.include?('"') value.delete!('"') value = value.split end options[key.to_sym] = value || true end options[:linenos] = "inline" if options[:linenos] == true options end
render_codehighlighter(code)
click to toggle source
# File lib/jekyll/tags/highlight.rb, line 117 def render_codehighlighter(code) h(code).strip end
render_pygments(code, _context)
click to toggle source
# File lib/jekyll/tags/highlight.rb, line 77 def render_pygments(code, _context) Jekyll.logger.warn "Warning:", "Highlight Tag no longer supports rendering with Pygments." Jekyll.logger.warn "", "Using the default highlighter, Rouge, instead." render_rouge(code) end
render_rouge(code)
click to toggle source
# File lib/jekyll/tags/highlight.rb, line 83 def render_rouge(code) require "rouge" formatter = Rouge::Formatters::HTML.new formatter = line_highlighter_formatter(formatter) if @highlight_options[:mark_lines] formatter = table_formatter(formatter) if @highlight_options[:linenos] lexer = Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText formatter.format(lexer.lex(code)) end
table_formatter(formatter)
click to toggle source
# File lib/jekyll/tags/highlight.rb, line 108 def table_formatter(formatter) Rouge::Formatters::HTMLTable.new( formatter, :css_class => "highlight", :gutter_class => "gutter", :code_class => "code" ) end