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
# File 'lib/metanorma/document.rb', line 7

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

Instance Attribute Details

#fileStrin (readonly)

Returns:

  • (Strin)


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

def file
  @file
end

Class Method Details

.parse_file(file) ⇒ Metanorma::Document

Parameters:

  • file (String)

    file path

Returns:



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

def parse_file(file)
  new bibitem(file), file
end

.parse_xml(xml) ⇒ Metanorma::Document

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

Returns:



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

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



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

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)


65
66
67
68
69
70
71
72
73
74
# File 'lib/metanorma/document.rb', line 65

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)


77
78
79
80
81
# File 'lib/metanorma/document.rb', line 77

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