class Metanorma::CollectionManifest

Metanorma collection's manifest

Attributes

collection[R]

@return [Metanorma::Collection]

Public Class Methods

from_xml(mnf) click to toggle source

@param mnf [Nokogiri::XML::Element] @return [Metanorma::CollectionManifest]

# File lib/metanorma/collection_manifest.rb, line 36
def from_xml(mnf)
  level = mnf.at("level").text
  title = mnf.at("title")&.text
  manifest = mnf.xpath("xmlns:manifest").map { |m| from_xml(m) }
  new(level, title, parse_docref(mnf), manifest)
end
from_yaml(mnf) click to toggle source

@param mnf [Nokogiri::XML::Element] @return [Metanorma::CollectionManifest]

# File lib/metanorma/collection_manifest.rb, line 26
def from_yaml(mnf)
  manifest = RelatonBib::HashConverter.array(mnf["manifest"]).map do |m|
    from_yaml m
  end
  docref = RelatonBib::HashConverter.array mnf["docref"]
  new(mnf["level"], mnf["title"], docref, manifest)
end
new(level, title = nil, docref = [], manifest = []) click to toggle source

@param level [String] @param title [String, nil] @param docref [Array<Hash{String=>String}>] @param manifest [Array<Metanorma::CollectionManifest>]

# File lib/metanorma/collection_manifest.rb, line 15
def initialize(level, title = nil, docref = [], manifest = [])
  @level = level
  @title = title
  @docref = docref
  @manifest = manifest
  @disambig = Util::DisambigFiles.new
end

Private Class Methods

parse_docref(mnf) click to toggle source

@param mnf [Nokogiri::XML::Element] @return [Hash{String=>String}]

# File lib/metanorma/collection_manifest.rb, line 47
def parse_docref(mnf)
  mnf.xpath("xmlns:docref").map do |dr|
    h = { "identifier" => dr.at("identifier").children.to_xml }
    %i(fileref attachment sectionsplit index).each do |s|
      h[s.to_s] = dr[s] if dr[s]
    end
    h["presentation-xml"] = dr[:presentationxml] if dr[:presentationxml]
    h
  end
end

Public Instance Methods

collection=(col) click to toggle source

@param col [Metanorma::Collection]

# File lib/metanorma/collection_manifest.rb, line 60
def collection=(col)
  @collection = col
  @manifest.each { |mnf| mnf.collection = col }
end
docref_by_id(docid) click to toggle source
# File lib/metanorma/collection_manifest.rb, line 98
def docref_by_id(docid)
  refs = docrefs
  dref = refs.detect { |k| k["identifier"] == docid }
  dref || docrefs.detect { |k| /^#{k["identifier"]}/ =~ docid }
end
docrefs() click to toggle source

@return [Array<Hash{String=>String}>]

# File lib/metanorma/collection_manifest.rb, line 91
def docrefs
  return @docrefs if @docrefs

  drfs = @docref.map { |dr| dr }
  @manifest.reduce(drfs) { |mem, mnf| mem + mnf.docrefs }
end
documents(dir = "") click to toggle source

@param dir [String] path to collection @return [Hash<String, Metanorma::Document>]

# File lib/metanorma/collection_manifest.rb, line 67
def documents(dir = "")
  docs = @docref.each_with_object({}) do |dr, m|
    next m unless dr["fileref"]

    m[dr["identifier"]] = Document.parse_file(
      File.join(dir, dr["fileref"]),
      dr["attachment"], dr["identifier"], dr["index"]
    )
    m
  end
  @manifest.reduce(docs) { |mem, mnf| mem.merge mnf.documents(dir) }
end
to_xml(builder) click to toggle source

@param builder [Nokogiri::XML::Builder]

# File lib/metanorma/collection_manifest.rb, line 81
def to_xml(builder)
  builder.manifest do |b|
    b.level @level
    b.title @title if @title
    docref_to_xml b
    @manifest.each { |m| m.to_xml b }
  end
end

Private Instance Methods

docref_to_xml(builder) click to toggle source

@param builder [Nokogiri::XML::Builder]

# File lib/metanorma/collection_manifest.rb, line 107
def docref_to_xml(builder)
  @disambig = Util::DisambigFiles.new
  @docref.each do |dr|
    drf = builder.docref do |b|
      b.identifier do |i|
        i << dr["identifier"]
      end
    end
    docref_to_xml_attrs(drf, dr)
  end
end
docref_to_xml_attrs(elem, docref) click to toggle source
# File lib/metanorma/collection_manifest.rb, line 119
def docref_to_xml_attrs(elem, docref)
  elem[:fileref] = @disambig.source2dest_filename(docref["fileref"])
  %i(attachment sectionsplit).each do |i|
    elem[i] = docref[i.to_s] if docref[i.to_s]
  end
  elem[:index] = docref.has_key?("index") ? docref["index"] : "true"
  elem[:presentationxml] = "true" if docref["presentation-xml"] &&
    [true, "true"].include?(docref["presentation-xml"])
  docref_to_xml_attrs_id(elem, docref)
end
docref_to_xml_attrs_id(elem, docref) click to toggle source
# File lib/metanorma/collection_manifest.rb, line 130
def docref_to_xml_attrs_id(elem, docref)
  if collection.directives.include?("documents-inline")
    id = collection.documents.find_index do |k, _|
      k == docref["identifier"]
    end
    elem[:id] = format("doc%<index>09d", index: id)
  end
end