class BlockReader
fix: move into TextUtils
namespace/module!!
Public Class Methods
from_file( path )
click to toggle source
# File lib/textutils/reader/block_reader.rb, line 10 def self.from_file( path ) ## nb: assume/enfore utf-8 encoding (with or without BOM - byte order mark) ## - see textutils/utils.rb text = File.read_utf8( path ) self.from_string( text ) end
from_string( text )
click to toggle source
# File lib/textutils/reader/block_reader.rb, line 17 def self.from_string( text ) self.new( text ) end
new( text )
click to toggle source
# File lib/textutils/reader/block_reader.rb, line 21 def initialize( text ) @text = text end
Public Instance Methods
read()
click to toggle source
# File lib/textutils/reader/block_reader.rb, line 25 def read ## note returns an array of (line) strings e.g. ## [ ## "line1\nline2", ## -- block1 ## "line1\nline2\nline3" ## -- block2 ## ] blocks = [] buf = "" @text.each_line do |line| # comments allow: # 1) ##### (shell/ruby style) if line =~ /^\s*#/ # skip komments and do NOT copy to result (keep comments secret!) logger.debug 'skipping comment line' next end # if line =~ /^\s*$/ # # kommentar oder leerzeile überspringen # logger.debug 'skipping blank line' # next # end # pass 2) remove leading and trailing whitespace line = line.strip if line =~ /^-{3,}$/ ## three or more lines logger.debug 'block separator' blocks << buf.strip ## note: strip leading and trailing whitespace buf = "" else buf << "#{line}\n" end end # each lines blocks << buf.strip ## note: strip leading and trailing whitespace blocks end