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

def initialize(bibitem, file, options = {})
  @bibitem = bibitem
  @file = file
  @attachment = options[:attachment]
  @index = options[:index]
  @index = true if @index.nil?
  @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

#indexStrin (readonly)

Returns:

  • (Strin)


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

def index
  @index
end

Class Method Details

.attachment_bibitem(identifier) ⇒ Object



42
43
44
45
46
# File 'lib/metanorma/document.rb', line 42

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

.parse_file(file, attachment, identifier = nil, index = true) ⇒ 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

  • index (Bool) (defaults to: true)

    is indication on whether to index this file in coverpage

Returns:



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

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

.parse_xml(xml) ⇒ Metanorma::Document

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

Returns:



30
31
32
# File 'lib/metanorma/document.rb', line 30

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



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

def raw_file(filename)
  doc = Nokogiri::XML(File.read(filename, encoding: "UTF-8")) do |config|
    config.huge
  end
  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)


84
85
86
87
88
89
90
91
92
93
# File 'lib/metanorma/document.rb', line 84

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)


96
97
98
99
100
# File 'lib/metanorma/document.rb', line 96

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