Class: Metanorma::Collection
- Inherits:
-
Object
- Object
- Metanorma::Collection
show all
- Defined in:
- lib/metanorma/collection/collection.rb,
lib/metanorma/collection/util/util.rb,
lib/metanorma/collection/renderer/svg.rb,
lib/metanorma/collection/config/config.rb,
lib/metanorma/collection/config/bibdata.rb,
lib/metanorma/collection/renderer/utils.rb,
lib/metanorma/collection/config/manifest.rb,
lib/metanorma/collection/config/directive.rb,
lib/metanorma/collection/config/converters.rb,
lib/metanorma/collection/document/document.rb,
lib/metanorma/collection/manifest/manifest.rb,
lib/metanorma/collection/renderer/renderer.rb,
lib/metanorma/collection/renderer/fileparse.rb,
lib/metanorma/collection/renderer/navigation.rb,
lib/metanorma/collection/util/disambig_files.rb,
lib/metanorma/collection/renderer/fileprocess.rb,
lib/metanorma/collection/renderer/render_word.rb,
lib/metanorma/collection/filelookup/filelookup.rb,
lib/metanorma/collection/config/compile_options.rb,
lib/metanorma/collection/sectionsplit/collection.rb,
lib/metanorma/collection/xrefprocess/xrefprocess.rb,
lib/metanorma/collection/multilingual/multilingual.rb,
lib/metanorma/collection/sectionsplit/sectionsplit.rb,
lib/metanorma/collection/filelookup/filelookup_sectionsplit.rb
Overview
Metanorma collection of documents
Defined Under Namespace
Modules: Config, Util, XrefProcess
Classes: Document, FileLookup, Manifest, Multilingual, Renderer, Sectionsplit
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(**args) ⇒ Collection
Returns a new instance of Collection.
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/metanorma/collection/collection.rb', line 26
def initialize(**args)
@file = args[:file]
@dirname = File.expand_path(File.dirname(@file)) @documents = args[:documents] || {} @bibdatas = args[:documents] || {}
initialize_vars
initialize_config(args[:config])
initialize_directives
initialize_docs
validate_flavor(flavor)
end
|
Instance Attribute Details
#bibdata ⇒ Object
Returns the value of attribute bibdata.
21
22
23
|
# File 'lib/metanorma/collection/collection.rb', line 21
def bibdata
@bibdata
end
|
#bibdatas ⇒ Array<String>
Returns documents-inline to inject the XML into the collection manifest; documents-external to keeps them outside.
20
21
22
|
# File 'lib/metanorma/collection/collection.rb', line 20
def bibdatas
@bibdatas
end
|
#compile ⇒ Object
Returns the value of attribute compile.
21
22
23
|
# File 'lib/metanorma/collection/collection.rb', line 21
def compile
@compile
end
|
#config ⇒ Object
Returns the value of attribute config.
21
22
23
|
# File 'lib/metanorma/collection/collection.rb', line 21
def config
@config
end
|
#coverpage ⇒ Array<String>
Returns documents-inline to inject the XML into the collection manifest; documents-external to keeps them outside.
20
21
22
|
# File 'lib/metanorma/collection/collection.rb', line 20
def coverpage
@coverpage
end
|
#directives ⇒ Array<String>
Returns documents-inline to inject the XML into the collection manifest; documents-external to keeps them outside.
20
21
22
|
# File 'lib/metanorma/collection/collection.rb', line 20
def directives
@directives
end
|
#dirname ⇒ Array<String>
Returns documents-inline to inject the XML into the collection manifest; documents-external to keeps them outside.
20
21
22
|
# File 'lib/metanorma/collection/collection.rb', line 20
def dirname
@dirname
end
|
#disambig ⇒ Object
Returns the value of attribute disambig.
21
22
23
|
# File 'lib/metanorma/collection/collection.rb', line 21
def disambig
@disambig
end
|
#documents ⇒ Array<String>
Returns documents-inline to inject the XML into the collection manifest; documents-external to keeps them outside.
20
21
22
|
# File 'lib/metanorma/collection/collection.rb', line 20
def documents
@documents
end
|
#file ⇒ Object
Returns the value of attribute file.
16
17
18
|
# File 'lib/metanorma/collection/collection.rb', line 16
def file
@file
end
|
#final ⇒ String?
105
106
107
|
# File 'lib/metanorma/collection/collection.rb', line 105
def final
@final
end
|
#manifest ⇒ Object
Returns the value of attribute manifest.
21
22
23
|
# File 'lib/metanorma/collection/collection.rb', line 21
def manifest
@manifest
end
|
#prefatory ⇒ String?
105
106
107
|
# File 'lib/metanorma/collection/collection.rb', line 105
def prefatory
@prefatory
end
|
Class Method Details
.check_file_existence(filepath) ⇒ Object
228
229
230
231
232
233
234
|
# File 'lib/metanorma/collection/collection.rb', line 228
def check_file_existence(filepath)
unless File.exist?(filepath)
error_message = "#{filepath} not found!"
::Metanorma::Util.log("[metanorma] Error: #{error_message}", :error)
raise FileNotFoundException.new error_message.to_s
end
end
|
.parse(file) ⇒ Object
236
237
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/metanorma/collection/collection.rb', line 236
def parse(file)
@dirname = File.expand_path(File.dirname(file))
config = case file
when /\.xml$/
::Metanorma::Collection::Config::Config.from_xml(File.read(file))
when /.ya?ml$/
y = YAML.safe_load(File.read(file))
pre_parse_model(y)
::Metanorma::Collection::Config::Config.from_yaml(y.to_yaml)
end
new(file: file, config: config)
end
|
.pre_parse_model(collection_model) ⇒ Object
202
203
204
205
|
# File 'lib/metanorma/collection/collection.rb', line 202
def pre_parse_model(collection_model)
@pre_parse_model_proc or return
@pre_parse_model_proc.call(collection_model)
end
|
.resolve_fileref(ref_folder, fileref) ⇒ String
216
217
218
219
220
221
222
223
224
|
# File 'lib/metanorma/collection/collection.rb', line 216
def resolve_fileref(ref_folder, fileref)
unless @fileref_resolver
(Pathname.new fileref).absolute? or
fileref = File.join(ref_folder, fileref)
return fileref
end
@fileref_resolver.call(ref_folder, fileref)
end
|
.resolve_identifier(identifier) ⇒ String
209
210
211
212
|
# File 'lib/metanorma/collection/collection.rb', line 209
def resolve_identifier(identifier)
@identifier_resolver or return identifier
@identifier_resolver.call(identifier)
end
|
.set_fileref_resolver(&block) ⇒ Object
Note:
allow user-specific function to resolve fileref
NOTE: MUST ALWAYS RETURN PATH relative to working directory (initial YAML file location). @fileref_resolver.call(ref_folder, fileref) fileref is not what is in the YAML, but the resolved path relative to the working directory
193
194
195
|
# File 'lib/metanorma/collection/collection.rb', line 193
def set_fileref_resolver(&block)
@fileref_resolver = block
end
|
.set_identifier_resolver(&block) ⇒ Object
Note:
allow user-specific function to resolve identifier
183
184
185
|
# File 'lib/metanorma/collection/collection.rb', line 183
def set_identifier_resolver(&block)
@identifier_resolver = block
end
|
.set_pre_parse_model(&block) ⇒ Object
Note:
allow user-specific function to run in pre-parse model stage
177
178
179
|
# File 'lib/metanorma/collection/collection.rb', line 177
def set_pre_parse_model(&block)
@pre_parse_model_proc = block
end
|
.unset_fileref_resolver ⇒ Object
197
198
199
|
# File 'lib/metanorma/collection/collection.rb', line 197
def unset_fileref_resolver
@fileref_resolver = nil
end
|
Instance Method Details
#clean_exit ⇒ Object
79
80
81
82
|
# File 'lib/metanorma/collection/collection.rb', line 79
def clean_exit
@log.write(File.join(@dirname,
"#{File.basename(@file, '.*')}.err.html"))
end
|
#content_to_xml(elm, builder) ⇒ Object
118
119
120
121
122
123
|
# File 'lib/metanorma/collection/collection.rb', line 118
def content_to_xml(elm, builder)
(cnt = send(elm)) or return
@compile.load_flavor(flavor)
out = sections( + cnt.strip)
builder.send("#{elm}-content") { |b| b << out }
end
|
#doccontainer(builder) ⇒ Object
140
141
142
143
144
145
|
# File 'lib/metanorma/collection/collection.rb', line 140
def doccontainer(builder)
@directives.detect { |d| d.key == "documents-inline" } or return
documents.each_with_index do |(_, d), i|
doccontainer1(builder, d, i)
end
end
|
#doccontainer1(builder, doc, idx) ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/metanorma/collection/collection.rb', line 147
def doccontainer1(builder, doc, idx)
id = format("doc%<index>09d", index: idx)
builder.send(:"doc-container", id: id) do |b|
if doc.attachment
doc.bibitem and b << doc.bibitem.root.to_xml
b.attachment Vectory::Utils::datauri(doc.file)
else
doc.to_xml b
end
end
end
|
108
109
110
111
112
113
114
|
# File 'lib/metanorma/collection/collection.rb', line 108
def
<<~DUMMY
= X
A
DUMMY
end
|
#fetch_flavor ⇒ Object
TODO: retrieve flavor based on @bibdata publisher when lookup implemented Will still infer based on docid, but will validate it before proceeding
165
166
167
168
169
170
171
172
|
# File 'lib/metanorma/collection/collection.rb', line 165
def fetch_flavor
docid = @bibdata.docidentifier.first or return
f = docid.type.downcase || docid.id.sub(/\s.*$/, "").downcase or return
require ::Metanorma::Compile.new.stdtype2flavor(f)
f
rescue LoadError
nil
end
|
#flavor ⇒ Object
159
160
161
|
# File 'lib/metanorma/collection/collection.rb', line 159
def flavor
@flavor ||= fetch_flavor || "standoc"
end
|
#initialize_config(config) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/metanorma/collection/collection.rb', line 51
def initialize_config(config)
@config = config
@directives = config.directive || [] @bibdata = config.bibdata
@prefatory = config.prefatory_content
@final = config.final_content
@manifest = ::Metanorma::Collection::Manifest
.new(config.manifest, self, @dirname) @format = config.format.map(&:to_sym)
@format&.empty? and @format = nil
end
|
#initialize_directives ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/metanorma/collection/collection.rb', line 63
def initialize_directives
d = @directives.each_with_object({}) { |x, m| m[x.key] = x.value }
@coverpage = d["coverpage"]
@coverpage_style = d["coverpage-style"]
@flavor = d["flavor"]
if (@documents.any? || @manifest) && !d.key?("documents-inline") &&
!d.key?("documents-external")
@directives << ::Metanorma::Collection::Config::Directive
.new(key: "documents-inline")
end
end
|
#initialize_docs ⇒ Object
38
39
40
41
42
43
|
# File 'lib/metanorma/collection/collection.rb', line 38
def initialize_docs
@documents.merge! @manifest.documents
@bibdatas.merge! @manifest.documents
@documents.transform_keys { |k| Util::key(k) }
@bibdatas.transform_keys { |k| Util::key(k) }
end
|
#initialize_vars ⇒ Object
#render(opts) ⇒ Object
94
95
96
97
98
99
100
101
102
|
# File 'lib/metanorma/collection/collection.rb', line 94
def render(opts)
if opts[:format].nil? || opts[:format].empty?
opts[:format] = @format || [:html]
end
opts[:log] = @log
opts[:flavor] = @flavor
::Metanorma::Collection::Renderer.render self, opts
clean_exit
end
|
#sections(cnt) ⇒ String
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/metanorma/collection/collection.rb', line 127
def sections(cnt)
c = Asciidoctor.convert(cnt, backend: flavor.to_sym, header_footer: true)
x = Nokogiri::XML(c)
x.xpath("//xmlns:clause").each { |n| n["unnumbered"] = true }
file = Tempfile.new(%w(foo presentation.xml))
file.write(x.to_xml(indent: 0))
file.close
c1 = Util::isodoc_create(@flavor, @manifest.lang, @manifest.script, x, presxml: true)
.convert(file.path, nil, true)
Nokogiri::XML(c1).at("//xmlns:sections").children.to_xml
end
|
#to_xml ⇒ String
85
86
87
88
89
90
91
92
|
# File 'lib/metanorma/collection/collection.rb', line 85
def to_xml
c = ::Metanorma::Collection::Config::Config
.new(directive: @directives, bibdata: @bibdata,
manifest: @manifest.config, documents: @documents,
prefatory_content: @prefatory, final_content: @final)
c.collection = self
c.to_xml end
|
#validate_flavor(flavor) ⇒ Object