Class: Metanorma::Collection::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/collection/document/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)


10
11
12
13
14
15
16
17
# File 'lib/metanorma/collection/document/document.rb', line 10

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)


7
8
9
# File 'lib/metanorma/collection/document/document.rb', line 7

def attachment
  @attachment
end

#bibitemStrin (readonly)

Returns:

  • (Strin)


7
8
9
# File 'lib/metanorma/collection/document/document.rb', line 7

def bibitem
  @bibitem
end

#fileStrin (readonly)

Returns:

  • (Strin)


7
8
9
# File 'lib/metanorma/collection/document/document.rb', line 7

def file
  @file
end

#indexStrin (readonly)

Returns:

  • (Strin)


7
8
9
# File 'lib/metanorma/collection/document/document.rb', line 7

def index
  @index
end

Class Method Details

.attachment_bibitem(identifier) ⇒ Object



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

def attachment_bibitem(identifier)
  Nokogiri::XML <<~DOCUMENT
    <bibdata><docidentifier>#{identifier}</docidentifier></bibdata>
  DOCUMENT
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:

  • (Metanorma::Document)


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

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:

  • (Metanorma::Document)


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

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



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

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


103
104
105
106
107
108
109
110
111
112
# File 'lib/metanorma/collection/document/document.rb', line 103

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)


115
116
117
118
119
120
# File 'lib/metanorma/collection/document/document.rb', line 115

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