Class: Metanorma::Collection::Manifest
- Inherits:
-
Object
- Object
- Metanorma::Collection::Manifest
- 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
- #collection ⇒ Metanorma::Collection readonly
- #config ⇒ Metanorma::Collection readonly
- #lang ⇒ Metanorma::Collection readonly
- #script ⇒ Metanorma::Collection readonly
Instance Method Summary collapse
- #clean_manifest(mnf) ⇒ Object
- #clean_manifest_bibdata(mnf) ⇒ Object
- #clean_manifest_id(mnf) ⇒ Object
- #compile_adoc(resolved_filename, rel_filename) ⇒ Object
-
#compile_adoc_file(file) ⇒ Object
param filepath [String].
- #compile_adoc_file?(file) ⇒ Boolean
- #docref_by_id(docid) ⇒ Object
- #docrefs ⇒ Array<Hash{String=>String}>
- #documents(mnf = @config) ⇒ Object
- #documents_add(docref) ⇒ Object
-
#initialize(config, collection, dir) ⇒ Manifest
constructor
A new instance of Manifest.
- #load_isodoc ⇒ Object
- #manifest_bibdata(config) ⇒ Object
- #manifest_compile_adoc(config) ⇒ Object
- #manifest_expand_yaml(config, dir) ⇒ Object
- #manifest_expand_yaml_entry(entry, dir) ⇒ Object
- #manifest_filexist(config) ⇒ Object
- #manifest_identifier(config) ⇒ Object
- #manifest_postprocess(config) ⇒ Object
- #manifest_sectionsplit(config) ⇒ Object
- #populate_id_from_doc(config) ⇒ Object
- #retrieve_id_from_doc(file) ⇒ Object
- #set_adoc2xml(fileref) ⇒ Object
- #to_xml(builder = nil) ⇒ Object
- #update_filepaths(entry, prefix) ⇒ Object
Constructor Details
#initialize(config, collection, dir) ⇒ Manifest
Returns a new instance of Manifest.
16 17 18 19 20 21 22 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 16 def initialize(config, collection, dir) #require "debug"; binding.b @collection = collection @dir = dir @disambig = ::Metanorma::Collection::Util::DisambigFiles.new @config = manifest_postprocess(config) end |
Instance Attribute Details
#collection ⇒ Metanorma::Collection (readonly)
9 10 11 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 9 def collection @collection end |
#config ⇒ Metanorma::Collection (readonly)
9 10 11 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 9 def config @config end |
#lang ⇒ Metanorma::Collection (readonly)
9 10 11 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 9 def lang @lang end |
#script ⇒ Metanorma::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
227 228 229 230 231 232 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 227 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
210 211 212 213 214 215 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 210 def clean_manifest_bibdata(mnf) if mnf.file && !mnf. && !mnf.sectionsplit && @collection && d = @collection.bibdatas[Util::key mnf.identifier] mnf.bibdata = d.bibitem.dup end end |
#clean_manifest_id(mnf) ⇒ Object
217 218 219 220 221 222 223 224 225 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 217 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
149 150 151 152 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 149 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]
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 164 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
175 176 177 178 179 180 181 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 175 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
239 240 241 242 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 239 def docref_by_id(docid) @config.entry.detect { |k| k.identifier == docid } || @config.entry.detect { |k| /^#{k.identifier}/ =~ docid } end |
#docrefs ⇒ Array<Hash{String=>String}>
235 236 237 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 235 def docrefs @config.entry end |
#documents(mnf = @config) ⇒ Object
183 184 185 186 187 188 189 190 191 192 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 183 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
194 195 196 197 198 199 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 194 def documents_add(docref) ::Metanorma::Collection::Document.parse_file( Util::rel_path_resolve(@dir, docref.file), docref., docref.identifier, docref.index ) end |
#load_isodoc ⇒ Object
77 78 79 80 81 82 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 77 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
35 36 37 38 39 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 35 def manifest_bibdata(config) b = config.bibdata @lang = b&.language&.first || "en" @script = b&.script&.first || "Latn" end |
#manifest_compile_adoc(config) ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 139 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
108 109 110 111 112 113 114 115 116 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 108 def (config, dir) Array(config.entry).each do |e| currdir = dir /\.ya?ml$/.match?(e.file) and currdir = (e, dir) (e, currdir) end config end |
#manifest_expand_yaml_entry(entry, dir) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 118 def (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
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 97 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
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 43 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
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 24 def manifest_postprocess(config) #require "debug"; binding.b manifest_bibdata(config) (config, @dir) manifest_compile_adoc(config) manifest_filexist(config) manifest_sectionsplit(config) manifest_identifier(config) config end |
#manifest_sectionsplit(config) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 84 def manifest_sectionsplit(config) if config.sectionsplit && !config.file config.sectionsplit = nil Array(config.entry).each do |e| e. and next e.sectionsplit = true end end Array(config.entry).each do |f| manifest_sectionsplit(f) end end |
#populate_id_from_doc(config) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 56 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
67 68 69 70 71 72 73 74 75 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 67 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
155 156 157 158 159 160 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 155 def set_adoc2xml(fileref) File.join( File.dirname(fileref), File.basename(fileref).gsub(/\.adoc$/, ".xml"), ) end |
#to_xml(builder = nil) ⇒ Object
201 202 203 204 205 206 207 208 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 201 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
131 132 133 134 135 136 137 |
# File 'lib/metanorma/collection/manifest/manifest.rb', line 131 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 |