lib/avv2word/document.rb in avv2word-1.0.0 vs lib/avv2word/document.rb in avv2word-1.0.4

- old
+ new

@@ -69,10 +69,15 @@ out.put_next_entry entry.name if @replaceable_files[entry.name] && entry.name == Document.doc_xml_file source = entry.get_input_stream.read # Change only the body of document. TODO: Improve this... source = source.sub(/(<w:body>)((.|\n)*?)(<w:sectPr)/, "\\1#{@replaceable_files[entry.name]}\\4") + # add header and footer only if they really exist + if entry.name == 'word/document.xml' + source.sub!('<!--<w:headerReference w:type="default" r:id="rId8"/>-->','<w:headerReference w:type="default" r:id="rId8"/>') if @header + source.sub!('<!--<w:footerReference w:type="default" r:id="rId9"/>-->','<w:footerReference w:type="default" r:id="rId9"/>') if @footer + end out.write(source) elsif @replaceable_files[entry.name] out.write(@replaceable_files[entry.name]) elsif entry.name == Document.content_types_xml_file raw_file = entry.get_input_stream.read @@ -97,12 +102,19 @@ end end def replace_files(html, extras = false) html = '<body></body>' if html.nil? || html.empty? + + header_html = (html =~ /(<header>.*?<\/header>)/m ? $1 : '') + footer_html = (html =~ /(<footer>.*?<\/footer>)/m ? $1 : '') original_source = Nokogiri::HTML(html.gsub(/>\s+</, '><')) - transform_and_replace(original_source, xslt_path('header'), Document.header_xml_file) - transform_and_replace(original_source, xslt_path('footer'), Document.footer_xml_file) + header = Nokogiri::HTML(header_html.gsub(/>\s+</, '><')) + footer = Nokogiri::HTML(footer_html.gsub(/>\s+</, '><')) + @header = (header_html.empty? ? false : true) + @footer = (footer_html.empty? ? false : true) + transform_and_replace(header, xslt_path('header'), Document.header_xml_file) + transform_and_replace(footer, xslt_path('footer'), Document.footer_xml_file) transform_and_replace(original_source, xslt_path('relations'), Document.relations_xml_file) source = xslt(stylesheet_name: 'cleanup').transform(original_source) transform_and_replace(source, xslt_path('numbering'), Document.numbering_xml_file) transform_doc_xml(source, extras) local_images(source)