diff --git a/.gitignore b/.gitignore index d87d4be..86db56e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/docs *.gem *.rbc .bundle diff --git a/lib/rocco.rb b/lib/rocco.rb index 92c2c23..f1f4a6c 100644 --- a/lib/rocco.rb +++ b/lib/rocco.rb @@ -73,7 +73,10 @@ # # * `:stylesheet`, which specifies the css stylesheet to use for each # rendered template. _Defaults to `http://jashkenas.github.com/docco/resources/docco.css` -# (the original docco stylesheet) +# (the original docco stylesheet)._ +# +# * `:encoding`: specifies the encoding that input files are written in. +# _Defaults to `UTF-8`_. class Rocco VERSION = '0.8.2' @@ -81,18 +84,19 @@ def initialize(filename, sources=[], options={}) @file = filename @sources = sources - # When `block` is given, it must read the contents of the file using - # whatever means necessary and return it as a string. With no `block`, - # the file is read to retrieve data. - @data = if block_given? then yield else File.read(filename) end - @options = { :language => 'ruby', :comment_chars => '#', :template_file => nil, - :stylesheet => 'http://jashkenas.github.com/docco/resources/docco.css' + :stylesheet => 'http://jashkenas.github.com/docco/resources/docco.css', + :encoding => 'UTF-8' }.merge(options) + # When `block` is given, it must read the contents of the file using + # whatever means necessary and return it as a string. With no `block`, + # the file is read to retrieve data. + @data = if block_given? then yield else read_with_encoding(filename) end + # If we detect a language if "text" != detect_language # then assign the detected language to `:language`, and look for @@ -148,6 +152,20 @@ def to_html # Helper Functions # ---------------- + # Read *file* encoded `@options[:encoding]` into a string encoded in UTF-8. + def read_with_encoding filename + # This works differently in Ruby 1.8 and Ruby 1.9, which are + # distinguished by checking if `IO#external_encoding` exists. + if IO.method_defined?("external_encoding") + File.read(filename, :external_encoding => @options[:encoding], + :internal_encoding => "UTF-8") + else + require 'iconv' + data = File.read(filename) + Iconv.conv("UTF-8", @options[:encoding], data) + end + end + # Returns `true` if `pygmentize` is available locally, `false` otherwise. def pygmentize? @_pygmentize ||= ENV['PATH'].split(':'). @@ -380,6 +398,7 @@ def highlight(blocks) # into separate sections. markdown = docs_blocks.join("\n\n##### DIVIDER\n\n") docs_html = process_markdown(markdown).split(/\n*
DIVIDER<\/h5>\n*/m) + docs_html << "" if docs_html.empty? # Combine all code blocks into a single big stream with section dividers and # run through either `pygmentize(1)` or diff --git a/test/test_reported_issues.rb b/test/test_reported_issues.rb index 64085ca..0899fa7 100644 --- a/test/test_reported_issues.rb +++ b/test/test_reported_issues.rb @@ -28,11 +28,9 @@ def test_issue10_utf8_processing "UTF-8 input files ought behave correctly." ) # and, just for grins, ensure that iso-8859-1 works too. - # @TODO: Is this really the correct behavior? Converting text - # to UTF-8 on the way out is probably preferable. - r = Rocco.new( File.dirname(__FILE__) + "/fixtures/issue10.iso-8859-1.rb" ) + r = Rocco.new( File.dirname(__FILE__) + "/fixtures/issue10.iso-8859-1.rb", [], :encoding => 'ISO-8859-1' ) assert_equal( - "

hello w\366rld

\n", + "

hello wörld

\n", r.sections[0][0], "ISO-8859-1 input should probably also behave correctly." ) @@ -83,4 +81,12 @@ def test_issue15_extra_space_after_comment_character_remains ) assert_equal("

Comment 1

\n", r.sections[0][0]) end + + def test_issue71_empty_comment + r = roccoize( "filename.rb", "def codeblock\nend\n" ) + assert_equal 1, r.sections.length + assert_equal 2, r.sections[ 0 ].length + assert_match /\s*/, r.sections[ 0 ][ 0 ] + assert_equal "def codeblock\nend", r.sections[ 0 ][ 1 ] + end end