From 03fe5be4fd16776d8371474df63d675d724a5576 Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Sun, 9 Jun 2024 17:52:54 +0800 Subject: [PATCH] fix: make SchemaDocument work --- lib/suma/collection_manifest.rb | 68 ++++++++++++++------------------ lib/suma/processor.rb | 3 +- lib/suma/schema_config/config.rb | 31 ++++++++------- 3 files changed, 47 insertions(+), 55 deletions(-) diff --git a/lib/suma/collection_manifest.rb b/lib/suma/collection_manifest.rb index 0b40055..8b3cbd3 100644 --- a/lib/suma/collection_manifest.rb +++ b/lib/suma/collection_manifest.rb @@ -8,7 +8,7 @@ module Suma class CollectionManifest < Metanorma::Collection::Config::Manifest attribute :schemas_only, Shale::Type::Boolean attribute :entry, CollectionManifest, collection: true - attribute :schema_source, Shale::Type::String + # attribute :schema_source, Shale::Type::String attr_accessor :schema_config yaml do @@ -36,32 +36,39 @@ def docref_from_yaml(model, value) def export_schema_config(path) export_config = @schema_config || Suma::SchemaConfig::Config.new - entry&.map do |x| - x.export_schema_config(path) - end.compact.each_with_object(export_config) do |x, acc| - acc.concat(x) - acc + return export_config unless entry + + entry.each do |x| + child_config = x.export_schema_config(path) + export_config.concat(child_config) if child_config end + export_config end def lookup(attr_sym, match) - (entry.select do |e| - e.send(attr_sym) == match - end + [self.send(attr_sym) == match ? self.send(attr_sym) : nil]).compact + results = entry.select { |e| e.send(attr_sym) == match } + results << self if send(attr_sym) == match + results end - def expand_schemas_only(schema_output_path) - unless file - entry or return [self] - ret = entry.each_with_object([]) do |e, m| - add = e.expand_schemas_only(schema_output_path) - add.each { |x| m << x } - end - self.entry = ret - return [self] + def process_entry(schema_output_path) + return [self] unless entry + + ret = entry.each_with_object([]) do |e, m| + add = e.expand_schemas_only(schema_output_path) + m.concat(add) end + self.entry = ret + [self] + end + + def expand_schemas_only(schema_output_path) + return process_entry(schema_output_path) unless file + + # If there is collection.yml, this is a document collection, we process + # schemas.yaml. if File.basename(file) == 'collection.yml' schemas_yaml_path = File.join(File.dirname(file), "schemas.yaml") if schemas_yaml_path && File.exist?(schemas_yaml_path) @@ -69,40 +76,23 @@ def expand_schemas_only(schema_output_path) end end - unless schemas_only - entry or return [self] - ret = entry.each_with_object([]) do |e, m| - add = e.expand_schemas_only(schema_output_path) - add.each { |x| m << x } - end - self.entry = ret - return [self] - end + return process_entry(schema_output_path) unless schemas_only - # The schemas can't load if the file is removed - # self.file = nil - # If we are going to keep the schemas-only file and compile it, we can't have it showing up in output + # If we are going to keep the schemas-only file and compile it, we can't + # have it showing up in output. self.index = false - #self.title = "Collection" - #self.type = "collection" - # This is the collection.yml file path doc = CollectionConfig.from_file(file) doc_id = doc.bibdata.id - # pp @schema_config - entries = @schema_config.schemas.map do |schema| - # TODO: We compile these later, but where is the actual compile command? - # Answer: in manifest_compile_adoc, on postprocess, end of initialisation of manifest object fname = [File.basename(schema.path, ".exp"), ".xml"].join CollectionManifest.new( identifier: schema.id, title: schema.id, file: File.join(schema_output_path, "doc_#{schema.id}", fname), - type: "express_doc", # This means this schema is a SchemaDocument - schema_source: schema.path + # schema_source: schema.path ) end diff --git a/lib/suma/processor.rb b/lib/suma/processor.rb index be0ba05..fffabf0 100644 --- a/lib/suma/processor.rb +++ b/lib/suma/processor.rb @@ -12,6 +12,7 @@ module Suma class Processor class << self + def run(metanorma_yaml_path:, schemas_all_path:, compile:, output_directory: "_site") Utils.log "Current directory: #{Dir.getwd}" @@ -27,8 +28,6 @@ def run(metanorma_yaml_path:, schemas_all_path:, compile:, output_directory: "_s exported_schema_config = collection_config.manifest.export_schema_config(schemas_all_path) exported_schema_config.path = schemas_all_path - pp exported_schema_config - Utils.log "Writing #{schemas_all_path}..." exported_schema_config.to_file Utils.log "Done." diff --git a/lib/suma/schema_config/config.rb b/lib/suma/schema_config/config.rb index 76e97e0..5bdcaac 100644 --- a/lib/suma/schema_config/config.rb +++ b/lib/suma/schema_config/config.rb @@ -8,9 +8,9 @@ module Suma module SchemaConfig class Config < Shale::Mapper attribute :schemas, Schema, collection: true - attr_accessor :path + attribute :path, Shale::Type::String - def initialize(path: nil, **args) + def initialize(**args) @path = path_relative_to_absolute(path) if path super(**args) end @@ -43,15 +43,20 @@ def set_initial_path(new_path) end def schemas_from_yaml(model, value) - puts "*"*30 model.schemas = value.map do |k, v| Schema.new(id: k, path: path_relative_to_absolute(v["path"])) end end + # TODO: I can't get the relative path working. The schemas-*.yaml file is + # meant to contain the "path" key, which is a relative path to its + # location, then sets the base path to each schema path, which is supposed + # to be relative to "path" key. Somehow, the @path variable is always + # missing in to_yaml... def schemas_to_yaml(model, doc) - puts "^"*30 - pp self + # puts "^"*30 + # pp self + # pp @path doc["schemas"] = model.schemas.sort_by(&:id).to_h do |schema| [schema.id, { "path" => path_absolute_to_relative(schema.path) }] end @@ -68,17 +73,15 @@ def path_relative_to_absolute(relative_path) end def path_absolute_to_relative(absolute_path) - puts "path_absolute_to_relative 1 #{absolute_path}" + # puts "path_absolute_to_relative 1 #{absolute_path}" + # pp self + # pp path + # pp @hello return absolute_path unless @path - eval_path = Pathname.new(absolute_path) - - puts "path_absolute_to_relative 2 #{eval_path}" - # Or based on current working directory? - - x = Pathname.new(absolute_path).relative_path_from(File.dirname(@path)).to_s - puts "path_absolute_to_relative x #{x}" - x + relative_path = Pathname.new(absolute_path).relative_path_from(Pathname.new(@path).dirname).to_s + # puts "path_absolute_to_relative x #{relative_path}" + relative_path end def update_path(new_path)