class EdifactConverter::XML::XftxParser

Constants

FTXElm

Attributes

code[RW]
paragraphs[RW]
xftx[RW]

Public Class Methods

new(xftx_node) click to toggle source
# File lib/edifact_converter/xml/xftx_parser.rb, line 13
def initialize(xftx_node)
  self.paragraphs = [Paragraph.new]
  self.xftx = xftx_node
end
parse(xftx_node) click to toggle source
# File lib/edifact_converter/xml/xftx_parser.rb, line 9
def self.parse(xftx_node)
  self.new(xftx_node).transform_xftx
end

Public Instance Methods

pack_paragraphs() click to toggle source
# File lib/edifact_converter/xml/xftx_parser.rb, line 91
def pack_paragraphs
  paragraphs.reject! { |paragraph| paragraph.empty? }
  paragraphs.inject(nil) do |previous, current|
    if previous and not(previous.texts?)
      previous.content.delete_if do |elm|
        current.add_start_break elm
        true
      end
    end
  end
  paragraphs.reject! { |paragraph| paragraph.empty? }
end
push_paragraph() { |previous, current| ... } click to toggle source
# File lib/edifact_converter/xml/xftx_parser.rb, line 84
def push_paragraph
  previous = paragraphs.last
  paragraphs << current = previous.empty_copy
  yield previous, current
  paragraphs << previous.empty_copy
end
set_alignment(alignment) { || ... } click to toggle source
# File lib/edifact_converter/xml/xftx_parser.rb, line 61
def set_alignment(alignment)
  push_paragraph do |previous, paragraph|
    paragraph.format = alignment
    paragraph.add_break unless previous.end_breaks?
    yield
    paragraphs.last.add_break
  end
end
set_font(font) { || ... } click to toggle source
# File lib/edifact_converter/xml/xftx_parser.rb, line 70
def set_font(font)
  push_paragraph do |previous, paragraph|
    paragraph.font = font
    yield
  end
end
set_style(style) { || ... } click to toggle source
# File lib/edifact_converter/xml/xftx_parser.rb, line 77
def set_style(style)
  push_paragraph do |previous, paragraph|
    paragraph.format = style
    yield
  end
end
to_ftx() click to toggle source
# File lib/edifact_converter/xml/xftx_parser.rb, line 129
def to_ftx()
  ftxs = []
  paragraphs.delete_if do |paragraph|
    next_paragraph = paragraphs[1]
    texts = paragraph.to_subelms(paragraphs[1])
    texts.each_slice(5) do |content|
      if content.size < 5 and next_paragraph
        while content.size < 5 and next_paragraph.start_breaks?
          content << "."
          next_paragraph.remove_start_break
        end
      end
      ftxs << FTXElm.new(code, paragraph.ftx_format, content)
    end
    true
  end
  ftxs.each do |ftx|
    ftx.insert_before(xftx)
  end
  xftx.remove
end
to_s() click to toggle source
# File lib/edifact_converter/xml/xftx_parser.rb, line 104
def to_s
  text = "#{code}:\n"
  paragraphs.each do |paragraph|
    text << paragraph.to_s
  end
  text
end
transform_content(subelm) click to toggle source
# File lib/edifact_converter/xml/xftx_parser.rb, line 25
def transform_content(subelm)
  subelm.children.each do |node|
    case node.node_type
    when Nokogiri::XML::Node::TEXT_NODE
      paragraphs.last.add_text node.text
    when Nokogiri::XML::Node::ELEMENT_NODE
      transform_element node
    end
  end
end
transform_element(element) click to toggle source
# File lib/edifact_converter/xml/xftx_parser.rb, line 36
def transform_element(element)
  case element.name
  when 'Break'
    paragraphs.last.add_break
    return
  when 'Space'
    paragraphs.last.add_text ' '
    return
  when 'Center'
    set_alignment(Paragraph::CENTER) { transform_content element }
  when 'Right'
    set_alignment(Paragraph::RIGHT) { transform_content element }
  when 'Bold'
    set_style(Paragraph::BOLD) { transform_content element }
  when 'Italic'
    set_style(Paragraph::ITALIC) { transform_content element }
  when 'Underline'
    set_style(Paragraph::UNDERLINE) { transform_content element }
  when 'FixedFont'
    set_font(Paragraph::MONOSPACE) { transform_content element }
  else
    throw RuntimeError "Unknown xftx #{element.name}"
  end
end
transform_xftx() click to toggle source
# File lib/edifact_converter/xml/xftx_parser.rb, line 18
def transform_xftx
  self.code = xftx.children[0].children[0].text
  transform_content xftx.children[3].children[0]
  pack_paragraphs
  to_ftx
end