Class: Metanorma::Document

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bibitem, file, options = {}) ⇒ Document

Returns a new instance of Document.

Parameters:

  • bibitem (RelatonBib::BibliographicItem)


7
8
9
10
11
12
# File 'lib/metanorma/document.rb', line 7

def initialize(bibitem, file, options = {})
  @bibitem = bibitem
  @file = file
  @attachment = options[:attachment]
  @raw = options[:raw]
end

Instance Attribute Details

#attachmentStrin (readonly)

Returns:

  • (Strin)


4
5
6
# File 'lib/metanorma/document.rb', line 4

def attachment
  @attachment
end

#bibitemStrin (readonly)

Returns:

  • (Strin)


4
5
6
# File 'lib/metanorma/document.rb', line 4

def bibitem
  @bibitem
end

#fileStrin (readonly)

Returns:

  • (Strin)


4
5
6
# File 'lib/metanorma/document.rb', line 4

def file
  @file
end

Class Method Details

.attachment_bibitem(identifier) ⇒ Object



37
38
39
40
41
# File 'lib/metanorma/document.rb', line 37

def attachment_bibitem(identifier)
  Nokogiri::XML(
    "<bibdata><docidentifier>#{identifier}</docidentifier></bibdata>",
  )
end

.parse_file(file, attachment, identifier = nil) ⇒ Metanorma::Document

in the collection file

Parameters:

  • file (String)

    file path

  • attachment (Bool)

    is an attachment

  • identifier (String) (defaults to: nil)

    is the identifier assigned the file

Returns:



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

def parse_file(file, attachment, identifier = nil)
  new(bibitem(file, attachment, identifier), file,
      { attachment: attachment })
end

.parse_xml(xml) ⇒ Metanorma::Document

#param xml [Nokogiri::XML::Document, Nokogiri::XML::Element]

Returns:



27
28
29
# File 'lib/metanorma/document.rb', line 27

def parse_xml(xml)
  new from_xml(xml)
end

.raw_file(filename) ⇒ Object

raw XML file, can be used to put in entire file instead of just bibitem



32
33
34
35
# File 'lib/metanorma/document.rb', line 32

def raw_file(filename)
  doc = Nokogiri::XML(File.read(filename, encoding: "UTF-8"))
  new(doc, filename, raw: true)
end

Instance Method Details

#to_xml(builder = nil) ⇒ Nokogiri::XML::Builder, String

Parameters:

  • builder (Nokogiri::XML::Builder, nil) (defaults to: nil)

Returns:

  • (Nokogiri::XML::Builder, String)


79
80
81
82
83
84
85
86
87
88
# File 'lib/metanorma/document.rb', line 79

def to_xml(builder = nil)
  if builder
    render_xml builder
  else
    Nokogiri::XML::Builder.new do |b|
      root = render_xml b
      root["xmlns"] = "http://metanorma.org"
    end.to_xml
  end
end

#typeString

Returns:

  • (String)


91
92
93
94
95
# File 'lib/metanorma/document.rb', line 91

def type
  @type ||= (@bibitem.docidentifier.first&.type&.downcase ||
             @bibitem.docidentifier.first&.id&.match(/^[^\s]+/)&.to_s)&.downcase ||
  "standoc"
end