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*
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("