lib/tableware/parser.rb in tableware-0.1.3 vs lib/tableware/parser.rb in tableware-0.2.0

- old
+ new

@@ -2,26 +2,30 @@ class Parser class TableWithoutHeaderError < StandardError; end - ROW_START = /^\s*\|/ - ROW_END = /\|\s*$/ + ROW_START = /^>?\s*\|?/ + ROW_END = /\|?\s*$/ def arrays(input) make_arrays(input) end def hashes(input) items = make_arrays(input) - raise TableWithoutHeaderError, 'Sorry, only text tables with headers rows can be turned into hashes' unless @headers + raise TableWithoutHeaderError, 'Sorry, only text tables with header rows can be turned into hashes' unless @headers items.map! { |row| @headers.zip(row).to_h } end private def make_arrays(input) lines = prepare_lines(input) - lines[@data_start..-1].map { |line| parse_line(line) } + lines[@data_start..-1].map do |line| + parsed = parse_line(line) + return [parsed] if @focused_line + parsed + end end private def prepare_lines(input) lines = input.strip.lines @@ -36,12 +40,14 @@ lines end private def parse_line(line) line + .strip + .tap { |ln| @focused_line = ln[0] == '>' } .sub(ROW_START, '') .sub(ROW_END, '') - .split('|') + .split(' | ') .map!(&:strip) end end