From 8dd30853aa818b4c3cb064eae05172f04f86bb27 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 20 Mar 2011 16:23:11 -0400 Subject: [PATCH 1/2] =?UTF-8?q?Add=20na=C3=AFve=20support=20for=20Docblock?= =?UTF-8?q?=20@annotations.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/rocco | 2 ++ lib/rocco.rb | 15 +++++++++++++++ test/test_docblock_annotations.rb | 13 +++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 test/test_docblock_annotations.rb diff --git a/bin/rocco b/bin/rocco index a56c0d3..6468a20 100755 --- a/bin/rocco +++ b/bin/rocco @@ -8,6 +8,7 @@ #/ The string to recognize as a comment marker #/ -o, --output= Directory where generated HTML files are written #/ -t, --template= The file to use as template when rendering HTML +#/ -d, --docblocks Parse Docblock @annotations in comments #/ --help Show this help message require 'optparse' @@ -39,6 +40,7 @@ ARGV.options { |o| o.on("-l", "--language=LANG") { |lang| options[:language] = lang } o.on("-c", "--comment-chars=CHARS") { |chars| options[:comment_chars] = Regexp.escape(chars) } o.on("-t", "--template=TEMPLATE") { |template| options[:template_file] = template } + o.on("-d", "--docblocks") { options[:docblocks] = true } o.on_tail("-h", "--help") { usage($stdout, 0) } o.parse! } or abort_with_note diff --git a/lib/rocco.rb b/lib/rocco.rb index 7da1322..4b8d9b2 100644 --- a/lib/rocco.rb +++ b/lib/rocco.rb @@ -368,11 +368,26 @@ class Rocco [docs_blocks, code_blocks] end + # Take a list of block comments and convert Docblock @annotations to + # Markdown syntax. + def docblock(docs) + docs.map do |doc| + doc.split("\n").map do |line| + line.match(/^@\w+/) ? line.sub(/^@(\w+)\s+/, '> **\1** ')+" " : line + end.join("\n") + end + end + # Take the result of `split` and apply Markdown formatting to comments and # syntax highlighting to source code. def highlight(blocks) docs_blocks, code_blocks = blocks + # Pre-process Docblock @annotations. + if @options[:docblocks] + docs_blocks = docblock(docs_blocks) + end + # Combine all docs blocks into a single big markdown document with section # dividers and run through the Markdown processor. Then split it back out # into separate sections. diff --git a/test/test_docblock_annotations.rb b/test/test_docblock_annotations.rb new file mode 100644 index 0000000..5fb05a6 --- /dev/null +++ b/test/test_docblock_annotations.rb @@ -0,0 +1,13 @@ +require File.expand_path('../helper', __FILE__) + +class RoccoDocblockAnnotationsTest < Test::Unit::TestCase + def test_docblock_annotation_conversion + r = Rocco.new( 'test', '', { :language => "c", :docblocks => true } ) { "" } # Generate throwaway instance so I can test `parse` + assert_equal( + [ + "Comment\n\n> **param** mixed foo \n> **return** void " + ], + r.docblock( ["Comment\n\n@param mixed foo\n@return void"] ) + ) + end +end From 3ebbb82f14196f7b0b70f79a29c28d369612968c Mon Sep 17 00:00:00 2001 From: Mike West Date: Tue, 19 Apr 2011 19:54:42 +0200 Subject: [PATCH 2/2] One more test. --- test/test_docblock_annotations.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/test_docblock_annotations.rb b/test/test_docblock_annotations.rb index 5fb05a6..41af56d 100644 --- a/test/test_docblock_annotations.rb +++ b/test/test_docblock_annotations.rb @@ -1,7 +1,7 @@ require File.expand_path('../helper', __FILE__) class RoccoDocblockAnnotationsTest < Test::Unit::TestCase - def test_docblock_annotation_conversion + def test_basics r = Rocco.new( 'test', '', { :language => "c", :docblocks => true } ) { "" } # Generate throwaway instance so I can test `parse` assert_equal( [ @@ -10,4 +10,13 @@ class RoccoDocblockAnnotationsTest < Test::Unit::TestCase r.docblock( ["Comment\n\n@param mixed foo\n@return void"] ) ) end + def test_highlighted_in_blocks + r = Rocco.new( 'test', '', { :language => "c", :docblocks => true } ) { "" } # Generate throwaway instance so I can test `parse` + highlighted = r.highlight( r.split( r.parse( "/**\n * Comment\n * @param type name\n */\ndef codeblock\nend\n" ) ) ) + + assert_equal( + "

Comment

\n\n

param type name

\n", + highlighted[0][0] + ) + end end