From d067210faaac21718d74d22c3e65c7e8765f9173 Mon Sep 17 00:00:00 2001 From: Mike West Date: Sun, 21 Nov 2010 16:53:22 +0100 Subject: [PATCH] Refactoring comment_char internals: prepping for block comments --- lib/rocco.rb | 18 ++++++++++++------ test/test_commentchar_detection.rb | 10 +++++----- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/rocco.rb b/lib/rocco.rb index 3c77ece..664169d 100644 --- a/lib/rocco.rb +++ b/lib/rocco.rb @@ -100,19 +100,25 @@ class Rocco if detect_language() != "text" # then assign the detected language to `:language`, and look for # comment characters based on that language - @options[:language] = detect_language() + @options[:language] = detect_language() @options[:comment_chars] = generate_comment_chars() # If we didn't detect a language, but the user provided one, use it # to look around for comment characters to override the default. elsif @options[:language] != defaults[:language] @options[:comment_chars] = generate_comment_chars() + + # If neither is true, then convert the default comment character string + # into the comment_char syntax (we'll discuss that syntax in detail when + # we get to `generate_comment_chars()` in a moment. + else + @options[:comment_chars] = { :single => @options[:comment_chars], :multi => "" } end # Turn `:comment_chars` into a regex matching a series of spaces, the # `:comment_chars` string, and the an optional space. We'll use that # to detect single-line comments. - @comment_pattern = Regexp.new("^\\s*#{@options[:comment_chars]}\s?") + @comment_pattern = Regexp.new("^\\s*#{@options[:comment_chars][:single]}\s?") # `parse()` the file contents stored in `@data`. Run the result through `split()` # and that result through `highlight()` to generate the final section list. @@ -214,9 +220,9 @@ class Rocco } if comment_styles[language] - comment_styles[language][:single] + comment_styles[language] else - @options[:comment_chars] + { :single => @options[:comment_chars], :multi => nil } end end end @@ -281,7 +287,7 @@ class Rocco # Combine all code blocks into a single big stream and run through either # `pygmentize(1)` or - code_stream = code_blocks.join("\n\n#{@options[:comment_chars]} DIVIDER\n\n") + code_stream = code_blocks.join("\n\n#{@options[:comment_chars][:single]} DIVIDER\n\n") if pygmentize? code_html = highlight_pygmentize(code_stream) @@ -292,7 +298,7 @@ class Rocco # Do some post-processing on the pygments output to split things back # into sections and remove partial `
` blocks.
     code_html = code_html.
-      split(/\n*#{@options[:comment_chars]} DIVIDER<\/span>\n*/m).
+      split(/\n*#{@options[:comment_chars][:single]} DIVIDER<\/span>\n*/m).
       map { |code| code.sub(/\n?
/m, '') }.
       map { |code| code.sub(/\n?<\/pre><\/div>\n/m, '') }
 
diff --git a/test/test_commentchar_detection.rb b/test/test_commentchar_detection.rb
index 1c87196..efb4ee2 100644
--- a/test/test_commentchar_detection.rb
+++ b/test/test_commentchar_detection.rb
@@ -3,22 +3,22 @@ require File.dirname(__FILE__) + '/helper'
 class RoccoAutomaticCommentChars < Test::Unit::TestCase
     def test_basic_detection
         r = Rocco.new( 'filename.js' ) { "" }
-        assert_equal "//", r.options[:comment_chars]
+        assert_equal "//", r.options[:comment_chars][:single]
     end
     def test_fallback_language
         r = Rocco.new( 'filename.an_extension_with_no_meaning_whatsoever', '', { :language => "js" } ) { "" }
-        assert_equal "//", r.options[:comment_chars]
+        assert_equal "//", r.options[:comment_chars][:single]
     end
     def test_fallback_default
         r = Rocco.new( 'filename.an_extension_with_no_meaning_whatsoever' ) { "" }
-        assert_equal "#", r.options[:comment_chars], "`:comment_chars` should be `#` when falling back to defaults."
+        assert_equal "#", r.options[:comment_chars][:single], "`:comment_chars` should be `#` when falling back to defaults."
     end
     def test_fallback_user
         r = Rocco.new( 'filename.an_extension_with_no_meaning_whatsoever', '', { :comment_chars => "user" } ) { "" }
-        assert_equal "user", r.options[:comment_chars], "`:comment_chars` should be the user's default when falling back to user-provided settings."
+        assert_equal "user", r.options[:comment_chars][:single], "`:comment_chars` should be the user's default when falling back to user-provided settings."
     end
     def test_fallback_user_with_unknown_language
         r = Rocco.new( 'filename.an_extension_with_no_meaning_whatsoever', '', { :language => "not-a-language", :comment_chars => "user" } ) { "" }
-        assert_equal "user", r.options[:comment_chars], "`:comment_chars` should be the user's default when falling back to user-provided settings."
+        assert_equal "user", r.options[:comment_chars][:single], "`:comment_chars` should be the user's default when falling back to user-provided settings."
     end
 end