Skip to content
Browse files

Successfully parse and normalize simple single-line blocks

  • Loading branch information...
1 parent 04a38c3 commit 82c85ab7966eac48b0ab50a191cc0b1c025e9067 @kneath committed Feb 6, 2012
Showing with 11 additions and 4 deletions.
  1. +11 −4 lib/kss/comment_parser.rb
View
15 lib/kss/comment_parser.rb
@@ -40,11 +40,11 @@ def self.parse_multi_line(line)
# file_path - The location of the file to parse as a String.
# options - Optional options hash.
# :preserve_whitespace - Preserve the whitespace before/after comment
- # markers (default:true).
+ # markers (default:false).
#
def initialize(file_path, options={})
@options = options
- @options[:preserve_whitespace] = true if @options[:preserve_whitespace].nil?
+ @options[:preserve_whitespace] = false if @options[:preserve_whitespace].nil?
@file_path = file_path
@blocks = []
@parsed = false
@@ -97,7 +97,6 @@ def parse_blocks
@blocks << normalize(current_block) if inside_single_line_block || inside_multi_line_block
inside_single_line_block = false
- inside_multi_line_block = false
current_block = nil
end
end
@@ -114,7 +113,15 @@ def parse_blocks
# Returns a String of normalized text.
def normalize(text_block)
return text_block if @options[:preserve_whitespace]
- text_block
+
+ # Strip consistent indenting by measuring first line's whitespace
+ indent_size = nil
+ unindented = text_block.split("\n").collect do |line|
+ indent_size = line.scan(/^\s*/)[0].to_s.size if indent_size.nil?
+ line == "" ? "" : line.slice(indent_size, line.length - 1)
+ end.join("\n")
+
+ unindented.strip
end
end

0 comments on commit 82c85ab

Please sign in to comment.
Something went wrong with that request. Please try again.