class Jekyll::Tags::HighlightBlock

Constants

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]
MSG
        end
      end

Public Instance Methods

render(context) click to toggle source
Calls superclass method
# 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) click to toggle source
# 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

Private Instance Methods

add_code_tag(code) click to toggle source
# File lib/jekyll/tags/highlight.rb, line 130
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
parse_options(input) click to toggle source
# File lib/jekyll/tags/highlight.rb, line 70
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 && 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 126
def render_codehighlighter(code)
  h(code).strip
end
render_pygments(code, is_safe) click to toggle source
# File lib/jekyll/tags/highlight.rb, line 89
      def render_pygments(code, is_safe)
        Jekyll::External.require_with_graceful_fail("pygments") unless defined?(Pygments)

        highlighted_code = Pygments.highlight(
          code,
          :lexer   => @lang,
          :options => sanitized_opts(@highlight_options, is_safe)
        )

        if highlighted_code.nil?
          Jekyll.logger.error <<-MSG
There was an error highlighting your code:

#{code}

While attempting to convert the above code, Pygments.rb returned an unacceptable value.
This is usually a timeout problem solved by running `jekyll build` again.
MSG
          raise ArgumentError, "Pygments.rb returned an unacceptable value "\
          "when attempting to highlight some code."
        end

        highlighted_code.sub('<div class="highlight"><pre>', "").sub("</pre></div>", "")
      end
render_rouge(code) click to toggle source
# File lib/jekyll/tags/highlight.rb, line 114
def render_rouge(code)
  formatter = Jekyll::Utils::Rouge.html_formatter(
    :line_numbers => @highlight_options[:linenos],
    :wrap         => false,
    :css_class    => "highlight",
    :gutter_class => "gutter",
    :code_class   => "code"
  )
  lexer = ::Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText
  formatter.format(lexer.lex(code))
end