class Readingme::TableProcessor

Constants

STATE_TRANSITIONS

Public Class Methods

call(input=$stdin, output=$stdout) click to toggle source
# File lib/readingme/table_processor.rb, line 6
def call input=$stdin, output=$stdout
  tab_proc = self.new
  input.each do |line|
    output.puts tab_proc.process_line(line)
  end
end
new() click to toggle source
# File lib/readingme/table_processor.rb, line 15
def initialize
  @state = :normal
end

Public Instance Methods

process_line(line) click to toggle source
# File lib/readingme/table_processor.rb, line 26
def process_line line
  if line =~ /^```(table)?$/
    if !$1 and @state == :normal
      process_table_line line
    else
      start_stop
    end
  else
    process_table_line line
  end
end

Private Instance Methods

change_state() click to toggle source
# File lib/readingme/table_processor.rb, line 55
def change_state
  @state = STATE_TRANSITIONS[@state]
end
process_table_line(line) click to toggle source
# File lib/readingme/table_processor.rb, line 66
def process_table_line line
  return line if @state == :normal

  if line =~ /^ *-{3,}/
    change_state
    ""
  else
    wrap_line line
  end
end
start_stop() click to toggle source
# File lib/readingme/table_processor.rb, line 60
def start_stop
  change_state
  @state != :normal ? "<table>" : "</table>"
end
wrap(data) click to toggle source
# File lib/readingme/table_processor.rb, line 49
def wrap data
  elem = @state == :header ? "th" : "td"
  "<#{elem}>#{data}</#{elem}>"
end
wrap_line(line) click to toggle source
# File lib/readingme/table_processor.rb, line 40
def wrap_line line
  wrapped_line = line.scan(/[^|]+/).map(&:strip).
                      reduce("") do |acc, data|
                        acc << wrap(data)
                      end
  "<tr>#{wrapped_line}</tr>"
end