Class: Nanoc::Filters::ColorizeSyntax::Colorizers::RougeColorizer Private

Inherits:
Abstract
  • Object
show all
Defined in:
lib/nanoc/filters/colorize_syntax/colorizers.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#postprocess(_language, element) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/nanoc/filters/colorize_syntax/colorizers.rb', line 141

def postprocess(_language, element)
  # Removes the double wrapping.
  #
  # Before:
  #
  #   <pre><code class="language-ruby"><pre class="highlight"><code>
  #
  # After:
  #
  #   <pre><code class="language-ruby highlight">

  return if element.name != 'pre'

  code1 = element.xpath('code').first
  return if code1.nil?

  div = code1.xpath('div').first

  # For Rouge 2.x and 1.x, respectively
  pre = (div || code1).xpath('pre').first
  return if pre.nil?

  code2 = pre.xpath('code').first
  return if code2.nil?

  code1.inner_html = code2.inner_html
  code1['class'] = [code1['class'], pre['class']].compact.join(' ')
end

#process(code, language, params = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/nanoc/filters/colorize_syntax/colorizers.rb', line 120

def process(code, language, params = {})
  require 'rouge'

  if params.fetch(:legacy, false)
    formatter_options = {
      css_class: params.fetch(:css_class, 'highlight'),
      inline_theme: params.fetch(:inline_theme, nil),
      line_numbers: params.fetch(:line_numbers, false),
      start_line: params.fetch(:start_line, 1),
      wrap: params.fetch(:wrap, false),
    }
    formatter_cls = Rouge::Formatters::HTMLLegacy
    formatter = formatter_cls.new(formatter_options)
  else
    formatter = params.fetch(:formatter, Rouge::Formatters::HTML.new)
  end

  lexer = Rouge::Lexer.find_fancy(language, code) || Rouge::Lexers::PlainText
  formatter.format(lexer.lex(code))
end