Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Stub out heredoc support. Needs more tests. (Issue #40)

  • Loading branch information...
commit d0690e626983c8eef432b0e3ef541300e35ec7f9 1 parent e5e0d53
@mikewest mikewest authored
Showing with 53 additions and 16 deletions.
  1. +40 −16 lib/rocco.rb
  2. +13 −0 test/test_heredoc.rb
View
56 lib/rocco.rb
@@ -212,41 +212,50 @@ def detect_language
"bash" => { :single => "#", :multi => nil },
"c" => {
:single => "//",
- :multi => { :start => "/**", :middle => "*", :end => "*/" }
+ :multi => { :start => "/**", :middle => "*", :end => "*/" },
+ :heredoc => nil
},
"coffee-script" => {
:single => "#",
- :multi => { :start => "###", :middle => nil, :end => "###" }
+ :multi => { :start => "###", :middle => nil, :end => "###" },
+ :heredoc => nil
},
"cpp" => {
:single => "//",
- :multi => { :start => "/**", :middle => "*", :end => "*/" }
+ :multi => { :start => "/**", :middle => "*", :end => "*/" },
+ :heredoc => nil
},
"css" => {
:single => nil,
- :multi => { :start => "/**", :middle => "*", :end => "*/" }
+ :multi => { :start => "/**", :middle => "*", :end => "*/" },
+ :heredoc => nil
},
"java" => {
:single => "//",
- :multi => { :start => "/**", :middle => "*", :end => "*/" }
+ :multi => { :start => "/**", :middle => "*", :end => "*/" },
+ :heredoc => nil
},
"js" => {
:single => "//",
- :multi => { :start => "/**", :middle => "*", :end => "*/" }
+ :multi => { :start => "/**", :middle => "*", :end => "*/" },
+ :heredoc => nil
},
"lua" => {
:single => "--",
- :multi => nil
+ :multi => nil,
+ :heredoc => nil
},
"python" => {
:single => "#",
- :multi => { :start => '"""', :middle => nil, :end => '"""' }
+ :multi => { :start => '"""', :middle => nil, :end => '"""' },
+ :heredoc => nil
},
"rb" => {
:single => "#",
- :multi => { :start => '=begin', :middle => nil, :end => '=end' }
+ :multi => { :start => '=begin', :middle => nil, :end => '=end' },
+ :heredoc => "<<-"
},
- "scheme" => { :single => ";;", :multi => nil },
+ "scheme" => { :single => ";;", :multi => nil, :heredoc => nil },
}
def generate_comment_chars
@@ -254,7 +263,7 @@ def generate_comment_chars
if COMMENT_STYLES[@options[:language]]
COMMENT_STYLES[@options[:language]]
else
- { :single => @options[:comment_chars], :multi => nil }
+ { :single => @options[:comment_chars], :multi => nil, :heredoc => nil }
end
end
@@ -279,8 +288,9 @@ def parse(data)
# To detect both block comments and single-line comments, we'll set
# up a tiny state machine, and loop through each line of the file.
# This requires an `in_comment_block` boolean, and a few regular
- # expressions for line tests.
+ # expressions for line tests. We'll do the same for fake heredoc parsing.
in_comment_block = false
+ in_heredoc = false
single_line_comment, block_comment_start, block_comment_mid, block_comment_end =
nil, nil, nil, nil
if not @options[:comment_chars][:single].nil?
@@ -293,6 +303,9 @@ def parse(data)
block_comment_mid = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:middle])}\\s?")
end
end
+ if not @options[:comment_chars][:heredoc].nil?
+ heredoc_start = Regexp.new("#{Regexp.escape(@options[:comment_chars][:heredoc])}(\\S+)$")
+ end
lines.each do |line|
# If we're currently in a comment block, check whether the line matches
# the _end_ of a comment block.
@@ -302,11 +315,22 @@ def parse(data)
else
docs << line.sub( block_comment_mid || '', '' )
end
- # Otherwise, check whether the line matches the beginning of a block, or
- # a single-line comment all on it's lonesome. In either case, if there's
- # code, start a new section
+ # If we're currently in a heredoc, we're looking for the end of the
+ # heredoc, and everything it contains is code.
+ elsif in_heredoc
+ if line.match(Regexp.new("^#{Regexp.escape(in_heredoc)}$"))
+ in_heredoc = false
+ end
+ code << line
+ # Otherwise, check whether the line starts a heredoc. If so, note the end
+ # pattern, and the line is code. Otherwise check whether the line matches
+ # the beginning of a block, or a single-line comment all on it's lonesome.
+ # In either case, if there's code, start a new section.
else
- if block_comment_start && line.match( block_comment_start )
+ if heredoc_start && line.match( heredoc_start )
+ in_heredoc = $1
+ code << line
+ elsif block_comment_start && line.match( block_comment_start )
in_comment_block = true
if code.any?
sections << [docs, code]
View
13 test/test_heredoc.rb
@@ -0,0 +1,13 @@
+require File.expand_path('../helper', __FILE__)
+
+class RoccoBlockCommentTest < Test::Unit::TestCase
+ def test_basics
+ r = Rocco.new( 'test', '', { :language => "rb" } ) { "" } # Generate throwaway instance so I can test `parse`
+ assert_equal(
+ [
+ [ [ "Comment 1" ], [ "heredoc <<-EOH", "#comment", "code", "EOH" ] ]
+ ],
+ r.parse( "# Comment 1\nheredoc <<-EOH\n#comment\ncode\nEOH" )
+ )
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.