Class: Metanorma::Sectionsplit
- Inherits:
-
Object
- Object
- Metanorma::Sectionsplit
- Defined in:
- lib/metanorma/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 def sectionsplit(filename, basename, dir, compile_options, fileslookup = nil, ident = nil).
- #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.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/metanorma/sectionsplit.rb', line 9 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.
7 8 9 |
# File 'lib/metanorma/sectionsplit.rb', line 7 def filecache @filecache end |
#key ⇒ Object
Returns the value of attribute key.
7 8 9 |
# File 'lib/metanorma/sectionsplit.rb', line 7 def key @key end |
Instance Method Details
#block?(node) ⇒ Boolean
80 81 82 83 84 |
# File 'lib/metanorma/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
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/metanorma/sectionsplit.rb', line 25 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
51 52 53 54 55 56 57 58 59 |
# File 'lib/metanorma/sectionsplit.rb', line 51 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
36 37 38 39 40 41 |
# File 'lib/metanorma/sectionsplit.rb', line 36 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
43 44 45 46 47 48 49 |
# File 'lib/metanorma/sectionsplit.rb', line 43 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
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/metanorma/sectionsplit.rb', line 178 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 }, } Util::recursive_string_keys(ret).to_yaml end |
#conflate_floatingtitles(nodes) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/metanorma/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
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/metanorma/sectionsplit.rb', line 151 def create_sectionfile(xml, out, file, chunks, parentnode) ins = out.at(ns("//metanorma-extension")) || out.at(ns("//bibdata")) sectionfile_insert(ins, chunks, parentnode) Metanorma::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
135 136 137 138 139 140 141 142 143 |
# File 'lib/metanorma/sectionsplit.rb', line 135 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
21 22 23 |
# File 'lib/metanorma/sectionsplit.rb', line 21 def ns(xpath) @isodoc.ns(xpath) end |
#section_split_cover(col, ident, one_doc_coll) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/metanorma/sectionsplit.rb', line 202 def section_split_cover(col, ident, one_doc_coll) dir = File.dirname(col.file) collection_setup(nil, dir) CollectionRenderer.new(col, dir, output_folder: "#{ident}_collection", format: %i(html), coverpage: File.join(dir, "cover.html")).coverpage #filename = one_doc_coll ? "#{ident}_index.html" : "index.html" filename = "#{ident}_index.html" FileUtils.mv "#{ident}_collection/index.html", File.join(dir, filename) FileUtils.rm_rf "#{ident}_collection" filename end |
#sectionfile(fulldoc, xml, file, chunks, parentnode) ⇒ Object
145 146 147 148 149 |
# File 'lib/metanorma/sectionsplit.rb', line 145 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
162 163 164 165 166 167 168 |
# File 'lib/metanorma/sectionsplit.rb', line 162 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 def sectionsplit(filename, basename, dir, compile_options, fileslookup = nil, ident = nil)
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/metanorma/sectionsplit.rb', line 69 def sectionsplit xml = sectionsplit_prep(File.read(@input_filename), @base, @dir) @key = Metanorma::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 |
# File 'lib/metanorma/sectionsplit.rb', line 96 def sectionsplit_prep(file, filename, dir) @splitdir = dir xml1filename, type = sectionsplit_preprocess_semxml(file, filename) Compile.new.compile( xml1filename, { format: :asciidoc, extension_keys: [:presentation], type: type } .merge(@compile_opts), ) Nokogiri::XML(File.read(xml1filename.sub(/\.xml$/, ".presentation.xml"), encoding: "utf-8"), &:huge) end |
#sectionsplit_preprocess_semxml(file, filename) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/metanorma/sectionsplit.rb', line 108 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) @filecache ||= [] @filecache << xml1 [xml1.path, type] end |
#sectionsplit_update_xrefs(xml) ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/metanorma/sectionsplit.rb', line 118 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 end end |
#sectionsplit_write_semxml(filename, xml) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/metanorma/sectionsplit.rb', line 127 def sectionsplit_write_semxml(filename, xml) Tempfile.open([filename, ".xml"], encoding: "utf-8") do |f| # f.write(@isodoc.to_xml(svg_preprocess(xml))) f.write(@isodoc.to_xml(xml)) f end end |
#titlerender(section) ⇒ Object
170 171 172 173 174 175 176 |
# File 'lib/metanorma/sectionsplit.rb', line 170 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 |