Class: StructuredText::CommentedReader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/structuredtext.rb

Overview

Removes comments from text.

Comments start with a specified comment delimiter and continue to the end of the line.

> StructuredText::CommentedReader.new(<<-EOTEXT
" line 1 # comment 1
" # comment 2
" line 3 # comment 3
" EOTEXT
> ).collect
=> ["line 1 ", "", "line 3 "]

The default comment delimiter is “#”. A different delimiter may be specified when the object is created. Blank lines may either be returned or ignored.

Instance Method Summary collapse

Constructor Details

#initialize(source, comment_delimiter = "#", skip_blanks = true) ⇒ CommentedReader

Intialize the reader with the text to parse and parameters that determine how comments will be processed.

source

an enumerable set of text lines, e.g. a stream or a string.

comment_delimiter

the comment delimiter, the default is “#”

skip_blanks

skip blank lines in the input, the default is true



53
54
55
56
57
# File 'lib/structuredtext.rb', line 53

def initialize(source, comment_delimiter = "#", skip_blanks = true)
  @source = source
  @comment_regex = Regexp.compile(comment_delimiter + '.*$')
  @skip_blanks = skip_blanks
end

Instance Method Details

#eachObject

Enumerate the lines in the source, removing all text after a comment character.



61
62
63
64
65
66
67
# File 'lib/structuredtext.rb', line 61

def each # :yields: line with comments removed
  @source.each do |line|
    line.chomp!
    line.sub!(@comment_regex, "")
    yield line if not @skip_blanks or not line.empty?
  end
end