Class: PDoc::Generators::Html::SyntaxHighlighter
- Inherits:
-
Object
- Object
- PDoc::Generators::Html::SyntaxHighlighter
- Defined in:
- lib/pdoc/generators/html/syntax_highlighter.rb
Constant Summary collapse
- CODE_BLOCK_REGEXP =
/(?:\n\n|\A)(?:\s{4,}lang(?:uage)?:\s*(\w+)\s*\n)?((?:\s{4}.*\n*)+)(^\s{0,3}\S|\z)?/
Instance Attribute Summary collapse
-
#highlighter ⇒ Object
readonly
Returns the value of attribute highlighter.
Instance Method Summary collapse
- #highlight_block(code, language) ⇒ Object
-
#initialize(h = nil) ⇒ SyntaxHighlighter
constructor
A new instance of SyntaxHighlighter.
- #parse(input) ⇒ Object
Constructor Details
#initialize(h = nil) ⇒ SyntaxHighlighter
Returns a new instance of SyntaxHighlighter.
9 10 11 |
# File 'lib/pdoc/generators/html/syntax_highlighter.rb', line 9 def initialize(h = nil) @highlighter = h.nil? ? :none : h.to_sym end |
Instance Attribute Details
#highlighter ⇒ Object (readonly)
Returns the value of attribute highlighter.
7 8 9 |
# File 'lib/pdoc/generators/html/syntax_highlighter.rb', line 7 def highlighter @highlighter end |
Instance Method Details
#highlight_block(code, language) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pdoc/generators/html/syntax_highlighter.rb', line 21 def highlight_block(code, language) language = :javascript if language.nil? case highlighter.to_sym when :none require 'cgi' code = CGI.escapeHTML(code) "<pre><code class=\"#{language}\">#{code}</code></pre>" when :coderay require 'coderay' CodeRay.scan(code, language).div when :pygments require 'albino' Albino.new(code, language).colorize else raise "Requested unsupported syntax highlighter: #{highlighter}" end end |
#parse(input) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/pdoc/generators/html/syntax_highlighter.rb', line 13 def parse(input) input.gsub(CODE_BLOCK_REGEXP) do |block| language, codeblock, remainder = $1, $2, $3 codeblock = codeblock.gsub(/^\s{4}/, '').rstrip "\n\n#{highlight_block(codeblock, language)}\n#{remainder}" end end |