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:



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)) # 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.



23
24
25
# File 'lib/metanorma/collection/collection.rb', line 23

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



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

def bibdatas
  @bibdatas
end

#compileObject

Returns the value of attribute compile.



23
24
25
# File 'lib/metanorma/collection/collection.rb', line 23

def compile
  @compile
end

#configObject

Returns the value of attribute config.



23
24
25
# File 'lib/metanorma/collection/collection.rb', line 23

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



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

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



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

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



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

def dirname
  @dirname
end

#disambigObject

Returns the value of attribute disambig.



23
24
25
# File 'lib/metanorma/collection/collection.rb', line 23

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



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

def documents
  @documents
end

#fileObject (readonly)

Returns the value of attribute file.



18
19
20
# File 'lib/metanorma/collection/collection.rb', line 18

def file
  @file
end

#finalString? (readonly)

Returns:

  • (String, nil)


107
108
109
# File 'lib/metanorma/collection/collection.rb', line 107

def final
  @final
end

#manifestObject

Returns the value of attribute manifest.



23
24
25
# File 'lib/metanorma/collection/collection.rb', line 23

def manifest
  @manifest
end

#prefatoryString? (readonly)

Returns:

  • (String, nil)


107
108
109
# File 'lib/metanorma/collection/collection.rb', line 107

def prefatory
  @prefatory
end

Class Method Details

.check_file_existence(filepath) ⇒ Object

Parameters:

  • filepath

Raises:



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


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

Parameters:

  • fileref (String)

Returns:

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

Parameters:

  • identifier (String)

Returns:

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

Parameters:

  • Block (Proc)


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

Parameters:

  • Block (Proc)


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

Parameters:

  • Block (Proc)


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_resolverObject



192
193
194
# File 'lib/metanorma/collection/collection.rb', line 192

def unset_fileref_resolver
  @fileref_resolver = nil
end

Instance Method Details

#clean_exitObject



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

Parameters:

  • elm (String)

    ‘prefatory’ or ‘final’

  • builder (Nokogiri::XML::Builder)


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

#doccontainer(builder) ⇒ Object

Parameters:

  • builder (Nokogiri::XML::Builder)


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

#dummy_headerString

Returns:

  • (String)


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

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



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

#flavorObject



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 || [] # 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



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_docsObject



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_varsObject



47
48
49
50
51
# File 'lib/metanorma/collection/collection.rb', line 47

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

#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

Returns XML.

Parameters:

  • cnt (String)

    prefatory/final content

Returns:

  • (String)

    XML



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_xmlString

Returns XML.

Returns:

  • (String)

    XML



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

#validate_flavor(flavor) ⇒ Object



77
78
79
# File 'lib/metanorma/collection/collection.rb', line 77

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