Class: Metanorma::Collection

Inherits:
Object
  • Object
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.

Parameters:



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)) # feeds @manifest
  @documents = args[:documents] || {} # feeds initialize_directives, initialize_docs
  @bibdatas = args[:documents] || {}
  initialize_vars
  initialize_config(args[:config])
  initialize_directives
  initialize_docs
  validate_flavor(flavor)
end

Instance Attribute Details

#bibdataObject

Returns the value of attribute bibdata.



21
22
23
# File 'lib/metanorma/collection/collection.rb', line 21

def bibdata
  @bibdata
end

#bibdatasArray<String>

Returns documents-inline to inject the XML into the collection manifest; documents-external to keeps them outside.

Returns:

  • (Array<String>)

    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

#compileObject

Returns the value of attribute compile.



21
22
23
# File 'lib/metanorma/collection/collection.rb', line 21

def compile
  @compile
end

#configObject

Returns the value of attribute config.



21
22
23
# File 'lib/metanorma/collection/collection.rb', line 21

def config
  @config
end

#coverpageArray<String>

Returns documents-inline to inject the XML into the collection manifest; documents-external to keeps them outside.

Returns:

  • (Array<String>)

    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

#directivesArray<String>

Returns documents-inline to inject the XML into the collection manifest; documents-external to keeps them outside.

Returns:

  • (Array<String>)

    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

#dirnameArray<String>

Returns documents-inline to inject the XML into the collection manifest; documents-external to keeps them outside.

Returns:

  • (Array<String>)

    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

#disambigObject

Returns the value of attribute disambig.



21
22
23
# File 'lib/metanorma/collection/collection.rb', line 21

def disambig
  @disambig
end

#documentsArray<String>

Returns documents-inline to inject the XML into the collection manifest; documents-external to keeps them outside.

Returns:

  • (Array<String>)

    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

#fileObject (readonly)

Returns the value of attribute file.



16
17
18
# File 'lib/metanorma/collection/collection.rb', line 16

def file
  @file
end

#finalString? (readonly)

Returns:

  • (String, nil)


105
106
107
# File 'lib/metanorma/collection/collection.rb', line 105

def final
  @final
end

#manifestObject

Returns the value of attribute manifest.



21
22
23
# File 'lib/metanorma/collection/collection.rb', line 21

def manifest
  @manifest
end

#prefatoryString? (readonly)

Returns:

  • (String, nil)


105
106
107
# File 'lib/metanorma/collection/collection.rb', line 105

def prefatory
  @prefatory
end

Class Method Details

.check_file_existence(filepath) ⇒ Object

Parameters:

  • filepath

Raises:



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)
  # need @dirname initialised before collection object initialisation
  @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

Parameters:

  • collection_model (Hash{String=>String})


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

Parameters:

  • fileref (String)

Returns:

  • (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

Parameters:

  • identifier (String)

Returns:

  • (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

Parameters:

  • Block (Proc)


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

Parameters:

  • Block (Proc)


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

Parameters:

  • Block (Proc)


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_resolverObject



197
198
199
# File 'lib/metanorma/collection/collection.rb', line 197

def unset_fileref_resolver
  @fileref_resolver = nil
end

Instance Method Details

#clean_exitObject



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

Parameters:

  • elm (String)

    ‘prefatory’ or ‘final’

  • builder (Nokogiri::XML::Builder)


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(dummy_header + cnt.strip)
  builder.send("#{elm}-content") { |b| b << out }
end

#doccontainer(builder) ⇒ Object

Parameters:

  • builder (Nokogiri::XML::Builder)


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

#dummy_headerString

Returns:

  • (String)


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

def dummy_header
  <<~DUMMY
    = X
    A

  DUMMY
end

#fetch_flavorObject

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

#flavorObject



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 || [] # feeds initialize_directives
  @bibdata = config.bibdata
  @prefatory = config.prefatory_content
  @final = config.final_content
  @manifest = ::Metanorma::Collection::Manifest
    .new(config.manifest, self, @dirname) # feeds initialize_directives
  @format = config.format.map(&:to_sym)
  @format&.empty? and @format = nil
end

#initialize_directivesObject



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_docsObject



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_varsObject



45
46
47
48
49
# File 'lib/metanorma/collection/collection.rb', line 45

def initialize_vars
  @compile = Metanorma::Compile.new # feeds @manifest
  @log = Metanorma::Utils::Log.new
  @disambig = Util::DisambigFiles.new
end

#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

Returns XML.

Parameters:

  • cnt (String)

    prefatory/final content

Returns:

  • (String)

    XML



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_xmlString

Returns XML.

Returns:

  • (String)

    XML



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 # .sub("<metanorma-collection", "<metanorma-collection xmlns='http://metanorma.org'")
end

#validate_flavor(flavor) ⇒ Object



75
76
77
# File 'lib/metanorma/collection/collection.rb', line 75

def validate_flavor(flavor)
  ::Metanorma::Compile.new.load_flavor(flavor)
end