class EdifactConverter::FTXText

Constants

Line
Text

Attributes

current_line[RW]

Public Class Methods

new(ftxs) click to toggle source
# File lib/edifact_converter/ftx_text.rb, line 56
def initialize(ftxs)
  ftxs.each do |ftx|
    font, format, align = extract_style ftx
    ftx.xpath('Elm[position() > 2]/SubElm/text()').each do |text|
      text = text.to_s
      continue_line = if text[-1] == '\\'
        text.chop!
        true
      end
      current_or_new_line(align).texts << Text.new(format, font, text)
      self.current_line = nil unless continue_line
    end
  end
  optimize_formatting
  pack_lines
  #lines.each { |line| puts line }
end
parse(ftxs) click to toggle source
# File lib/edifact_converter/ftx_text.rb, line 52
def self.parse(ftxs)
  self.new ftxs
end

Public Instance Methods

==(ftx_text) click to toggle source
# File lib/edifact_converter/ftx_text.rb, line 74
def ==(ftx_text)
  lines == ftx_text.lines
end
lines() click to toggle source
# File lib/edifact_converter/ftx_text.rb, line 48
def lines
  @lines ||= []
end

Private Instance Methods

current_line=(line) click to toggle source
# File lib/edifact_converter/ftx_text.rb, line 92
def current_line=(line)
  lines << line if line
  @current_line = line
end
current_or_new_line(align) click to toggle source
# File lib/edifact_converter/ftx_text.rb, line 84
def current_or_new_line(align)
  if current_line and current_line.align == align
    current_line
  else
    new_line align
  end
end
extract_style(ftx) click to toggle source
# File lib/edifact_converter/ftx_text.rb, line 97
def extract_style(ftx)
  compressed = ftx.xpath('Elm[2]/SubElm/text()').to_s
  compressed.chars.map.with_index do |char, index|
    case char
    when 'P'
      :proportional
    when 'F'
      if index == 0
        :fixed
      else
        :bold
      end
    when 'H'
      :right
    when 'M'
      :center
    when 'U'
      :underline
    when '0'
      if index == 2
        :left
      else
        :normal
      end
    end
  end
end
new_line(align) click to toggle source
# File lib/edifact_converter/ftx_text.rb, line 80
def new_line(align)
  self.current_line = Line.new(align)
end
optimize_formatting() click to toggle source
# File lib/edifact_converter/ftx_text.rb, line 125
def optimize_formatting
  start_spaces_regex = Regexp.new '^( +)'
  end_spaces_regex = Regexp.new '( +)$'
  lines.each do |line|
    line.texts.each_with_index do |text, index|
      next if text.format == :normal
      end_spaces_regex.match(text.text) do |m|
        line.texts.insert index+1, Text.new(:normal, text.font, m[0])
      end
      start_spaces_regex.match(text.text) do |m|
        line.texts.insert index, Text.new(:normal, text.font, m[0])
      end
      text.text.strip!
    end
  end
end
pack_lines() click to toggle source
# File lib/edifact_converter/ftx_text.rb, line 142
def pack_lines
  lines.each do |line|
    chunked = line.texts.chunk { |text| "#{text.format}:#{text.font}" }.map do |set|
      set[1] = set.last.reduce :merge
    end
    line.texts = chunked
  end
end