module IsoDoc module WordFunction module Postprocess def word_preface(docxml) word_cover(docxml) if @wordcoverpage word_intro(docxml, @wordToClevels) if @wordintropage end def word_cover(docxml) cover = File.read(@wordcoverpage, encoding: "UTF-8") cover = populate_template(cover, :word) coverxml = to_word_xhtml_fragment(cover) docxml.at('//div[@class="WordSection1"]').children.first.previous = coverxml.to_xml(encoding: "US-ASCII") end def word_intro(docxml, level) intro = insert_toc(File.read(@wordintropage, encoding: "UTF-8"), docxml, level) intro = populate_template(intro, :word) introxml = to_word_xhtml_fragment(intro) docxml.at('//div[@class="WordSection2"]').children.first.previous = introxml.to_xml(encoding: "US-ASCII") end def insert_toc(intro, docxml, level) toc = "" toc += make_WordToC(docxml, level) toc += make_table_word_toc(docxml) toc += make_figure_word_toc(docxml) toc += make_recommendation_word_toc(docxml) intro.sub(/WORDTOC/, toc) end def word_toc_entry(toclevel, heading) bookmark = bookmarkid # Random.rand(1000000000) <<~TOC
#{heading} . PAGEREF _Toc#{bookmark} \\h 1
TOC end def word_toc_preface(level) <<~TOC TOC \\o "1-#{level}" \\h \\z \\u TOC end WORD_TOC_SUFFIX1 = <<~TOC.freeze)/, %{\\1#{word_toc_preface(level)}}) + WORD_TOC_SUFFIX1 end # inheriting gems need to add native Word name of style, if different # including both CSS style name and human readable style name. # Any human readable style name needs to come first for the Word template # to work in regenerating the ToC def table_toc_class %w(TableTitle tabletitle) end def figure_toc_class %w(FigureTitle figuretitle) end def reqt_toc_class %w(RecommendationTitle RecommendationTestTitle recommendationtitle recommendationtesttitle) end def toc_word_class_list(classes) classes.map do |x| / /.match?(x) ? %("#{x}") : x end.join(",") end def word_toc_reqt_preface1 <<~TOC TOC \\h \\z \\t #{toc_word_class_list reqt_toc_class} TOC end def word_toc_table_preface1 <<~TOC TOC \\h \\z \\t #{toc_word_class_list table_toc_class} TOC end def word_toc_figure_preface1 <<~TOC TOC \\h \\z \\t #{toc_word_class_list figure_toc_class} TOC end def table_toc_xpath attr = table_toc_class.map { |x| "@class = '#{x}'" } "//p[#{attr.join(' or ')}]" end def make_table_word_toc(docxml) (docxml.at(table_toc_xpath) && @toctablestitle) or return "" toc = %{
#{@toctablestitle}
} docxml.xpath(table_toc_xpath).each do |h| toc += word_toc_entry(1, header_strip(h)) end toc.sub(/()/, %{\\1#{word_toc_table_preface1}}) + WORD_TOC_SUFFIX1 end def figure_toc_xpath attr = figure_toc_class.map { |x| "@class = '#{x}'" } "//p[#{attr.join(' or ')}]" end def make_figure_word_toc(docxml) (docxml.at(figure_toc_xpath) && @tocfigurestitle) or return "" toc = %{
#{@tocfigurestitle}
} docxml.xpath(figure_toc_xpath).each do |h| toc += word_toc_entry(1, header_strip(h)) end toc.sub(/()/, %{\\1#{word_toc_figure_preface1}}) + WORD_TOC_SUFFIX1 end def reqt_toc_xpath attr = reqt_toc_class.map { |x| "@class = '#{x}'" } "//p[#{attr.join(' or ')}]" end def make_recommendation_word_toc(docxml) (docxml.at(reqt_toc_xpath) && @tocrecommendationstitle) or return "" toc = %{
#{@tocrecommendationstitle}
} docxml.xpath(reqt_toc_xpath).sort_by do |h| recommmendation_sort_key(h.text) end.each do |h| toc += word_toc_entry(1, header_strip(h)) end toc.sub(/()/, %{\\1#{word_toc_reqt_preface1}}) + WORD_TOC_SUFFIX1 end def recommmendation_sort_key(header) m = /^([^0-9]+) (\d+)/.match(header) || /^([^:]+)/.match(header) m ||= [header, nil] ret = "#{recommmendation_sort_key1(m[1])}::" m[2] and ret += ("%04d" % m[2].to_i).to_s ret end def recommmendation_sort_key1(type) case type&.downcase when "requirement" then "04" when "recommendation" then "05" when "permission" then "06" else type end end def authority_cleanup1(docxml, klass) dest = docxml.at("//div[@id = 'boilerplate-#{klass}-destination']") auth = docxml.at("//div[@id = 'boilerplate-#{klass}' " \ "or @class = 'boilerplate-#{klass}']") auth&.xpath(".//h1[not(text())] | .//h2[not(text())]")&.each(&:remove) auth&.xpath(".//h1 | .//h2")&.each do |h| h.name = "p" h["class"] = "TitlePageSubhead" end dest and auth and dest.replace(auth.remove) end def authority_cleanup(docxml) %w(copyright license legal feedback).each do |t| authority_cleanup1(docxml, t) end coverpage_note_cleanup(docxml) end def coverpage_note_cleanup(docxml) if dest = docxml.at("//div[@id = 'coverpage-note-destination']") auth = docxml.xpath("//*[@coverpage]") if auth.empty? then dest.remove else auth.each do |x| dest << x.remove end end end docxml.xpath("//*[@coverpage]").each { |x| x.delete("coverpage") } end def generate_header(filename, _dir) return nil unless @header template = IsoDoc::Common.liquid(File.read(@header, encoding: "UTF-8")) meta = @meta.get.merge(@labels ? { labels: @labels } : {}) .merge(@meta.labels ? { labels: @meta.labels } : {}) meta[:filename] = filename params = meta.transform_keys(&:to_s) Tempfile.open(%w(header html), encoding: "utf-8") do |f| f.write(template.render(params)) f end end def word_section_breaks(docxml) @landscapestyle = "" word_section_breaks1(docxml, "WordSection2") word_section_breaks1(docxml, "WordSection3") word_remove_pb_before_annex(docxml) docxml.xpath("//br[@orientation]").each do |br| br.delete("orientation") end end def word_section_breaks1(docxml, sect) docxml.xpath("//div[@class = '#{sect}']//br[@orientation]").reverse .each_with_index do |br, i| @landscapestyle += "\ndiv.#{sect}_#{i} {page:#{sect}" \ "#{br['orientation'] == 'landscape' ? 'L' : 'P'};}\n" split_at_section_break(docxml, sect, br, i) end end def split_at_section_break(docxml, sect, brk, idx) move = brk.parent.xpath("following::node()") & brk.document.xpath("//div[@class = '#{sect}']//*") ins = docxml.at("//div[@class = '#{sect}']") .after("
").next_element move.each do |m| next if m.at("./ancestor::div[@class = '#{sect}_#{idx}']") ins << m.remove end end end end end