Class: Metanorma::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/collection.rb

Overview

Metanorma collection of documents

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Collection

rubocop:disable Metrics/AbcSize,Metrics/MethodLength

Parameters:

  • file (String)

    path to source file

  • directives (Array<String>)

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

  • bibdata (RelatonBib::BibliographicItem)
  • manifest (Metanorma::CollectionManifest)
  • documents (Hash<String, Metanorma::Document>)
  • prefatory (String)
  • coverpage (String)
  • final (String)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/metanorma/collection.rb', line 29

def initialize(**args)
  @file = args[:file]
  @directives = args[:directives] || []
  @bibdata = args[:bibdata]
  @manifest = args[:manifest]
  @manifest.collection = self
  c = @directives.detect { |x| x.is_a?(Hash) && x["coverpage"] }
  c and @coverpage = c["coverpage"]
  c = @directives.detect { |x| x.is_a?(Hash) && x["coverpage-style"] }
  c and @coverpage_style = c["coverpage-style"]
  @documents = args[:documents] || {}
  @bibdatas = args[:documents] || {}
  if @documents.any? && !@directives.include?("documents-inline")
    @directives << "documents-inline"
  end
  @documents.merge! @manifest.documents(File.dirname(@file))
  @bibdatas.merge! @manifest.documents(File.dirname(@file))
  @prefatory = args[:prefatory]
  @final = args[:final]
  @log = Metanorma::Utils::Log.new
  @disambig = Util::DisambigFiles.new
end

Instance Attribute Details

#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



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

def bibdatas
  @bibdatas
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



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

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



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

def directives
  @directives
end

#disambigObject

Returns the value of attribute disambig.



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

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



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

def documents
  @documents
end

#fileObject (readonly)

Returns the value of attribute file.



12
13
14
# File 'lib/metanorma/collection.rb', line 12

def file
  @file
end

#manifestObject

Returns the value of attribute manifest.



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

def manifest
  @manifest
end

Class Method Details

.parse(file) ⇒ RelatonBib::BibliographicItem, RelatonIso::IsoBibliographicItem

Parameters:

  • file (String)

Returns:

  • (RelatonBib::BibliographicItem, RelatonIso::IsoBibliographicItem)


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

def parse(file)
  case file
  when /\.xml$/ then parse_xml(file)
  when /.ya?ml$/ then parse_yaml(file)
  end
end

Instance Method Details

#clean_exitObject

rubocop:enable Metrics/AbcSize,Metrics/MethodLength



53
54
55
56
# File 'lib/metanorma/collection.rb', line 53

def clean_exit
  @log.write(File.join(File.dirname(@file),
                       "#{File.basename(@file, '.*')}.err.html"))
end

#collection_body(coll) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/metanorma/collection.rb', line 69

def collection_body(coll)
  coll << @bibdata.to_xml(bibdata: true, date_format: :full)
  @directives.each do |d|
    coll << "<directives>#{obj_to_xml(d)}</directives>"
  end
  @manifest.to_xml coll
  content_to_xml "prefatory", coll
  doccontainer coll
  content_to_xml "final", coll
end

#obj_to_xml(elem) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/metanorma/collection.rb', line 80

def obj_to_xml(elem)
  case elem
  when ::Array
    elem.each_with_object([]) do |v, m|
      m << "<value>#{obj_to_xml(v)}</value>"
    end.join
  when ::Hash
    elem.each_with_object([]) do |(k, v), m|
      m << "<#{k}>#{obj_to_xml(v)}</#{k}>"
    end.join
  else elem end
end

#render(opts) ⇒ Object



93
94
95
96
# File 'lib/metanorma/collection.rb', line 93

def render(opts)
  CollectionRenderer.render self, opts.merge(log: @log)
  clean_exit
end

#to_xmlString

Returns XML.

Returns:

  • (String)

    XML



59
60
61
62
63
64
65
66
67
# File 'lib/metanorma/collection.rb', line 59

def to_xml
  b = Nokogiri::XML::Builder.new do |xml|
    xml.send(:"metanorma-collection",
             "xmlns" => "http://metanorma.org") do |mc|
      collection_body(mc)
    end
  end
  b.to_xml
end