Class: Gollum::Filter::Code
- Inherits:
-
Gollum::Filter
- Object
- Gollum::Filter
- Gollum::Filter::Code
- Defined in:
- lib/gollum-lib/filter/code.rb
Overview
Code
Render a block of code using the Rouge syntax-highlighter.
Instance Method Summary collapse
- #extract(data) ⇒ Object
-
#process(data) ⇒ Object
Process all code from the codemap and replace the placeholders with the final HTML.
Methods inherited from Gollum::Filter
Methods included from Helpers
Constructor Details
This class inherits a constructor from Gollum::Filter
Instance Method Details
#extract(data) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gollum-lib/filter/code.rb', line 7 def extract(data) return data if @markup.format == :txt data.gsub!(/^([ \t]*)(~~~+) ?([^\r\n]+)?\r?\n(.+?)\r?\n\1(~~~+)[ \t\r]*$/m) do m_indent = $1 m_start = $2 # ~~~ m_lang = $3 m_code = $4 m_end = $5 # ~~~ # start and finish tilde fence must be the same length next '' if m_start.length != m_end.length lang = m_lang ? m_lang.strip : nil id = Digest::SHA1.hexdigest("#{lang}.#{m_code}") cached = @markup.check_cache(:code, id) # extract lang from { .ruby } or { #stuff .ruby .indent } # see http://johnmacfarlane.net/pandoc/README.html#delimited-code-blocks if lang lang = lang.match(/\.([^}\s]+)/) lang = lang[1] unless lang.nil? end @map[id] = cached ? { :output => cached } : { :lang => lang, :code => m_code, :indent => m_indent } "#{m_indent}#{id}" # print the SHA1 ID with the proper indentation end data.gsub!(/^([ \t]*)``` ?([^\r\n]+)?\r?\n(.+?)\r?\n\1```[ \t]*\r?$/m) do lang = $2 ? $2.strip : nil id = Digest::SHA1.hexdigest("#{lang}.#{$3}") cached = @markup.check_cache(:code, id) @map[id] = cached ? { :output => cached } : { :lang => lang, :code => $3, :indent => $1 } "#{$1}#{id}" # print the SHA1 ID with the proper indentation end data end |
#process(data) ⇒ Object
Process all code from the codemap and replace the placeholders with the final HTML.
data - The String data (with placeholders). encoding - Encoding Constant or String.
Returns the marked up String data.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/gollum-lib/filter/code.rb', line 58 def process(data) return data if data.nil? || data.size.zero? || @map.size.zero? blocks = [] @map.each do |id, spec| next if spec[:output] # cached code = spec[:code] remove_leading_space(code, /^#{spec[:indent]}/m) remove_leading_space(code, /^( |\t)/m) blocks << [spec[:lang], code] end highlighted = [] blocks.each do |lang, code| encoding = @markup.encoding || 'utf-8' if defined? Pygments # treat unknown and bash as standard pre tags if !lang || lang.downcase == 'bash' hl_code = "<pre>#{code}</pre>" else # must set startinline to true for php to be highlighted without <? hl_code = Pygments.highlight(code, :lexer => lang, :options => { :encoding => encoding.to_s, :startinline => true }) end else # Rouge begin # if `lang` was not defined then assume plaintext # if `lang` is defined but cannot be found then wrap it and escape it lang ||= 'plaintext' if Rouge::Lexer.find(lang).nil? lexer = Rouge::Lexers::PlainText.new formatter = Rouge::Formatters::HTML.new(:wrap => false) hl_code = formatter.format(lexer.lex(code)) hl_code = "<pre class='highlight'><span class='err'>#{CGI.escapeHTML(hl_code)}</span></pre>" else hl_code = Rouge.highlight(code, lang, 'html') end rescue hl_code = code end end highlighted << hl_code end @map.each do |id, spec| body = spec[:output] || begin if (body = highlighted.shift.to_s).size > 0 @markup.update_cache(:code, id, body) body else "<pre><code>#{CGI.escapeHTML(spec[:code])}</code></pre>" end end # Removes paragraph tags surrounding <pre> blocks, see issue https://github.com/gollum/gollum-lib/issues/97 data.gsub!(/(<p>#{id}<\/p>|#{id})/) do body end end data end |