Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/docs
*.gem
*.rbc
.bundle
Expand Down
33 changes: 26 additions & 7 deletions lib/rocco.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,30 @@
#
# * `: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'

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
Expand Down Expand Up @@ -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(':').
Expand Down Expand Up @@ -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*<h5>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 <http://pygments.appspot.com>
Expand Down
14 changes: 10 additions & 4 deletions test/test_reported_issues.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"<p>hello w\366rld</p>\n",
"<p>hello wörld</p>\n",
r.sections[0][0],
"ISO-8859-1 input should probably also behave correctly."
)
Expand Down Expand Up @@ -83,4 +81,12 @@ def test_issue15_extra_space_after_comment_character_remains
)
assert_equal("<h2>Comment 1</h2>\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 "<span class=\"k\">def</span> <span class=\"nf\">codeblock</span>\n<span class=\"k\">end</span>", r.sections[ 0 ][ 1 ]
end
end