Class: Metanorma::Collection::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/collection/manifest/manifest.rb

Constant Summary collapse

GUID =
"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, collection, dir) ⇒ Manifest

Returns a new instance of Manifest.

Parameters:



16
17
18
19
20
21
# File 'lib/metanorma/collection/manifest/manifest.rb', line 16

def initialize(config, collection, dir)
  @collection = collection
  @dir = dir
  @disambig = ::Metanorma::Collection::Util::DisambigFiles.new
  @config = manifest_postprocess(config)
end

Instance Attribute Details

#collectionMetanorma::Collection (readonly)



9
10
11
# File 'lib/metanorma/collection/manifest/manifest.rb', line 9

def collection
  @collection
end

#configMetanorma::Collection (readonly)



9
10
11
# File 'lib/metanorma/collection/manifest/manifest.rb', line 9

def config
  @config
end

#langMetanorma::Collection (readonly)



9
10
11
# File 'lib/metanorma/collection/manifest/manifest.rb', line 9

def lang
  @lang
end

#scriptMetanorma::Collection (readonly)



9
10
11
# File 'lib/metanorma/collection/manifest/manifest.rb', line 9

def script
  @script
end

Instance Method Details

#clean_manifest(mnf) ⇒ Object



244
245
246
247
248
249
# File 'lib/metanorma/collection/manifest/manifest.rb', line 244

def clean_manifest(mnf)
  clean_manifest_bibdata(mnf)
  # mnf.file &&= @disambig.strip_root(mnf.file)
  clean_manifest_id(mnf)
  Array(mnf.entry).each { |e| clean_manifest(e) }
end

#clean_manifest_bibdata(mnf) ⇒ Object



227
228
229
230
231
232
# File 'lib/metanorma/collection/manifest/manifest.rb', line 227

def clean_manifest_bibdata(mnf)
  if mnf.file && !mnf.attachment && !mnf.sectionsplit && @collection &&
      d = @collection.bibdatas[Util::key mnf.identifier]
    mnf.bibdata = d.bibitem.dup
  end
end

#clean_manifest_id(mnf) ⇒ Object



234
235
236
237
238
239
240
241
242
# File 'lib/metanorma/collection/manifest/manifest.rb', line 234

def clean_manifest_id(mnf)
  @collection.directives.detect do |d|
    d.key == "documents-inline"
  end or return
  id = @collection.documents.find_index do |k, _|
    k == mnf.identifier
  end
  id and mnf.id = format("doc%<index>09d", index: id)
end

#compile_adoc(resolved_filename, rel_filename) ⇒ Object



165
166
167
168
# File 'lib/metanorma/collection/manifest/manifest.rb', line 165

def compile_adoc(resolved_filename, rel_filename)
  compile_adoc_file(resolved_filename)
  set_adoc2xml(rel_filename)
end

#compile_adoc_file(file) ⇒ Object

param filepath [String]



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/metanorma/collection/manifest/manifest.rb', line 180

def compile_adoc_file(file)
  f = (Pathname.new file).absolute? ? file : File.join(@dir, file)
  File.exist?(f) or raise AdocFileNotFoundException.new "#{f} not found!"
  compile_adoc_file?(file) or return
  ::Metanorma::Util.log("[metanorma] Info: Compiling #{f}...", :warning)
  ::Metanorma::Compile.new
    .compile(f, agree_to_terms: true, install_fonts: false,
                extension_keys: [:xml])
  ::Metanorma::Util.log("[metanorma] Info: Compiling #{f}...done!",
                        :warning)
end

#compile_adoc_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


192
193
194
195
196
197
198
# File 'lib/metanorma/collection/manifest/manifest.rb', line 192

def compile_adoc_file?(file)
  !File.exist?(file.sub(/\.adoc$/, ".xml")) and return true
  @collection.directives.detect do |d|
    d.key == "recompile-xml" && d.value == "false"
  end and return false
  true
end

#docref_by_id(docid) ⇒ Object



256
257
258
259
# File 'lib/metanorma/collection/manifest/manifest.rb', line 256

def docref_by_id(docid)
  @config.entry.detect { |k| k.identifier == docid } ||
    @config.entry.detect { |k| /^#{k.identifier}/ =~ docid }
end

#docrefsArray<Hash{String=>String}>

Returns:

  • (Array<Hash{String=>String}>)


252
253
254
# File 'lib/metanorma/collection/manifest/manifest.rb', line 252

def docrefs
  @config.entry
end

#documents(mnf = @config) ⇒ Object



200
201
202
203
204
205
206
207
208
209
# File 'lib/metanorma/collection/manifest/manifest.rb', line 200

def documents(mnf = @config)
  Array(mnf.entry).each_with_object({}) do |dr, m|
    if dr.file
      m[Util::key dr.identifier] = documents_add(dr)
    elsif dr.entry
      m.merge! documents(dr)
    end
    m
  end
end

#documents_add(docref) ⇒ Object



211
212
213
214
215
216
# File 'lib/metanorma/collection/manifest/manifest.rb', line 211

def documents_add(docref)
  ::Metanorma::Collection::Document.parse_file(
    Util::rel_path_resolve(@dir, docref.file),
    docref.attachment, docref.identifier, docref.index
  )
end

#load_isodocObject



93
94
95
96
97
98
# File 'lib/metanorma/collection/manifest/manifest.rb', line 93

def load_isodoc
  @isodoc and return
  @collection.compile.load_flavor(@flavor)
  @isodoc = Util::load_isodoc(@flavor)
  @isodoc.i18n_init(@lang, @script, nil) # for @i18n.all_parts in docid
end

#manifest_bibdata(config) ⇒ Object



40
41
42
43
44
# File 'lib/metanorma/collection/manifest/manifest.rb', line 40

def manifest_bibdata(config)
  b = config.bibdata
  @lang = b&.language&.first || "en"
  @script = b&.script&.first || "Latn"
end

#manifest_compile_adoc(config) ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/metanorma/collection/manifest/manifest.rb', line 155

def manifest_compile_adoc(config)
  if /\.adoc$/.match?(config.file)
    file = @collection.class.resolve_fileref(@dir, config.file)
    config.file = compile_adoc(file, config.file)
  end
  Array(config.entry).each do |f|
    manifest_compile_adoc(f)
  end
end

#manifest_expand_yaml(config, dir) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/metanorma/collection/manifest/manifest.rb', line 124

def manifest_expand_yaml(config, dir)
  Array(config.entry).each do |e|
    currdir = dir
    /\.ya?ml$/.match?(e.file) and
      currdir = manifest_expand_yaml_entry(e, dir)
    manifest_expand_yaml(e, currdir)
  end
  config
end

#manifest_expand_yaml_entry(entry, dir) ⇒ Object



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

def manifest_expand_yaml_entry(entry, dir)
  f = @collection.class.resolve_fileref(dir, entry.file)
  currdir = File.dirname(f)
  @collection.class.check_file_existence(f)
  entry.file = nil
  entry.entry = ::Metanorma::Collection::Config::Config.from_yaml(File.read(f)).manifest
  if currdir != dir
    prefix = Pathname.new(currdir).relative_path_from(Pathname.new(dir))
    update_filepaths(entry.entry, prefix.to_s)
  end
  currdir
end

#manifest_filexist(config) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/metanorma/collection/manifest/manifest.rb', line 113

def manifest_filexist(config)
  if config.file
    file = @collection.class.resolve_fileref(@dir, config.file)
    @collection.class.check_file_existence(file)
    config.file = Pathname.new(file).relative_path_from(Pathname.new(@dir))
  end
  Array(config.entry).each do |f|
    manifest_filexist(f)
  end
end

#manifest_identifier(config) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/metanorma/collection/manifest/manifest.rb', line 59

def manifest_identifier(config)
  no_id = populate_id_from_doc(config)
  config.identifier =
    if no_id && config.file # make file name be id
      @collection.class.resolve_identifier(File.basename(config.file))
    else
      @collection.class.resolve_identifier(config.identifier)
    end
  Array(config.entry).each do |f|
    manifest_identifier(f)
  end
end

#manifest_output_filenames(config, parent = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/metanorma/collection/manifest/manifest.rb', line 48

def manifest_output_filenames(config, parent = nil)
  config.output_filename ||=
    parent&.output_filename # || "{basename}.{document-num}"
  config.sectionsplit_filename ||= parent&.sectionsplit_filename ||
    "{basename_legacy}.{sectionsplit-num}"
  # "{basename}-{document-num}.{sectionsplit-num}"
  Array(config.entry).each do |f|
    manifest_output_filenames(f, config)
  end
end

#manifest_postprocess(config) ⇒ Object

Ordered pipeline of in-place tree-walks over the manifest; the order is load-bearing. expand_yaml must run early because it grafts nested fileref manifests into this tree, so every later pass sees the full entry set; bibdata sets @lang/@script used downstream; compile_adoc turns .adoc sources into .xml before filexist verifies them. Each pass recurses the manifest independently (see the architecture review plan).



29
30
31
32
33
34
35
36
37
38
# File 'lib/metanorma/collection/manifest/manifest.rb', line 29

def manifest_postprocess(config)
  manifest_bibdata(config)
  manifest_expand_yaml(config, @dir)
  manifest_output_filenames(config)
  manifest_compile_adoc(config)
  manifest_filexist(config)
  manifest_sectionsplit(config)
  manifest_identifier(config)
  config
end

#manifest_sectionsplit(config) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/metanorma/collection/manifest/manifest.rb', line 100

def manifest_sectionsplit(config)
  if config.sectionsplit && !config.file
    config.sectionsplit = nil
    Array(config.entry).each do |e|
      e.attachment and next
      e.sectionsplit = true
    end
  end
  Array(config.entry).each do |f|
    manifest_sectionsplit(f)
  end
end

#populate_id_from_doc(config) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/metanorma/collection/manifest/manifest.rb', line 72

def populate_id_from_doc(config)
  no_id = /^#{GUID}$/o.match?(config.identifier)
  # GUID assumed to be no identifier supplied
  if no_id && /\.xml$/.match?(config.file) &&
      (i = retrieve_id_from_doc(config.file))
    config.identifier = i
    no_id = false
  end
  no_id
end

#retrieve_id_from_doc(file) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/metanorma/collection/manifest/manifest.rb', line 83

def retrieve_id_from_doc(file)
  x = Nokogiri::XML(File.read(File.join(@dir, file)), &:huge)
  i = x.at("//xmlns:bibdata/xmlns:docidentifier[@primary = 'true']") ||
    x.at("//xmlns:bibdata/xmlns:docidentifier")
  i or return nil
  @flavor = @collection.flavor
  load_isodoc
  Util::key(@isodoc.docid_prefix(i["type"], i.text))
end

#set_adoc2xml(fileref) ⇒ Object

Parameters:

  • fileref (String)


171
172
173
174
175
176
# File 'lib/metanorma/collection/manifest/manifest.rb', line 171

def set_adoc2xml(fileref)
  File.join(
    File.dirname(fileref),
    File.basename(fileref).gsub(/\.adoc$/, ".xml"),
  )
end

#to_xml(builder = nil) ⇒ Object



218
219
220
221
222
223
224
225
# File 'lib/metanorma/collection/manifest/manifest.rb', line 218

def to_xml(builder = nil)
  clean_manifest(@config)
  if builder
    builder.parent.add_child(@config.to_xml)
  else
    @config.to_xml
  end
end

#update_filepaths(entry, prefix) ⇒ Object



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

def update_filepaths(entry, prefix)
  entry.file && !(Pathname.new entry.file).absolute? and
    entry.file = File.join(prefix, entry.file)
  entry&.entry&.each do |f|
    update_filepaths(f, prefix)
  end
end