class Jekyll::Tags::HighlightBlock
def add_code_tag(code)
def add_code_tag(code) code_attributes = [ "class=\"language-#{@lang.to_s.tr("+", "-")}\"", "data-lang=\"#{@lang}\"", ].join(" ") "<figure class=\"highlight\"><pre><code #{code_attributes}>" \ "#{code.chomp}</code></pre></figure>" end
def initialize(tag_name, markup, tokens)
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
def parse_options(input)
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
def render(context)
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
def render_codehighlighter(code)
def render_codehighlighter(code) h(code).strip end
def render_pygments(code, _context)
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
def render_rouge(code)
def render_rouge(code) require "rouge" formatter = ::Rouge::Formatters::HTML.new if @highlight_options[:linenos] formatter = ::Rouge::Formatters::HTMLTable.new( formatter, { :css_class => "highlight", :gutter_class => "gutter", :code_class => "code", } ) end lexer = ::Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText formatter.format(lexer.lex(code)) end