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.
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/metanorma/collection/collection.rb', line 28
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.
23
24
25
|
# File 'lib/metanorma/collection/collection.rb', line 23
def bibdata
@bibdata
end
|
#bibdatas ⇒ Array<String>
Returns documents-inline to inject the XML into the collection manifest; documents-external to keeps them outside.
22
23
24
|
# File 'lib/metanorma/collection/collection.rb', line 22
def bibdatas
@bibdatas
end
|
#compile ⇒ Object
Returns the value of attribute compile.
23
24
25
|
# File 'lib/metanorma/collection/collection.rb', line 23
def compile
@compile
end
|
#config ⇒ Object
Returns the value of attribute config.
23
24
25
|
# File 'lib/metanorma/collection/collection.rb', line 23
def config
@config
end
|
#coverpage ⇒ Array<String>
Returns documents-inline to inject the XML into the collection manifest; documents-external to keeps them outside.
22
23
24
|
# File 'lib/metanorma/collection/collection.rb', line 22
def coverpage
@coverpage
end
|
#directives ⇒ Array<String>
Returns documents-inline to inject the XML into the collection manifest; documents-external to keeps them outside.
22
23
24
|
# File 'lib/metanorma/collection/collection.rb', line 22
def directives
@directives
end
|
#dirname ⇒ Array<String>
Returns documents-inline to inject the XML into the collection manifest; documents-external to keeps them outside.
22
23
24
|
# File 'lib/metanorma/collection/collection.rb', line 22
def dirname
@dirname
end
|
#disambig ⇒ Object
Returns the value of attribute disambig.
23
24
25
|
# File 'lib/metanorma/collection/collection.rb', line 23
def disambig
@disambig
end
|
#documents ⇒ Array<String>
Returns documents-inline to inject the XML into the collection manifest; documents-external to keeps them outside.
22
23
24
|
# File 'lib/metanorma/collection/collection.rb', line 22
def documents
@documents
end
|
#file ⇒ Object
Returns the value of attribute file.
18
19
20
|
# File 'lib/metanorma/collection/collection.rb', line 18
def file
@file
end
|
#final ⇒ String?
107
108
109
|
# File 'lib/metanorma/collection/collection.rb', line 107
def final
@final
end
|
#manifest ⇒ Object
Returns the value of attribute manifest.
23
24
25
|
# File 'lib/metanorma/collection/collection.rb', line 23
def manifest
@manifest
end
|
#prefatory ⇒ String?
107
108
109
|
# File 'lib/metanorma/collection/collection.rb', line 107
def prefatory
@prefatory
end
|
Class Method Details
.check_file_existence(filepath) ⇒ Object
223
224
225
226
227
228
229
|
# File 'lib/metanorma/collection/collection.rb', line 223
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
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# File 'lib/metanorma/collection/collection.rb', line 231
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
197
198
199
200
|
# File 'lib/metanorma/collection/collection.rb', line 197
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
211
212
213
214
215
216
217
218
219
|
# File 'lib/metanorma/collection/collection.rb', line 211
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
204
205
206
207
|
# File 'lib/metanorma/collection/collection.rb', line 204
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
188
189
190
|
# File 'lib/metanorma/collection/collection.rb', line 188
def set_fileref_resolver(&block)
@fileref_resolver = block
end
|
.set_identifier_resolver(&block) ⇒ Object
Note:
allow user-specific function to resolve identifier
178
179
180
|
# File 'lib/metanorma/collection/collection.rb', line 178
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
172
173
174
|
# File 'lib/metanorma/collection/collection.rb', line 172
def set_pre_parse_model(&block)
@pre_parse_model_proc = block
end
|
.unset_fileref_resolver ⇒ Object
192
193
194
|
# File 'lib/metanorma/collection/collection.rb', line 192
def unset_fileref_resolver
@fileref_resolver = nil
end
|
Instance Method Details
#clean_exit ⇒ Object
81
82
83
84
|
# File 'lib/metanorma/collection/collection.rb', line 81
def clean_exit
@log.write(File.join(@dirname,
"#{File.basename(@file, '.*')}.err.html"))
end
|
#content_to_xml(elm, builder) ⇒ Object
120
121
122
123
124
125
|
# File 'lib/metanorma/collection/collection.rb', line 120
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
135
136
137
138
139
140
|
# File 'lib/metanorma/collection/collection.rb', line 135
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
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/metanorma/collection/collection.rb', line 142
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
|
110
111
112
113
114
115
116
|
# File 'lib/metanorma/collection/collection.rb', line 110
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
160
161
162
163
164
165
166
167
|
# File 'lib/metanorma/collection/collection.rb', line 160
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
154
155
156
|
# File 'lib/metanorma/collection/collection.rb', line 154
def flavor
@flavor ||= fetch_flavor || "standoc"
end
|
#initialize_config(config) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/metanorma/collection/collection.rb', line 53
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
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/metanorma/collection/collection.rb', line 65
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
40
41
42
43
44
45
|
# File 'lib/metanorma/collection/collection.rb', line 40
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
96
97
98
99
100
101
102
103
104
|
# File 'lib/metanorma/collection/collection.rb', line 96
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
129
130
131
132
|
# File 'lib/metanorma/collection/collection.rb', line 129
def sections(cnt)
c = Asciidoctor.convert(cnt, backend: flavor.to_sym, header_footer: true)
Nokogiri::XML(c, &:huge).at("//xmlns:sections").children.to_xml
end
|
#to_xml ⇒ String
87
88
89
90
91
92
93
94
|
# File 'lib/metanorma/collection/collection.rb', line 87
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