class Readingme::Encoder

Constants

EMPTY_LINE
PRE_LINE

Public Class Methods

call(input=$stdin, output=$stdout) click to toggle source
# File lib/readingme/encoder.rb, line 5
def call input=$stdin, output=$stdout
  encoder = self.new
  input.each do |line|
    output.puts encoder.process_line(line)
  end
end
new() click to toggle source
# File lib/readingme/encoder.rb, line 18
def initialize
  @prev_line = nil
  @pre_mode = :off
  @entities = HTMLEntities.new
end

Public Instance Methods

process_line(line) click to toggle source
# File lib/readingme/encoder.rb, line 34
def process_line line
  start_stop line

  r_line =  if @pre_mode == :off
              @entities.encode(line, :named).
                        gsub("&lt;", "<").
                        gsub("&gt;", ">").
                        gsub("&quot;", "\"")
            else
              line
            end
  @prev_line = line

  r_line
end
start_stop(line) click to toggle source
# File lib/readingme/encoder.rb, line 25
def start_stop line
  if @pre_mode == :off && @prev_line =~ EMPTY_LINE && line =~ PRE_LINE
    @pre_mode = :on
  elsif @pre_mode == :on && @prev_line =~ PRE_LINE && line =~ /\A$\Z|[^ ]+/
    @pre_mode = :off
  end
end