Created
October 14, 2009 18:59
-
-
Save sgruhier/210316 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PDoc | |
module Generators | |
module Html | |
CODE_BLOCK_REGEXP = /(?:\n\n|\A)(?:\s{4,}lang(?:uage)?:\s*(\w+)\s*\n)?((?:\s{4}.*\n*)+)(^\s{0,3}\S|\z)?/ | |
attr_reader :default_language, :highlighter | |
class SyntaxHighlighter | |
def initialize(options = {}) | |
@default_language = options[:default_language] ||= :javascript | |
@highlighter = options[:highlighter] ||= :none | |
end | |
def parse(input) | |
input.gsub(CODE_BLOCK_REGEXP) do |block| | |
language, codeblock, remainder = $1, $2, $3 | |
codeblock = codeblock.gsub(/^.{4}/, '').rstrip | |
"\n\n#{highlight_block(codeblock, language)}\n#{remainder}" | |
end | |
end | |
def highlight_block(code, language) | |
language = :javascript if language.nil? || language.empty? | |
case @highlighter | |
when :none | |
"<pre><code class=\"#{language}\">#{code}</code></pre>" | |
when :coderay | |
require 'coderay' | |
CodeRay.scan(code, language).div | |
when :ultraviolet | |
require 'uv' | |
Uv.parse(code, "xhtml", language.to_s, false, "idle") | |
when :pygments | |
require 'albino' | |
Albino.new(code, language).colorize | |
else | |
raise "Requested unsupported syntax highlighter: #{highlighter}" | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment