module IsoDoc module NIST class WordConvert < IsoDoc::WordConvert def toc_insert(docxml, level) insertion = docxml.at("//div[h1 = 'Executive Summary']/"\ "preceding-sibling::div[h1][1]") || docxml.at("//div[@class = 'WordSection2']/child::*[last()]") if docxml.at("//p[@class = 'TableTitle']") insertion.next = make_TableWordToC(docxml) insertion.next = %{

List of Tables

} end if docxml.at("//p[@class = 'FigureTitle']") insertion.next = make_FigureWordToC(docxml) insertion.next = %{

List of Figures

} end if docxml.at("//p[@class = 'h1Annex']") insertion.next = make_AppendixWordToC(docxml) insertion.next = %{

List of Appendices

} end insertion.next = make_WordToC(docxml, level) insertion.next = %{

Table of Contents

} docxml end WORD_TOC_APPENDIX_PREFACE1 = <<~TOC.freeze  TOC \\h \\z \\t "h1Annex,1,h2Annex,2,h3Annex,3" TOC WORD_TOC_TABLE_PREFACE1 = <<~TOC.freeze  TOC \\h \\z \\t "TableTitle,tabletitle" TOC WORD_TOC_FIGURE_PREFACE1 = <<~TOC.freeze  TOC \\h \\z \\t "FigureTitle,figuretitle" TOC def header_strip(h) h = h.to_s.gsub(/<\/?p[^>]*>/, ""). gsub(%r{]+>[^<]+}m, "") super end def make_TableWordToC(docxml) toc = "" docxml.xpath("//p[@class = 'TableTitle']").each do |h| toc += word_toc_entry(1, header_strip(h)) end toc.sub(/(

)/, %{\\1#{WORD_TOC_TABLE_PREFACE1}}) + WORD_TOC_SUFFIX1 end def make_FigureWordToC(docxml) toc = "" docxml.xpath("//p[@class = 'FigureTitle']").each do |h| toc += word_toc_entry(1, header_strip(h)) end toc.sub(/(

)/, %{\\1#{WORD_TOC_FIGURE_PREFACE1}}) + WORD_TOC_SUFFIX1 end def make_AppendixWordToC(docxml) toc = "" docxml.xpath("//p[@class = 'h1Annex'] | //p[@class = 'h2Annex'] | "\ "p[@class = 'h3Annex']").each do |h| toc += word_toc_entry(h["class"][1].to_i, header_strip(h)) end toc.sub(/(

)/, %{\\1#{WORD_TOC_APPENDIX_PREFACE1}}) + WORD_TOC_SUFFIX1 end end end end