Class: Metanorma::Sectionsplit

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/sectionsplit.rb,
lib/metanorma/sectionsplit_links.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

Instance Method Summary collapse

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

#filecacheObject

Returns the value of attribute filecache.



7
8
9
# File 'lib/metanorma/sectionsplit.rb', line 7

def filecache
  @filecache
end

Instance Method Details

#block?(node) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/metanorma/sectionsplit.rb', line 79

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_collectionObject



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_coverObject



51
52
53
54
55
56
57
58
59
# File 'lib/metanorma/sectionsplit.rb', line 51

def coll_cover
  <<~COVER
    <html><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



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/metanorma/sectionsplit.rb', line 163

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



85
86
87
88
89
90
91
92
93
# File 'lib/metanorma/sectionsplit.rb', line 85

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

#copy_repo_items_biblio(ins, section, xml) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/metanorma/sectionsplit_links.rb', line 95

def copy_repo_items_biblio(ins, section, xml)
  bibitems = Util::gather_bibitems(section)
  xml.xpath(ns("//references/bibitem[docidentifier/@type = 'repository']"))
    .each_with_object([]) do |b, m|
    bibitems[b["id"]] or next
    # section.at("//*[@bibitemid = '#{b['id']}']") or next
    ins << b.dup
    m << b["id"]
  end
end

#create_sectionfile(xml, out, file, chunks, parentnode) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/metanorma/sectionsplit.rb', line 136

def create_sectionfile(xml, out, file, chunks, parentnode)
  ins = out.at(ns("//metanorma-extension")) || out.at(ns("//bibdata"))
  sectionfile_insert(ins, chunks, parentnode)
  xref_process(out, xml, @key)
  outname = "#{file}.xml"
  File.open(File.join(@splitdir, outname), "w:UTF-8") do |f|
    f.write(out)
  end
  outname
end

#emptydoc(xml) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/metanorma/sectionsplit.rb', line 120

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

#eref_to_internal_eref(section, xml, key) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/metanorma/sectionsplit_links.rb', line 54

def eref_to_internal_eref(section, xml, key)
  bibitems = Util::gather_bibitems(xml)
  bibitemids = Util::gather_bibitemids(section)
  eref_to_internal_eref_select(section, xml, bibitems)
    .each_with_object([]) do |x, m|
    url = bibitems[x]&.at(ns("./uri[@type = 'citation']"))
    bibitemids[x]&.each do |e|
      id = eref_to_internal_eref1(e, key, url)
      id and m << id
    end
  end
end

#eref_to_internal_eref1(elem, key, url) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/metanorma/sectionsplit_links.rb', line 67

def eref_to_internal_eref1(elem, key, url)
  if url
    elem.name = "link"
    elem["target"] = url
    nil
  else
    elem["bibitemid"] = "#{key}_#{elem['bibitemid']}"
    elem << make_anchor(elem["bibitemid"])
    elem["type"] = key
    elem["bibitemid"]
  end
end

#eref_to_internal_eref_select(section, _xml, bibitems) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/metanorma/sectionsplit_links.rb', line 80

def eref_to_internal_eref_select(section, _xml, bibitems)
  refs = Util::gather_bibitemids(section).keys
  refs.uniq.reject do |x|
    b = bibitems[x] and (b["type"] == "internal" ||
                         b.at(ns("./docidentifier/@type = 'repository']")))
  end
end

#insert_indirect_biblio(ins, refs, prefix) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/metanorma/sectionsplit_links.rb', line 106

def insert_indirect_biblio(ins, refs, prefix)
  refs.each do |x|
    ins << <<~BIBENTRY
      <bibitem id="#{x}" type="internal">
      <docidentifier type="repository">#{x.sub(/^#{prefix}_/, "#{prefix}/")}</docidentifier>
      </bibitem>
    BIBENTRY
  end
end

#make_anchor(anchor) ⇒ Object



38
39
40
41
# File 'lib/metanorma/sectionsplit_links.rb', line 38

def make_anchor(anchor)
  "<localityStack><locality type='anchor'><referenceFrom>" \
    "#{anchor}</referenceFrom></locality></localityStack>"
end

#new_hidden_ref(xmldoc) ⇒ Object

from standoc



89
90
91
92
93
# File 'lib/metanorma/sectionsplit_links.rb', line 89

def new_hidden_ref(xmldoc)
  ins = xmldoc.at("bibliography") or
    xmldoc.root << "<bibliography/>" and ins = xmldoc.at("bibliography")
  ins.add_child("<references hidden='true' normative='false'/>").first
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) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/metanorma/sectionsplit.rb', line 187

def section_split_cover(col, ident)
  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
  FileUtils.mv "#{ident}_collection/index.html",
               File.join(dir, "#{ident}_index.html")
  FileUtils.rm_rf "#{ident}_collection"
  "#{ident}_index.html"
end

#sectionfile(fulldoc, xml, file, chunks, parentnode) ⇒ Object



130
131
132
133
134
# File 'lib/metanorma/sectionsplit.rb', line 130

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



147
148
149
150
151
152
153
# File 'lib/metanorma/sectionsplit.rb', line 147

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

#sectionsplitObject

Input XML is Semantic def sectionsplit(filename, basename, dir, compile_options, fileslookup = nil, ident = nil)



69
70
71
72
73
74
75
76
77
# File 'lib/metanorma/sectionsplit.rb', line 69

def sectionsplit
  xml = sectionsplit_prep(File.read(@input_filename), @base, @dir)
  @key = xref_preprocess(xml, @fileslookup, @ident)
  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



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/metanorma/sectionsplit.rb', line 95

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



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/metanorma/sectionsplit.rb', line 107

def sectionsplit_preprocess_semxml(file, filename)
  xml = Nokogiri::XML(file, &:huge)
  type = xml.root.name.sub("-standard", "").to_sym
  @fileslookup&.parent&.update_xrefs(xml, @ident, {})
  xml1 = Tempfile.open([filename, ".xml"], encoding: "utf-8") do |f|
    f.write(@isodoc.to_xml(svg_preprocess(xml)))
    f
  end
  @filecache ||= []
  @filecache << xml1
  [xml1.path, type]
end

#svg_preprocess(xml) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/metanorma/sectionsplit_links.rb', line 17

def svg_preprocess(xml)
  xml.xpath("//m:svg", "m" => "http://www.w3.org/2000/svg").each do |s|
    m = svgmap_wrap(s)
    s.xpath(".//m:a", "m" => "http://www.w3.org/2000/svg").each do |a|
      next unless /^#/.match? a["href"]

      a["href"] = a["href"].sub(/^#/, "")
      m << "<target href='#{a['href']}'>" \
           "<xref target='#{a['href']}'/></target>"
    end
  end
  xml
end

#svgmap_wrap(svg) ⇒ Object



31
32
33
34
35
36
# File 'lib/metanorma/sectionsplit_links.rb', line 31

def svgmap_wrap(svg)
  ret = svg.at("./ancestor::xmlns:svgmap") and return ret
  ret = svg.at("./ancestor::xmlns:figure")
  ret.wrap("<svgmap/>")
  svg.at("./ancestor::xmlns:svgmap")
end

#titlerender(section) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/metanorma/sectionsplit.rb', line 155

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

#xref_preprocess(xml, fileslookup, identifier) ⇒ Object



3
4
5
6
7
# File 'lib/metanorma/sectionsplit_links.rb', line 3

def xref_preprocess(xml, fileslookup, identifier)
  key = (0...8).map { rand(65..90).chr }.join # random string
  xml.root["type"] = key # to force recognition of internal refs
  key
end

#xref_process(section, xml, key) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/metanorma/sectionsplit_links.rb', line 9

def xref_process(section, xml, key)
  refs = eref_to_internal_eref(section, xml, key)
  refs += xref_to_internal_eref(section, key)
  ins = new_hidden_ref(section)
  copied_refs = copy_repo_items_biblio(ins, section, xml)
  insert_indirect_biblio(ins, refs - copied_refs, key)
end

#xref_to_internal_eref(xml, key) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/metanorma/sectionsplit_links.rb', line 43

def xref_to_internal_eref(xml, key)
  xml.xpath(ns("//xref")).each_with_object({}) do |x, m|
    x["bibitemid"] = "#{key}_#{x['target']}"
    x << make_anchor(x["target"])
    m[x["bibitemid"]] = true
    x.delete("target")
    x["type"] = key
    x.name = "eref"
  end.keys
end