lib/avv2word/document.rb in avv2word-1.1.33 vs lib/avv2word/document.rb in avv2word-1.1.34

- old
+ new

@@ -182,21 +182,43 @@ local_images(source) local_images(footer, :footer) if @footer local_images(header, :header) if @header add_footnotes(source.css("footnote").map{ |footnote| footnote.text }) + add_comments(source.css("comment")) unless source.css("comment").empty? end def transform_doc_xml(source, extras = false) transformed_source = xslt(stylesheet_name: 'cleanup').transform(source) transformed_source = xslt(stylesheet_name: 'inline_elements').transform(transformed_source) transform_and_replace(transformed_source, document_xslt(extras), Document.doc_xml_file, extras) end private - def add_footnotes(footnotes) + def add_comments(comments) + @replaceable_files["word/comments.xml"] = %(<?xml version="1.0" encoding="UTF-8"?><w:comments xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14">) + comments.each_with_index do |comment,id| + time = comment.attributes["time"].value # 2020-04-29T11:49:00Z + author = comment.attributes["author"].value + message = comment.attributes["message"].value + initials = comment.attributes["initials"].value + comment_generator = %Q(<w:comment w:id="#{id}" w:author="#{author}" w:date="#{time}" w:initials="#{initials}"><w:p w14:paraId="2F08968D" w14:textId="06CE6739" w:rsidR="009A6661" w:rsidRDefault="009A6661"><w:pPr><w:pStyle w:val="CommentText"/></w:pPr><w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr><w:annotationRef/></w:r><w:r><w:t>#{message}</w:t></w:r></w:p></w:comment>) + @replaceable_files["word/comments.xml"] += comment_generator + end + @replaceable_files["word/comments.xml"] += "</w:comments>" + + rels = Nokogiri::XML(@replaceable_files["word/_rels/document.xml.rels"]) + new_rel = %(<Relationship Id="rIdXX" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="comments.xml"/>) + new_rel.sub!(/XX/, (rels.xpath("//xmlns:Relationship").size+1).to_s ) + rels.children.first << Nokogiri::XML.parse(new_rel).child + @replaceable_files["word/_rels/document.xml.rels"] = rels.to_xml + + insert_nodes_to_xml(Document.content_types_xml_file, ['<Override PartName="/word/comments.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml"/>']) + end + + def add_footnotes(footnotes) if footnotes.empty? @replaceable_files["word/footnotes.xml"] = "" return end #text = Zip::File.open("templates/default.docx"){|zipfile| zipfile.read("word/footnotes.xml")} @@ -246,11 +268,11 @@ Zip::File.open(@template_path) do |zipfile| styles = Nokogiri::XML.parse(zipfile.read("word/styles.xml")) end end [ '<w:style w:type="paragraph" w:styleId="FootnoteText"><w:name w:val="footnote text"/><w:basedOn w:val="Normal"/><w:link w:val="FootnoteTextChar"/><w:uiPriority w:val="99"/><w:semiHidden/><w:unhideWhenUsed/><w:rsid w:val="008D12FB"/></w:style>', - '<w:style w:type="character" w:customStyle="1" w:styleId="FootnoteTextChar"><w:name w:val="Footnote Text Char"/><w:basedOn w:val="DefaultParagraphFont"/><w:link w:val="FootnoteText"/><w:uiPriority w:val="99"/><w:semiHidden/><w:rsid w:val="008D12FB"/></w:style>', + '<w:style w:type="character" w:customStyle="1" w:styleId="FootnoteTextChar"><w:name w:val="Footnote Text Char"/><w:basedOn w:val="DefaultParagraphFont"/><w:link w:val="FootnoteText"/><w:uiPriority w:val="99"/><w:semiHidden/><w:rsid w:val="008D12FB"/><w:rPr><w:sz w:val="14"/></w:rPr></w:style>', '<w:style w:type="character" w:styleId="FootnoteReference"><w:name w:val="footnote reference"/><w:basedOn w:val="DefaultParagraphFont"/><w:uiPriority w:val="99"/><w:semiHidden/><w:unhideWhenUsed/><w:rsid w:val="008D12FB"/><w:rPr><w:vertAlign w:val="superscript"/></w:rPr></w:style>' ].each do |new_style| styles.children.first << Nokogiri::XML.parse(new_style).child end [ '<w:lsdException w:name="footnote text" w:semiHidden="1" w:unhideWhenUsed="1"/>', @@ -259,19 +281,14 @@ styles.xpath("/w:styles/w:latentStyles").first << Nokogiri::XML.parse(new_latent_style).child end @replaceable_files["word/styles.xml"] = styles.to_xml # content type - Zip::File.open(@template_path) do |zipfile| - types = Nokogiri::XML.parse(zipfile.read("[Content_Types].xml")) - [ # '<Override PartName="/word/endnotes.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml"/>', - '<Override PartName="/word/footnotes.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml"/>' - ].each do |new_type| - types.children.first << Nokogiri::XML.parse(new_type).child - end - @replaceable_files["[Content_Types].xml"] = types.to_xml - end + insert_nodes_to_xml(Document.content_types_xml_file, [ + # '<Override PartName="/word/endnotes.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml"/>', + '<Override PartName="/word/footnotes.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml"/>' + ]) end def inject_relations(relations, noko_html_source) rel_doc = Nokogiri::XML(relations) relations_root = rel_doc.css("Relationships").first @@ -360,8 +377,23 @@ doc.at_css("Types").add_child( "<Default Extension='#{ext}' ContentType='image/#{content_type_from_extension(ext)}'/>") end #return the amended source to be saved into the zip doc.to_s + end + + def insert_nodes_to_xml(target_xml_file, inputs) + xml_content = + if @replaceable_files[target_xml_file] + Nokogiri::XML(@replaceable_files[target_xml_file]) + else + Zip::File.open(@template_path) do |zipfile| + Nokogiri::XML.parse(zipfile.read(target_xml_file)) + end + end + inputs.each do |new_node| + xml_content.children.first << Nokogiri::XML.parse(new_node).child + end + @replaceable_files[target_xml_file] = xml_content.to_xml end end end