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

Instance Method Details

#clean_manifest(mnf) ⇒ Object



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

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



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

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



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

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



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

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]



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/metanorma/collection/manifest/manifest.rb', line 162

def compile_adoc_file(file)
  f = (Pathname.new file).absolute? ? file : File.join(@dir, file)
  unless File.exist? f
    raise AdocFileNotFoundException.new "#{f} not found!"
  end

  ::Metanorma::Util.log("[metanorma] Info: Compiling #{f}...", :info)
  ::Metanorma::Compile.new
    .compile(f, agree_to_terms: true, no_install_fonts: true,
                extension_keys: [:xml])
  ::Metanorma::Util.log("[metanorma] Info: Compiling #{f}...done!", :info)
end

#docref_by_id(docid) ⇒ Object



231
232
233
234
# File 'lib/metanorma/collection/manifest/manifest.rb', line 231

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}>)


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

def docrefs
  @config.entry
end

#documents(mnf = @config) ⇒ Object



175
176
177
178
179
180
181
182
183
184
# File 'lib/metanorma/collection/manifest/manifest.rb', line 175

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



186
187
188
189
190
191
# File 'lib/metanorma/collection/manifest/manifest.rb', line 186

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



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

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

#manifest_bibdata(config) ⇒ Object



33
34
35
36
37
# File 'lib/metanorma/collection/manifest/manifest.rb', line 33

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

#manifest_compile_adoc(config) ⇒ Object



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

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



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

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



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/metanorma/collection/manifest/manifest.rb', line 116

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



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

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



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

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_postprocess(config) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/metanorma/collection/manifest/manifest.rb', line 23

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

#manifest_sectionsplit(config) ⇒ Object



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

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



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

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



65
66
67
68
69
70
71
72
73
# File 'lib/metanorma/collection/manifest/manifest.rb', line 65

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
  @doctype ||= i["type"]&.downcase || "standoc"
  load_isodoc
  Util::key(@isodoc.docid_prefix(i["type"], i.text))
end

#set_adoc2xml(fileref) ⇒ Object

Parameters:

  • fileref (String)


153
154
155
156
157
158
# File 'lib/metanorma/collection/manifest/manifest.rb', line 153

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

#to_xml(builder = nil) ⇒ Object



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

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



129
130
131
132
133
134
135
# File 'lib/metanorma/collection/manifest/manifest.rb', line 129

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