class Metanorma::Document
Attributes
@return [Strin]
@return [Strin]
@return [Strin]
@return [Strin]
Public Class Methods
# File lib/metanorma/document.rb, line 42 def attachment_bibitem(identifier) Nokogiri::XML( "<bibdata><docidentifier>#{identifier}</docidentifier></bibdata>", ) end
@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
@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
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 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
@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
@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
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
@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
@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
# 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