Class: Metanorma::Collection::Sectionsplit
- Inherits:
-
Object
- Object
- Metanorma::Collection::Sectionsplit
- Defined in:
- lib/metanorma/collection/sectionsplit/sectionsplit.rb
Constant Summary collapse
- SPLITSECTIONS =
[["//preface/*", "preface"], ["//sections/*", "sections"], ["//annex", nil], ["//bibliography/*[not(@hidden = 'true')]", "bibliography"], ["//indexsect", nil], ["//colophon", nil]].freeze
Instance Attribute Summary collapse
-
#filecache ⇒ Object
Returns the value of attribute filecache.
-
#key ⇒ Object
Returns the value of attribute key.
Instance Method Summary collapse
- #block?(node) ⇒ Boolean
- #build_collection ⇒ Object
- #coll_cover ⇒ Object
- #collection_manifest(filename, files, origxml, _presxml, dir) ⇒ Object
- #collection_setup(filename, dir) ⇒ Object
- #collectionyaml(files, xml) ⇒ Object
- #conflate_floatingtitles(nodes) ⇒ Object
- #create_sectionfile(xml, out, file, chunks, parentnode) ⇒ Object
- #emptydoc(xml) ⇒ Object
-
#initialize(opts) ⇒ Sectionsplit
constructor
A new instance of Sectionsplit.
- #ns(xpath) ⇒ Object
- #section_split_cover(col, ident, _one_doc_coll) ⇒ Object
- #sectionfile(fulldoc, xml, file, chunks, parentnode) ⇒ Object
- #sectionfile_insert(ins, chunks, parentnode) ⇒ Object
-
#sectionsplit ⇒ Object
Input XML is Semantic.
- #sectionsplit_prep(file, filename, dir) ⇒ Object
- #sectionsplit_preprocess_semxml(file, filename) ⇒ Object
- #sectionsplit_update_xrefs(xml) ⇒ Object
- #sectionsplit_write_semxml(filename, xml) ⇒ Object
- #titlerender(section) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Sectionsplit
Returns a new instance of Sectionsplit.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 10 def initialize(opts) @input_filename = opts[:input] @base = opts[:base] @output_filename = opts[:output] @xml = opts[:xml] @dir = opts[:dir] @compile_opts = opts[:compile_opts] || {} @fileslookup = opts[:fileslookup] @ident = opts[:ident] @isodoc = opts[:isodoc] end |
Instance Attribute Details
#filecache ⇒ Object
Returns the value of attribute filecache.
8 9 10 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 8 def filecache @filecache end |
#key ⇒ Object
Returns the value of attribute key.
8 9 10 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 8 def key @key end |
Instance Method Details
#block?(node) ⇒ Boolean
80 81 82 83 84 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 80 def block?(node) %w(p table formula admonition ol ul dl figure quote sourcecode example pre note pagebrreak hr bookmark requirement recommendation permission svgmap inputform toc passthrough review imagemap).include?(node.name) end |
#build_collection ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 26 def build_collection collection_setup(@base, @dir) files = sectionsplit # (@input_filename, @base, @dir, @compile_opts) input_xml = Nokogiri::XML(File.read(@input_filename, encoding: "UTF-8"), &:huge) collection_manifest(@base, files, input_xml, @xml, @dir).render( { format: %i(html), output_folder: "#{@output_filename}_collection", coverpage: File.join(@dir, "cover.html") }.merge(@compile_opts), ) end |
#coll_cover ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 52 def coll_cover <<~COVER <html><head><meta charset="UTF-8"/></head><body> <h1>{{ doctitle }}</h1> <h2>{{ docnumber }}</h2> <nav>{{ navigation }}</nav> </body></html> COVER end |
#collection_manifest(filename, files, origxml, _presxml, dir) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 37 def collection_manifest(filename, files, origxml, _presxml, dir) File.open(File.join(dir, "#{filename}.html.yaml"), "w:UTF-8") do |f| f.write(collectionyaml(files, origxml)) end Metanorma::Collection.parse File.join(dir, "#{filename}.html.yaml") end |
#collection_setup(filename, dir) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 44 def collection_setup(filename, dir) FileUtils.mkdir_p "#{filename}_collection" if filename FileUtils.mkdir_p dir File.open(File.join(dir, "cover.html"), "w:UTF-8") do |f| f.write(coll_cover) end end |
#collectionyaml(files, xml) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 180 def collectionyaml(files, xml) ret = { directives: ["presentation-xml", "bare-after-first"], bibdata: { title: { type: "title-main", language: @lang, content: xml.at(ns("//bibdata/title")).text }, type: "collection", docid: { type: xml.at(ns("//bibdata/docidentifier/@type")).text, id: xml.at(ns("//bibdata/docidentifier")).text, }, }, manifest: { level: "collection", title: "Collection", docref: files.sort_by { |f| f[:order] }.each.map do |f| { fileref: f[:url], identifier: f[:title] } end }, } ::Metanorma::Util::recursive_string_keys(ret).to_yaml end |
#conflate_floatingtitles(nodes) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 86 def conflate_floatingtitles(nodes) holdover = false nodes.each_with_object([]) do |x, m| if holdover then m.last << x else m << [x] end holdover = block?(x) end end |
#create_sectionfile(xml, out, file, chunks, parentnode) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 152 def create_sectionfile(xml, out, file, chunks, parentnode) ins = out.at(ns("//metanorma-extension")) || out.at(ns("//bibdata")) sectionfile_insert(ins, chunks, parentnode) Metanorma::Collection::XrefProcess::xref_process(out, xml, @key, @ident, @isodoc) outname = "#{file}.xml" File.open(File.join(@splitdir, outname), "w:UTF-8") do |f| f.write(out) end outname end |
#emptydoc(xml) ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 136 def emptydoc(xml) out = xml.dup out.xpath( ns("//preface | //sections | //annex | //bibliography/clause | " \ "//bibliography/references[not(@hidden = 'true')] | //indexsect | " \ "//colophon"), ).each(&:remove) out end |
#ns(xpath) ⇒ Object
22 23 24 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 22 def ns(xpath) @isodoc.ns(xpath) end |
#section_split_cover(col, ident, _one_doc_coll) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 204 def section_split_cover(col, ident, _one_doc_coll) dir = File.dirname(col.file) collection_setup(nil, dir) r = ::Metanorma::Collection::Renderer .new(col, dir, output_folder: "#{ident}_collection", format: %i(html), coverpage: File.join(dir, "cover.html")) r.coverpage # filename = one_doc_coll ? "#{ident}_index.html" : "index.html" filename = File.basename("#{ident}_index.html") # ident can be a directory with YAML indirection FileUtils.mv File.join(r.outdir, "index.html"), File.join(dir, filename) FileUtils.rm_rf r.outdir filename end |
#sectionfile(fulldoc, xml, file, chunks, parentnode) ⇒ Object
146 147 148 149 150 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 146 def sectionfile(fulldoc, xml, file, chunks, parentnode) fname = create_sectionfile(fulldoc, xml.dup, file, chunks, parentnode) { order: chunks.last["displayorder"].to_i, url: fname, title: titlerender(chunks.last) } end |
#sectionfile_insert(ins, chunks, parentnode) ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 164 def sectionfile_insert(ins, chunks, parentnode) if parentnode ins.next = "<#{parentnode}/>" chunks.each { |c| ins.next.add_child(c.dup) } else chunks.each { |c| ins.next = c.dup } end end |
#sectionsplit ⇒ Object
Input XML is Semantic
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 69 def sectionsplit xml = sectionsplit_prep(File.read(@input_filename), @base, @dir) @key = Metanorma::Collection::XrefProcess::xref_preprocess(xml, @isodoc) SPLITSECTIONS.each_with_object([]) do |n, ret| conflate_floatingtitles(xml.xpath(ns(n[0]))).each do |s| ret << sectionfile(xml, emptydoc(xml), "#{@base}.#{ret.size}", s, n[1]) end end end |
#sectionsplit_prep(file, filename, dir) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 96 def sectionsplit_prep(file, filename, dir) @splitdir = dir xml1, type = sectionsplit_preprocess_semxml(file, filename) flags = { format: :asciidoc, extension_keys: [:presentation], type: type }.merge(@compile_opts) Compile.new.compile(xml1, flags) f = File.open(xml1.sub(/\.xml$/, ".presentation.xml"), encoding: "utf-8") r = Nokogiri::XML(f, &:huge) r.xpath("//xmlns:svgmap1").each { |x| x.name = "svgmap" } r end |
#sectionsplit_preprocess_semxml(file, filename) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 109 def sectionsplit_preprocess_semxml(file, filename) xml = Nokogiri::XML(file, &:huge) type = xml.root.name.sub("-standard", "").to_sym sectionsplit_update_xrefs(xml) xml1 = sectionsplit_write_semxml(filename, xml) [xml1, type] end |
#sectionsplit_update_xrefs(xml) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 117 def sectionsplit_update_xrefs(xml) if c = @fileslookup&.parent n = c.nested c.nested = true # so unresolved erefs are not deleted c.update_xrefs(xml, @ident, {}) c.nested = n xml.xpath("//xmlns:svgmap").each { |x| x.name = "svgmap1" } # do not process svgmap until after files are split end end |
#sectionsplit_write_semxml(filename, xml) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 128 def sectionsplit_write_semxml(filename, xml) outname = Pathname.new("tmp_#{filename}").sub_ext(".xml").to_s File.open(outname, "w:UTF-8") do |f| f.write(@isodoc.to_xml(xml)) end outname end |
#titlerender(section) ⇒ Object
172 173 174 175 176 177 178 |
# File 'lib/metanorma/collection/sectionsplit/sectionsplit.rb', line 172 def titlerender(section) title = section.at(ns("./title")) or return "[Untitled]" t = title.dup t.xpath(ns(".//tab | .//br")).each { |x| x.replace(" ") } t.xpath(ns(".//strong")).each { |x| x.replace(x.children) } t.children.to_xml end |