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



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

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



207
208
209
210
211
212
# File 'lib/metanorma/collection/manifest/manifest.rb', line 207

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



214
215
216
217
218
219
220
221
222
# File 'lib/metanorma/collection/manifest/manifest.rb', line 214

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
# File 'lib/metanorma/collection/manifest/manifest.rb', line 162

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}...", :info)
  ::Metanorma::Compile.new
    .compile(f, agree_to_terms: true, install_fonts: false,
                extension_keys: [:xml])
  ::Metanorma::Util.log("[metanorma] Info: Compiling #{f}...done!", :info)
end

#compile_adoc_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#docref_by_id(docid) ⇒ Object



236
237
238
239
# File 'lib/metanorma/collection/manifest/manifest.rb', line 236

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


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

def docrefs
  @config.entry
end

#documents(mnf = @config) ⇒ Object



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

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



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

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(@flavor)
  @isodoc = Util::load_isodoc(@flavor)
  @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
  @flavor = @collection.flavor
  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



198
199
200
201
202
203
204
205
# File 'lib/metanorma/collection/manifest/manifest.rb', line 198

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