Module: RedclothCoderay

Defined in:
lib/redclothcoderay.rb

Constant Summary collapse

SINGLE_LINE =
'<code class="inline_code">%s</code>'
MULTI_LINE =
'<div class="multiline_code">%s</div>'
SOURCE_TAG_REGEXP =
/(([\t\n])?<source(?:\:([a-z]+))?>(.+?)<\/source>([\t\n])?)/m
CODERAY_OPTIONS =
{:wrap => nil, :css => :class}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.coderay_options(options) ⇒ Object



28
29
30
# File 'lib/redclothcoderay.rb', line 28

def self.coderay_options(options)
  CODERAY_OPTIONS.replace(options)
end

Instance Method Details

#preprocess_with_syntax_highlighting(text) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/redclothcoderay.rb', line 7

def preprocess_with_syntax_highlighting(text)
  text.gsub(SOURCE_TAG_REGEXP) do |m|
    all_of_it = $~[1]
    whitespace_before = $~[2]
    lang = ($~[3] || :ruby).to_sym
    code = $~[4].strip
    whitespace_after = $~[5]

    wrap_in = all_of_it =~ /\n/ ? MULTI_LINE : SINGLE_LINE
    if all_of_it =~ /\n/
      highlighted = wrap_in % CodeRay.scan(code, lang).div(CODERAY_OPTIONS)
    else
      options = CODERAY_OPTIONS
      options[:line_numbers] = nil
      highlighted = wrap_in % CodeRay.scan(code, lang).span(options)
    end

    "#{whitespace_before}<notextile>#{highlighted}</notextile>#{whitespace_after}"
  end
end