Class: EdifactConverter::XML::XftxParser
- Inherits:
-
Object
- Object
- EdifactConverter::XML::XftxParser
- Defined in:
- lib/edifact_converter/xml/xftx_parser.rb
Defined Under Namespace
Classes: FTXElm
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#paragraphs ⇒ Object
Returns the value of attribute paragraphs.
-
#xftx ⇒ Object
Returns the value of attribute xftx.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(xftx_node) ⇒ XftxParser
constructor
A new instance of XftxParser.
- #pack_paragraphs ⇒ Object
- #push_paragraph {|previous, current| ... } ⇒ Object
- #set_alignment(alignment) ⇒ Object
- #set_font(font) ⇒ Object
- #set_style(style) ⇒ Object
- #to_ftx ⇒ Object
- #to_s ⇒ Object
- #transform_content(subelm) ⇒ Object
- #transform_element(element) ⇒ Object
- #transform_xftx ⇒ Object
Constructor Details
#initialize(xftx_node) ⇒ XftxParser
Returns a new instance of XftxParser.
13 14 15 16 |
# File 'lib/edifact_converter/xml/xftx_parser.rb', line 13 def initialize(xftx_node) self.paragraphs = [Paragraph.new] self.xftx = xftx_node end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
7 8 9 |
# File 'lib/edifact_converter/xml/xftx_parser.rb', line 7 def code @code end |
#paragraphs ⇒ Object
Returns the value of attribute paragraphs.
7 8 9 |
# File 'lib/edifact_converter/xml/xftx_parser.rb', line 7 def paragraphs @paragraphs end |
#xftx ⇒ Object
Returns the value of attribute xftx.
7 8 9 |
# File 'lib/edifact_converter/xml/xftx_parser.rb', line 7 def xftx @xftx end |
Class Method Details
.parse(xftx_node) ⇒ Object
9 10 11 |
# File 'lib/edifact_converter/xml/xftx_parser.rb', line 9 def self.parse(xftx_node) self.new(xftx_node).transform_xftx end |
Instance Method Details
#pack_paragraphs ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 |
# 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| ... } ⇒ Object
84 85 86 87 88 89 |
# 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) ⇒ Object
61 62 63 64 65 66 67 68 |
# 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) ⇒ Object
70 71 72 73 74 75 |
# 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) ⇒ Object
77 78 79 80 81 82 |
# 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 ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# 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 ⇒ Object
104 105 106 107 108 109 110 |
# 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) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# 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) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# 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 ⇒ Object
18 19 20 21 22 23 |
# 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 |