class Metanorma::Document

Attributes

attachment[R]

@return [Strin]

bibitem[R]

@return [Strin]

file[R]

@return [Strin]

index[R]

@return [Strin]

Public Class Methods

attachment_bibitem(identifier) click to toggle source
# File lib/metanorma/document.rb, line 42
def attachment_bibitem(identifier)
  Nokogiri::XML(
    "<bibdata><docidentifier>#{identifier}</docidentifier></bibdata>",
  )
end
new(bibitem, file, options = {}) click to toggle source

@param bibitem [RelatonBib::BibliographicItem]

# 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
parse_file(file, attachment, identifier = nil, index = true) click to toggle source

@param file [String] file path @param attachment [Bool] is an attachment @param identifier [String] is the identifier assigned the file in the collection file @param index [Bool] is indication on whether to index this file in coverpage @return [Metanorma::Document]

# 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) click to toggle source

param xml [Nokogiri::XML::Document, Nokogiri::XML::Element] @return [Metanorma::Document]

# File lib/metanorma/document.rb, line 30
def parse_xml(xml)
  new from_xml(xml)
end
raw_file(filename) click to toggle source

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

# 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

Private Class Methods

bibitem(file, attachment, identifier) click to toggle source

@param file [String] @return [RelatonBib::BibliographicItem,

RelatonIso::IsoBibliographicItem]
# File lib/metanorma/document.rb, line 68
def bibitem(file, attachment, identifier)
  if attachment then attachment_bibitem(identifier)
  else
    case format(file)
    when :xml
      from_xml Nokogiri::XML(File.read(file, encoding: "UTF-8"))
    when :yaml
      yaml = File.read(file, encoding: "UTF-8")
      Relaton::Cli::YAMLConvertor.convert_single_file(yaml)
    end
  end
end
format(file) click to toggle source

@param file [String] @return [Symbol] file type

# File lib/metanorma/document.rb, line 58
def format(file)
  case file
  when /\.xml$/ then :xml
  when /.ya?ml$/ then :yaml
  end
end
from_xml(xml) click to toggle source

param xml [Nokogiri::XML::Document, Nokogiri::XML::Element] @return [RelatonBib::BibliographicItem,RelatonIso::IsoBibliographicItem]

# File lib/metanorma/document.rb, line 52
def from_xml(xml)
  Relaton::Cli.parse_xml xml.at("//xmlns:bibitem|//xmlns:bibdata")
end

Public Instance Methods

to_xml(builder = nil) click to toggle source

@param builder [Nokogiri::XML::Builder, nil] @return [Nokogiri::XML::Builder, String]

# 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
type() click to toggle source

@return [String]

# 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

Private Instance Methods

render_xml(builder) click to toggle source
# File lib/metanorma/document.rb, line 104
def render_xml(builder)
  if @raw
    builder << @bibitem.root.to_xml
  else
    builder.send("#{type}-standard") do |b|
      b << @bibitem.to_xml(bibdata: true)
    end
  end
end