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)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/metanorma/collection.rb', line 34

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

#bibdatasHash<String, Metanorma::Document>

Returns:



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

def bibdatas
  @bibdatas
end

#coverpageHash<String, Metanorma::Document>

Returns:



20
21
22
# File 'lib/metanorma/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



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

def directives
  @directives
end

#disambigObject

Returns the value of attribute disambig.



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

def disambig
  @disambig
end

#documentsHash<String, Metanorma::Document>

Returns:



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

def documents
  @documents
end

#fileString (readonly)

Returns:

  • (String)


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

def file
  @file
end

#manifestObject

Returns the value of attribute manifest.



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

def manifest
  @manifest
end

Class Method Details

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

Parameters:

  • file (String)

Returns:

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


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

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



58
59
60
61
# File 'lib/metanorma/collection.rb', line 58

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

#collection_body(coll) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/metanorma/collection.rb', line 74

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



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/metanorma/collection.rb', line 85

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



99
100
101
102
# File 'lib/metanorma/collection.rb', line 99

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

#to_xmlString

Returns XML.

Returns:

  • (String)

    XML



64
65
66
67
68
69
70
71
72
# File 'lib/metanorma/collection.rb', line 64

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