Skip to content

Instantly share code, notes, and snippets.

@samleb
Created October 21, 2009 23:39
Show Gist options
  • Select an option

  • Save samleb/215579 to your computer and use it in GitHub Desktop.

Select an option

Save samleb/215579 to your computer and use it in GitHub Desktop.
Integrate SyntaxHighlighter with CodeRay and Pygments support
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch syntax_highlighter
# Changes to be committed:
# (use "git reset HEAD^1 <file>..." to unstage)
#
# modified: lib/pdoc/generators/html/helpers.rb
# modified: lib/pdoc/generators/html/syntax_highlighter.rb
# modified: lib/pdoc/generators/html/website.rb
# modified: lib/pdoc/runner.rb
# modified: test/unit/templates/html_helpers_test.rb
#
diff --git a/lib/pdoc/generators/html/helpers.rb b/lib/pdoc/generators/html/helpers.rb
index 625ab23..e92643d 100644
--- a/lib/pdoc/generators/html/helpers.rb
+++ b/lib/pdoc/generators/html/helpers.rb
@@ -21,6 +21,7 @@ module PDoc
end
def htmlize(markdown)
+ markdown = Website.syntax_highlighter.parse(markdown)
Maruku.new(markdown).to_html
end
diff --git a/lib/pdoc/generators/html/syntax_highlighter.rb b/lib/pdoc/generators/html/syntax_highlighter.rb
index 0c9e2b9..086772a 100644
--- a/lib/pdoc/generators/html/syntax_highlighter.rb
+++ b/lib/pdoc/generators/html/syntax_highlighter.rb
@@ -1,13 +1,13 @@
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
+ CODE_BLOCK_REGEXP = /(?:\n\n|\A)(?:\s{4,}lang(?:uage)?:\s*(\w+)\s*\n)?((?:\s{4}.*\n*)+)(^\s{0,3}\S|\z)?/
+
+ attr_reader :highlighter
+
+ def initialize(highlighter = :none)
+ @highlighter = highlighter
end
def parse(input)
@@ -18,7 +18,8 @@ module PDoc
end
end
- def highlight_block(code, language = default_language)
+ def highlight_block(code, language)
+ language = :javascript if language.nil?
case highlighter
when :none
"<pre><code class=\"#{language}\">#{code}</code></pre>"
diff --git a/lib/pdoc/generators/html/website.rb b/lib/pdoc/generators/html/website.rb
index 8f24872..107edea 100644
--- a/lib/pdoc/generators/html/website.rb
+++ b/lib/pdoc/generators/html/website.rb
@@ -10,10 +10,18 @@ module PDoc
include Helpers::BaseHelper
+ class << Website
+ attr_accessor :syntax_highlighter
+ def syntax_highlighter
+ @syntax_highlighter || SyntaxHighlighter.new
+ end
+ end
+
def initialize(parser_output, options = {})
super
- @templates_directory = options[:templates] || TEMPLATES_DIRECTORY
- @index_page = options[:index_page]
+ @templates_directory = File.expand_path(options[:templates] || TEMPLATES_DIRECTORY)
+ @index_page = File.expand_path(options[:index_page])
+ Website.syntax_highlighter = SyntaxHighlighter.new(options[:syntax_highlighter])
load_custom_helpers
end
diff --git a/lib/pdoc/runner.rb b/lib/pdoc/runner.rb
index 47f6d5e..50af039 100644
--- a/lib/pdoc/runner.rb
+++ b/lib/pdoc/runner.rb
@@ -1,13 +1,12 @@
module PDoc
class Runner
def initialize(*source_files)
- options = source_files.last.is_a?(Hash) ? source_files.pop : {}
- @source_files = source_files
- @output_directory = File.expand_path(options[:output] || OUTPUT_DIR)
- @templates_directory = options[:templates] && File.expand_path(options[:templates])
- @index_page = options[:index_page] && File.expand_path(options[:index_page])
- @generator = options[:generator] || Generators::Html::Website
- @parser = Parser.new(source)
+ options = source_files.last.is_a?(Hash) ? source_files.pop.dup : {}
+ @source_files = source_files
+ @output_directory = File.expand_path(options.delete(:output) || OUTPUT_DIR)
+ @generator = options.delete(:generator) || Generators::Html::Website
+ @parser = Parser.new(source)
+ @options = options
end
def source
@@ -34,10 +33,7 @@ module PDoc
def render
log "Generating documentation to: #{@output_directory}.\n\n"
start_time = Time.new
- @generator.new(@parser_output, {
- :templates => @templates_directory,
- :index_page => @index_page
- }).render(@output_directory)
+ @generator.new(@parser_output, @options).render(@output_directory)
log "\c[[F Documentation generated in #{Time.new - start_time} seconds."
end
diff --git a/test/unit/templates/html_helpers_test.rb b/test/unit/templates/html_helpers_test.rb
index ccb4597..bc99975 100644
--- a/test/unit/templates/html_helpers_test.rb
+++ b/test/unit/templates/html_helpers_test.rb
@@ -19,6 +19,18 @@ class HtmlHelpersTest < Test::Unit::TestCase
end
end
+ def test_htmlize_syntax_highlight
+ doc = parse_doc(<<-DOC)
+Element#foo() -> Element
+
+Example:
+
+ var foo = "bar";
+DOC
+ html = run_helper(:htmlize, doc, doc.to_a.last.description)
+ assert_equal("<p>Example:</p>\n<pre><code class='javascript'>var foo = \"bar\";</code></pre>", html)
+ end
+
def test_auto_link
html = run_helper(:auto_link, 'Element')
assert_match %r(<a.*?>Element</a>), html, 'Simple namespace not linked'
@@ -86,7 +98,7 @@ class HtmlHelpersTest < Test::Unit::TestCase
**/
/**
-#{source.strip.map { |line| " * #{line.lstrip}" }}
+#{source.strip.map { |line| " * #{line}" }}
**/
)
PDoc::Parser.new(src).parse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment