module IsoDoc module IEEE class WordWPConvert < WordConvert def stylesmap { example: "IEEEStdsParagraph", # x MsoNormal: "MsoBodyText", NormRef: "MsoBodyText", Biblio: "References", figure: "MsoBodyText", formula: "IEEEStdsEquation", # x Sourcecode: "IEEEStdsComputerCode", # x TableTitle: "TableTitles", FigureTitle: "FigureHeadings", admonition: "IEEEStdsWarning", # x abstract: "Abstract", AbstractTitle: "Unnumberedheading", level1frontmatter: "Unnumberedheading", level2frontmatter: "IEEEStdsLevel2frontmatter", # x level3frontmatter: "IEEEStdsLevel3frontmatter", # x level1header: "IEEESectionHeader", level2header: "IEEEStdsLevel2Header", # x level3header: "IEEEStdsLevel3Header", # x level4header: "IEEEStdsLevel4Header", # x level5header: "IEEEStdsLevel5Header", # x level6header: "IEEEStdsLevel6Header", # x zzSTDTitle1: "Titleofdocument", tabledata_center: "IEEEStdsTableData-Center", # x tabledata_left: "Tablecelltext", table_head: "IEEEStdsTableLineHead", # x table_subhead: "IEEEStdsTableLineSubhead", # x table_columnhead: "Tablecolumnheader", nameslist: "IEEEnames", intro: "Intro", } end APPENDIX_STYLE = %w(Appendix Appendixlevel2 Appendixlevel3).freeze def headings_style(hdr, idx) if hdr.at("./ancestor::div[@class = 'Annex']") headings_style_annex(hdr, idx) elsif hdr.at("./ancestor::div[@class = 'Section3' or " \ "@class = 'WordSectionContents']") headings_style_preface(hdr, idx) else headings_style_body(hdr, idx) end end def headings_style_annex(hdr, idx) hdr.delete("class") if idx == 1 hdr.next = BLUELINE hdr["style"] = "margin-left:0cm;#{hdr['style']}" else hdr["class"] = "Unnumberedheading" end end def headings_style_preface(hdr, idx) hdr.name = "p" hdr["class"] = stylesmap["level#{idx}frontmatter".to_sym] end def headings_style_body(hdr, idx) idx == 1 and hdr.name = "p" if hdr["class"] != stylesmap[:AbstractTitle] if idx == 1 hdr["class"] = stylesmap["level#{idx}header".to_sym] else hdr["style"] = "mso-list:l22 level#{idx} lfo33;#{hdr['style']}" end end end def toWord(result, filename, dir, header) result = from_xhtml(word_cleanup(to_xhtml(result))) .gsub("-DOUBLE_HYPHEN_ESCAPE-", "--") @wordstylesheet = wordstylesheet_update ::Html2Doc::IEEE_WP.new( filename: filename, imagedir: @localdir, stylesheet: @wordstylesheet&.path, header_file: header&.path, dir: dir, asciimathdelims: [@openmathdelim, @closemathdelim], liststyles: { ul: @ulstyle, ol: @olstyle } ).process(result) header&.unlink @wordstylesheet.unlink if @wordstylesheet.is_a?(Tempfile) end def table_cleanup(docxml) super docxml.xpath("//div[@class = 'table_container']//div[@class = 'Note']//p") .each do |n| n["class"] = "Tablenotes" end end def authority_cleanup(docxml) %w(copyright disclaimers tm participants).each do |t| authority_cleanup1(docxml, t) end authority_style(docxml) end def authority_style(docxml) copyright_style(docxml) legal_style(docxml) officer_style(docxml) end def feedback_table(docxml) docxml.at("//div[@class = 'boilerplate-copyright']")&.xpath(".//table") &.each do |t| t.xpath(".//tr").each do |tr| feedback_table1(tr) end t.replace(t.at(".//tbody").elements) end end def feedback_table1(trow) trow.name = "p" trow["class"] = "CopyrightInformationPage" trow["align"] = "left" trow.xpath("./td").each do |td| td.next_element and td << " " td.xpath("./p").each { |p| p.replace(p.children) } td.replace(td.children) end end def copyright_style(docxml) docxml.at("//div[@class = 'boilerplate-copyright']")&.xpath(".//p") &.each do |p| p["class"] ||= "CopyrightInformationPage" end feedback_table(docxml) end def legal_style(docxml) %w(disclaimers tm).each do |e| docxml.at("//div[@id = 'boilerplate-#{e}']")&.xpath(".//p") &.each do |p| p["class"] ||= "Disclaimertext" end end end def officer_style(docxml) officemember_style(docxml) end def officemember_style(docxml) docxml.xpath("//p[@type = 'officemember' or @type = 'officeorgmember']") .each do |p| p["class"] = stylesmap[:nameslist] end end BLUELINE = <<~XHTML.freeze
XHTML def abstract_cleanup(docxml) if f = docxml.at("//div[@class = 'abstract_div']") abstract_cleanup1(f, nil) end end def abstract_cleanup1(source, _dest) source.elements.reject { |e| %w(h1 h2).include?(e.name) }.each do |e| e.xpath("self::p | .//p").each do |p| p["class"] ||= stylesmap[:abstract] p["style"] = "margin-left:0cm;margin-right:0.25cm;#{p['style']}" end end end def insert_toc(intro, docxml, level) toc = assemble_toc(docxml, level) if intro&.include?("WORDTOC") #s = docxml.at("//div[@class = 'WordSectionContents']") #s.at("./p[@class='Unnumberedheading']")&.remove intro.sub("WORDTOC", toc) else source = docxml.at("//div[@class = 'TOC']") and source.children = toc intro end end end end end