From 8e7a27eeb851861f795163f2aae1fbeb606c421f Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Sun, 9 Jun 2024 12:02:11 +0800 Subject: [PATCH 01/10] feat: complete migration from metanorma/iso-10303#65, rubocop fixes and others --- lib/suma/collection_config.rb | 4 -- lib/suma/collection_manifest.rb | 67 ++++++++++++++++++++++++-------- lib/suma/processor.rb | 24 ++++++++---- lib/suma/schema_attachment.rb | 12 +++--- lib/suma/schema_collection.rb | 58 +++++++++++++-------------- lib/suma/schema_config/config.rb | 10 +++-- lib/suma/schema_document.rb | 10 ----- 7 files changed, 107 insertions(+), 78 deletions(-) diff --git a/lib/suma/collection_config.rb b/lib/suma/collection_config.rb index 851c19b..dac089a 100644 --- a/lib/suma/collection_config.rb +++ b/lib/suma/collection_config.rb @@ -18,9 +18,5 @@ def self.from_file(path) def to_file(path) File.open(path, "w") { |f| f.write to_yaml } end - - def expand_schemas_only(path) - manifest.expand_schemas_only(path) - end end end diff --git a/lib/suma/collection_manifest.rb b/lib/suma/collection_manifest.rb index 2c6e252..07b0b12 100644 --- a/lib/suma/collection_manifest.rb +++ b/lib/suma/collection_manifest.rb @@ -32,23 +32,58 @@ def docref_from_yaml(model, value) model.entry = CollectionManifest.from_yaml(value.to_yaml) end - def expand_schemas_only(path) - if schemas_only - doc = YAML.safe_load(File.read(file)) - schemas = YAML.safe_load(File.read(File.join(File.dirname(file), "schemas.yaml"))) - self.file = nil - self.title = "ISO Collection" - self.type = "collection" - entries = schemas["schemas"].each_with_object([]) do |(k, v), m| - fname = Pathname(v["path"]).each_filename.to_a - fname[-1].sub!(/exp$/, "xml") # we compile these in col.compile below - m << CollectionManifest.new(identifier: k, title: k, file: File.join(path, fname[-2], "doc_#{fname[-1]}")) - end - doc = Array(CollectionManifest.new(title: doc["bibdata"]["docid"]["id"], type: "document", entry: entries)) - self.entry = doc - else - entry&.each { |e| e.expand_schemas_only(path) } + def is_express_doc + type == "express_doc" + end + + def all_express_docs + puts "self is_express_doc (#{is_express_doc}): #{self}" + entry&.map do |manifest| + manifest.all_express_docs + end.flatten.compact + (is_express_doc ? [self] : []) + end + + attr_accessor :schema_xml_files + + def expand_schemas_only(schema_output_path) + unless schemas_only + return entry&.each do |e| + e.expand_schemas_only(schema_output_path) + end end + + # This is the collection.yml file path + # doc = YAML.safe_load(File.read(file)) + doc = CollectionConfig.from_file(file) + doc_id = doc.bibdata.id + + # This is the schemas.yml file path + schemas_yaml_path = File.join(File.dirname(file), "schemas.yaml") + schema_config = SchemaConfig::Config.from_file(schemas_yaml_path) + + # The schemas can't load if the file is removed + # self.file = nil + self.title = "Collection" + self.type = "collection" + + entries = schema_config.schemas.map do |schema| + # TODO: We compile these later, but where is the actual compile command? + 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 + ) + end + + self.entry = [ + CollectionManifest.new( + title: doc_id, + type: "document", + entry: entries, + ), + ] end end end diff --git a/lib/suma/processor.rb b/lib/suma/processor.rb index 0989734..fa4d064 100644 --- a/lib/suma/processor.rb +++ b/lib/suma/processor.rb @@ -13,7 +13,11 @@ module Suma class Processor class << self # Can move to schema_config.rb - def write_all_schemas(schemas_all_path, document_paths) + def write_all_schemas(schemas_all_path, collection_config) + + # Gather all the inner (per-document) collection.yml files + document_paths = collection_config.manifest.entry.map(&:file) + all_schemas = Suma::SchemaConfig::Config.new(path: schemas_all_path) document_paths.each do |path| @@ -24,6 +28,11 @@ def write_all_schemas(schemas_all_path, document_paths) all_schemas.concat(schemas_config) end + schemas_only_list = collection_config.manifest.all_express_docs + schemas_only_list.each do |mani| + all_schemas.set_schemas_only(mani.identifier) + end + Utils.log "Writing #{schemas_all_path}..." all_schemas.to_file Utils.log "Done." @@ -39,16 +48,16 @@ def run(metanorma_yaml_path:, schemas_all_path:, compile:, output_directory: "_s collection_config_path = site_config.metanorma.source.files.first collection_config = Suma::CollectionConfig.from_file(collection_config_path) collection_config.path = collection_config_path + collection_config.manifest.expand_schemas_only("plain_schemas") - # Gather all the inner (per-document) collection.yml files - document_paths = collection_config.manifest.entry.map(&:file) + pp collection_config - write_all_schemas(schemas_all_path, document_paths) + write_all_schemas(schemas_all_path, collection_config) col = Suma::SchemaCollection.new( config_yaml: schemas_all_path, output_path_docs: "schema_docs", - output_path_schemas: "plain_schemas" + output_path_schemas: "plain_schemas", ) if compile @@ -97,6 +106,7 @@ def run(metanorma_yaml_path:, schemas_all_path:, compile:, output_directory: "_s if compile Utils.log "Compiling complete collection..." + # TODO: Why will defining a collection immediately compile?? metanorma_collection = Metanorma::Collection.parse(collection_config_path) # TODO: Somehow this is no longer used @@ -104,9 +114,9 @@ def run(metanorma_yaml_path:, schemas_all_path:, compile:, output_directory: "_s format: [:html], output_folder: output_directory, compile: { - no_install_fonts: true + no_install_fonts: true, }, - coverpage: "cover.html" + coverpage: "cover.html", } metanorma_collection.render(collection_opts) diff --git a/lib/suma/schema_attachment.rb b/lib/suma/schema_attachment.rb index 812469f..256e8a8 100644 --- a/lib/suma/schema_attachment.rb +++ b/lib/suma/schema_attachment.rb @@ -6,7 +6,7 @@ module Suma class SchemaAttachment - attr_accessor :schema, :output_path, :config + attr_accessor :schema, :output_path, :config, :id def initialize(schema:, output_path:) @schema = schema @@ -73,7 +73,7 @@ def to_config @config = SchemaConfig::Config.new @config.schemas << SchemaConfig::Schema.new( id: @schema.id, - path: @schema.path + path: @schema.path, ) @config @@ -100,13 +100,13 @@ def compile save_adoc relative_path = Pathname.new(filename_adoc).relative_path_from(Dir.pwd) - Utils.log "Compiling schema #{relative_path}..." + Utils.log "Compiling schema (id: #{id}, type: #{self.class}) => #{relative_path}" Metanorma::Compile.new.compile( filename_adoc, agree_to_terms: true, - no_install_fonts: true + no_install_fonts: true, ) - Utils.log "Compiling schema #{filename_adoc}...done!" + Utils.log "Compiling schema (id: #{id}, type: #{self.class}) => #{relative_path}... done!" # clean_artifacts @@ -124,7 +124,7 @@ def clean_artifacts filename_adoc, filename_adoc("presentation.xml"), filename_adoc("adoc.lutaml.log.txt"), - filename_adoc("err.html") + filename_adoc("err.html"), ].each do |filename| FileUtils.rm_rf(filename) end diff --git a/lib/suma/schema_collection.rb b/lib/suma/schema_collection.rb index 9401b5b..3796ed9 100644 --- a/lib/suma/schema_collection.rb +++ b/lib/suma/schema_collection.rb @@ -10,58 +10,54 @@ module Suma class SchemaCollection attr_accessor :config, :schemas, :docs, :output_path_docs, :output_path_schemas - def initialize(config: nil, config_yaml: nil, output_path_docs: nil, output_path_schemas: nil, document_paths: nil) + def initialize(config: nil, config_yaml: nil, output_path_docs: nil, output_path_schemas: nil) @schemas = [] @docs = [] @schema_name_to_docs = {} @output_path_docs = if output_path_docs - Pathname.new(output_path_docs).expand_path - else - Pathname.new(Dir.pwd) - end + Pathname.new(output_path_docs).expand_path + else + Pathname.new(Dir.pwd) + end @output_path_schemas = if output_path_schemas - Pathname.new(output_path_schemas).expand_path - else - Pathname.new(Dir.pwd) - end + Pathname.new(output_path_schemas).expand_path + else + Pathname.new(Dir.pwd) + end @config = if config - config - elsif config_yaml - SchemaConfig::Config.from_file(config_yaml) - end + config + elsif config_yaml + SchemaConfig::Config.from_file(config_yaml) + end + end - return self unless @config + def doc_from_schema_name(schema_name) + @schema_name_to_docs[schema_name] + end + def finalize @config.schemas.each do |config_schema| s = ExpressSchema.new( path: config_schema.path, - output_path: @output_path_schemas + output_path: @output_path_schemas, + ) + + klass = config_schema.schemas_only ? SchemaDocument : SchemaAttachment + doc = klass.new( + schema: s, + output_path: @output_path_docs.join(s.id), ) - doc = if config_schema.schemas_only - SchemaDocument.new( - schema: s, - output_path: @output_path_docs.join(s.id) - ) - else - SchemaAttachment.new( - schema: s, - output_path: @output_path_docs.join(s.id) - ) - end + @docs << doc @schemas << s @schema_name_to_docs[s.id] = doc end end - def doc_from_schema_name(schema_name) - @schema_name_to_docs[schema_name] - end - def compile + finalize schemas.map(&:save_exp) - docs.each(&:compile) # TODO: make this parallel diff --git a/lib/suma/schema_config/config.rb b/lib/suma/schema_config/config.rb index 401ec65..b48537d 100644 --- a/lib/suma/schema_config/config.rb +++ b/lib/suma/schema_config/config.rb @@ -8,7 +8,7 @@ module Suma module SchemaConfig class Config < Shale::Mapper attribute :schemas, Schema, collection: true - attr_accessor :base_path, :path, :schemas_only + attr_accessor :path def initialize(path: nil, **args) @path = path @@ -35,10 +35,12 @@ def to_file(to_path = path) end end - def set_schemas_only - schemas.each do |e| - e.schemas_only = true + def set_schemas_only(schema_name) + schema = schemas.detect do |e| + e.id == schema_name end + + schema.schemas_only = true end def schemas_from_yaml(model, value) diff --git a/lib/suma/schema_document.rb b/lib/suma/schema_document.rb index bea96a0..726e9e7 100644 --- a/lib/suma/schema_document.rb +++ b/lib/suma/schema_document.rb @@ -4,16 +4,6 @@ module Suma class SchemaDocument < SchemaAttachment - def empty_title1(anchor) - a = anchor.gsub(/\}\}/, ' | replace: "\", "-"}}') - <<~HEREDOC - [[#{@id}.#{a}]] - [%unnumbered,type=express] - === {blank} - - HEREDOC - end - def bookmark(anchor) a = anchor.gsub(/\}\}/, ' | replace: "\", "-"}}') "[[#{@id}.#{a}]]" From f9e66e9e20ab0760c3b24b3429e0e4a7446317d2 Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Sun, 9 Jun 2024 12:20:13 +0800 Subject: [PATCH 02/10] chore: rubocop --- lib/suma/collection_manifest.rb | 11 ++++------- lib/suma/processor.rb | 9 +++------ lib/suma/schema_attachment.rb | 6 +++--- lib/suma/schema_collection.rb | 28 ++++++++++++++-------------- 4 files changed, 24 insertions(+), 30 deletions(-) diff --git a/lib/suma/collection_manifest.rb b/lib/suma/collection_manifest.rb index 07b0b12..c280f4f 100644 --- a/lib/suma/collection_manifest.rb +++ b/lib/suma/collection_manifest.rb @@ -37,10 +37,7 @@ def is_express_doc end def all_express_docs - puts "self is_express_doc (#{is_express_doc}): #{self}" - entry&.map do |manifest| - manifest.all_express_docs - end.flatten.compact + (is_express_doc ? [self] : []) + entry&.map(&:all_express_docs)&.flatten&.compact&.+ (is_express_doc ? [self] : []) end attr_accessor :schema_xml_files @@ -73,7 +70,7 @@ def expand_schemas_only(schema_output_path) 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 + type: "express_doc" # This means this schema is a SchemaDocument ) end @@ -81,8 +78,8 @@ def expand_schemas_only(schema_output_path) CollectionManifest.new( title: doc_id, type: "document", - entry: entries, - ), + entry: entries + ) ] end end diff --git a/lib/suma/processor.rb b/lib/suma/processor.rb index fa4d064..fc2d0d2 100644 --- a/lib/suma/processor.rb +++ b/lib/suma/processor.rb @@ -14,7 +14,6 @@ class Processor class << self # Can move to schema_config.rb def write_all_schemas(schemas_all_path, collection_config) - # Gather all the inner (per-document) collection.yml files document_paths = collection_config.manifest.entry.map(&:file) @@ -50,14 +49,12 @@ def run(metanorma_yaml_path:, schemas_all_path:, compile:, output_directory: "_s collection_config.path = collection_config_path collection_config.manifest.expand_schemas_only("plain_schemas") - pp collection_config - write_all_schemas(schemas_all_path, collection_config) col = Suma::SchemaCollection.new( config_yaml: schemas_all_path, output_path_docs: "schema_docs", - output_path_schemas: "plain_schemas", + output_path_schemas: "plain_schemas" ) if compile @@ -114,9 +111,9 @@ def run(metanorma_yaml_path:, schemas_all_path:, compile:, output_directory: "_s format: [:html], output_folder: output_directory, compile: { - no_install_fonts: true, + no_install_fonts: true }, - coverpage: "cover.html", + coverpage: "cover.html" } metanorma_collection.render(collection_opts) diff --git a/lib/suma/schema_attachment.rb b/lib/suma/schema_attachment.rb index 256e8a8..712ce39 100644 --- a/lib/suma/schema_attachment.rb +++ b/lib/suma/schema_attachment.rb @@ -73,7 +73,7 @@ def to_config @config = SchemaConfig::Config.new @config.schemas << SchemaConfig::Schema.new( id: @schema.id, - path: @schema.path, + path: @schema.path ) @config @@ -104,7 +104,7 @@ def compile Metanorma::Compile.new.compile( filename_adoc, agree_to_terms: true, - no_install_fonts: true, + no_install_fonts: true ) Utils.log "Compiling schema (id: #{id}, type: #{self.class}) => #{relative_path}... done!" @@ -124,7 +124,7 @@ def clean_artifacts filename_adoc, filename_adoc("presentation.xml"), filename_adoc("adoc.lutaml.log.txt"), - filename_adoc("err.html"), + filename_adoc("err.html") ].each do |filename| FileUtils.rm_rf(filename) end diff --git a/lib/suma/schema_collection.rb b/lib/suma/schema_collection.rb index 3796ed9..a3754b0 100644 --- a/lib/suma/schema_collection.rb +++ b/lib/suma/schema_collection.rb @@ -15,21 +15,21 @@ def initialize(config: nil, config_yaml: nil, output_path_docs: nil, output_path @docs = [] @schema_name_to_docs = {} @output_path_docs = if output_path_docs - Pathname.new(output_path_docs).expand_path - else - Pathname.new(Dir.pwd) - end + Pathname.new(output_path_docs).expand_path + else + Pathname.new(Dir.pwd) + end @output_path_schemas = if output_path_schemas - Pathname.new(output_path_schemas).expand_path - else - Pathname.new(Dir.pwd) - end + Pathname.new(output_path_schemas).expand_path + else + Pathname.new(Dir.pwd) + end @config = if config - config - elsif config_yaml - SchemaConfig::Config.from_file(config_yaml) - end + config + elsif config_yaml + SchemaConfig::Config.from_file(config_yaml) + end end def doc_from_schema_name(schema_name) @@ -40,13 +40,13 @@ def finalize @config.schemas.each do |config_schema| s = ExpressSchema.new( path: config_schema.path, - output_path: @output_path_schemas, + output_path: @output_path_schemas ) klass = config_schema.schemas_only ? SchemaDocument : SchemaAttachment doc = klass.new( schema: s, - output_path: @output_path_docs.join(s.id), + output_path: @output_path_docs.join(s.id) ) @docs << doc From 8d05d34335c49285f7f4c4e8a796c7e4c05fbcc3 Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Sun, 9 Jun 2024 16:49:54 +1000 Subject: [PATCH 03/10] adjust suma, retain and separate schemas-only source file from its broken-down schemas documents: https://github.com/metanorma/iso-10303/issues/41 --- lib/suma/collection_manifest.rb | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/suma/collection_manifest.rb b/lib/suma/collection_manifest.rb index c280f4f..ed3baad 100644 --- a/lib/suma/collection_manifest.rb +++ b/lib/suma/collection_manifest.rb @@ -44,9 +44,12 @@ def all_express_docs def expand_schemas_only(schema_output_path) unless schemas_only - return entry&.each do |e| - e.expand_schemas_only(schema_output_path) + entry or return [self] + ret = entry.each_with_object([]) do |e| + add = e.expand_schemas_only(schema_output_path) + add.each { |x| ret << x } end + self.entry = ret end # This is the collection.yml file path @@ -60,11 +63,14 @@ def expand_schemas_only(schema_output_path) # The schemas can't load if the file is removed # self.file = nil - self.title = "Collection" - self.type = "collection" + # 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" 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, @@ -74,13 +80,23 @@ def expand_schemas_only(schema_output_path) ) end - self.entry = [ + # we need to separate this file from the following new entries + + added = CollectionManifest.new( + added.title = "Collection", + added.type = "collection", + added.identifier = self.identifier + "_" + ) + + added.entry = [ CollectionManifest.new( title: doc_id, type: "document", entry: entries ) ] + + [self, added] end end end From a230528b3bdb5ef2fe300957841a9fa3c3936110 Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Sun, 9 Jun 2024 16:51:00 +1000 Subject: [PATCH 04/10] adjust suma, retain and separate schemas-only source file from its broken-down schemas documents: https://github.com/metanorma/iso-10303/issues/41 --- lib/suma/collection_manifest.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/suma/collection_manifest.rb b/lib/suma/collection_manifest.rb index ed3baad..3621293 100644 --- a/lib/suma/collection_manifest.rb +++ b/lib/suma/collection_manifest.rb @@ -83,9 +83,9 @@ def expand_schemas_only(schema_output_path) # we need to separate this file from the following new entries added = CollectionManifest.new( - added.title = "Collection", - added.type = "collection", - added.identifier = self.identifier + "_" + title: "Collection", + type: "collection", + identifier: self.identifier + "_" ) added.entry = [ From e906643556579a6e2d46e88f991f8acef3ac8e9e Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Sun, 9 Jun 2024 16:52:10 +1000 Subject: [PATCH 05/10] adjust suma, retain and separate schemas-only source file from its broken-down schemas documents: https://github.com/metanorma/iso-10303/issues/41 --- lib/suma/collection_manifest.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/suma/collection_manifest.rb b/lib/suma/collection_manifest.rb index 3621293..e37a965 100644 --- a/lib/suma/collection_manifest.rb +++ b/lib/suma/collection_manifest.rb @@ -50,6 +50,7 @@ def expand_schemas_only(schema_output_path) add.each { |x| ret << x } end self.entry = ret + return [self] end # This is the collection.yml file path From 6d689b4af01a047ba581214c411676e953c798c8 Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Sun, 9 Jun 2024 16:54:17 +1000 Subject: [PATCH 06/10] adjust suma, retain and separate schemas-only source file from its broken-down schemas documents: https://github.com/metanorma/iso-10303/issues/41 --- lib/suma/collection_manifest.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/suma/collection_manifest.rb b/lib/suma/collection_manifest.rb index e37a965..a134b12 100644 --- a/lib/suma/collection_manifest.rb +++ b/lib/suma/collection_manifest.rb @@ -45,9 +45,9 @@ def all_express_docs def expand_schemas_only(schema_output_path) unless schemas_only entry or return [self] - ret = entry.each_with_object([]) do |e| + ret = entry.each_with_object([]) do |e, m| add = e.expand_schemas_only(schema_output_path) - add.each { |x| ret << x } + add.each { |x| m << x } end self.entry = ret return [self] From d4223f0dcee6dd87d80b167dfe6166eebf356f26 Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Sun, 9 Jun 2024 16:57:00 +1000 Subject: [PATCH 07/10] adjust suma, retain and separate schemas-only source file from its broken-down schemas documents: https://github.com/metanorma/iso-10303/issues/41 --- lib/suma/processor.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/suma/processor.rb b/lib/suma/processor.rb index fc2d0d2..1e10941 100644 --- a/lib/suma/processor.rb +++ b/lib/suma/processor.rb @@ -15,7 +15,7 @@ class << self # Can move to schema_config.rb def write_all_schemas(schemas_all_path, collection_config) # Gather all the inner (per-document) collection.yml files - document_paths = collection_config.manifest.entry.map(&:file) + document_paths = collection_config.manifest.entry.map(&:file).compact all_schemas = Suma::SchemaConfig::Config.new(path: schemas_all_path) From e145a0df73dea11e5e5f41dc3c6f4e6525eee785 Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Sun, 9 Jun 2024 17:14:00 +0800 Subject: [PATCH 08/10] feat: delay loading of Expressir content to prevent early crashes --- lib/suma/express_schema.rb | 18 ++++++++++++------ lib/suma/schema_collection.rb | 5 +++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/suma/express_schema.rb b/lib/suma/express_schema.rb index e6f7f06..9c1ef89 100644 --- a/lib/suma/express_schema.rb +++ b/lib/suma/express_schema.rb @@ -8,12 +8,9 @@ module Suma class ExpressSchema attr_accessor :path, :id, :parsed, :output_path - def initialize(path:, output_path:) + def initialize(id:, path:, output_path:) @path = Pathname.new(path).expand_path - @parsed = Expressir::Express::Parser.from_file(@path.to_s) - Utils.log "Loaded EXPRESS schema: #{path}" - - @id = @parsed.schemas.first.id + @id = id @output_path = output_path end @@ -28,8 +25,17 @@ def type end end + def parsed + return @parsed if @parsed + + @parsed = Expressir::Express::Parser.from_file(@path.to_s) + Utils.log "Loaded EXPRESS schema: #{path}" + @id = @parsed.schemas.first.id + @parsed + end + def to_plain - @parsed.to_s(no_remarks: true) + parsed.to_s(no_remarks: true) end def filename_plain diff --git a/lib/suma/schema_collection.rb b/lib/suma/schema_collection.rb index a3754b0..4e01c11 100644 --- a/lib/suma/schema_collection.rb +++ b/lib/suma/schema_collection.rb @@ -39,8 +39,9 @@ def doc_from_schema_name(schema_name) def finalize @config.schemas.each do |config_schema| s = ExpressSchema.new( - path: config_schema.path, - output_path: @output_path_schemas + id: config_schema.id, + path: config_schema.path.to_s, + output_path: @output_path_schemas.to_s ) klass = config_schema.schemas_only ? SchemaDocument : SchemaAttachment From 4259b9915fe93296fa5c50724820c59e4352ac3a Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Sun, 9 Jun 2024 17:14:24 +0800 Subject: [PATCH 09/10] fix: make SchemaDocument work --- lib/suma/collection_manifest.rb | 62 ++++++++++++++++++++++---------- lib/suma/processor.rb | 41 +++++++-------------- lib/suma/schema_collection.rb | 43 ++++++++++++++++------ lib/suma/schema_config/config.rb | 53 +++++++++++++++++++++------ lib/suma/schema_config/schema.rb | 2 +- 5 files changed, 130 insertions(+), 71 deletions(-) diff --git a/lib/suma/collection_manifest.rb b/lib/suma/collection_manifest.rb index a134b12..0b40055 100644 --- a/lib/suma/collection_manifest.rb +++ b/lib/suma/collection_manifest.rb @@ -8,6 +8,8 @@ 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 + attr_accessor :schema_config yaml do map "identifier", to: :identifier @@ -32,18 +34,25 @@ def docref_from_yaml(model, value) model.entry = CollectionManifest.from_yaml(value.to_yaml) end - def is_express_doc - type == "express_doc" + 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 + end + export_config end - def all_express_docs - entry&.map(&:all_express_docs)&.flatten&.compact&.+ (is_express_doc ? [self] : []) + 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 end - attr_accessor :schema_xml_files - def expand_schemas_only(schema_output_path) - unless schemas_only + unless file entry or return [self] ret = entry.each_with_object([]) do |e, m| add = e.expand_schemas_only(schema_output_path) @@ -53,14 +62,22 @@ def expand_schemas_only(schema_output_path) return [self] end - # This is the collection.yml file path - # doc = YAML.safe_load(File.read(file)) - doc = CollectionConfig.from_file(file) - doc_id = doc.bibdata.id + 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) + @schema_config = Suma::SchemaConfig::Config.from_file(schemas_yaml_path) + end + end - # This is the schemas.yml file path - schemas_yaml_path = File.join(File.dirname(file), "schemas.yaml") - schema_config = SchemaConfig::Config.from_file(schemas_yaml_path) + 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 # The schemas can't load if the file is removed # self.file = nil @@ -69,20 +86,27 @@ def expand_schemas_only(schema_output_path) #self.title = "Collection" #self.type = "collection" - entries = schema_config.schemas.map do |schema| + # 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 + type: "express_doc", # This means this schema is a SchemaDocument + schema_source: schema.path ) end # we need to separate this file from the following new entries - added = CollectionManifest.new( title: "Collection", type: "collection", @@ -93,8 +117,8 @@ def expand_schemas_only(schema_output_path) CollectionManifest.new( title: doc_id, type: "document", - entry: entries - ) + entry: entries, + ), ] [self, added] diff --git a/lib/suma/processor.rb b/lib/suma/processor.rb index 1e10941..be0ba05 100644 --- a/lib/suma/processor.rb +++ b/lib/suma/processor.rb @@ -12,31 +12,6 @@ module Suma class Processor class << self - # Can move to schema_config.rb - def write_all_schemas(schemas_all_path, collection_config) - # Gather all the inner (per-document) collection.yml files - document_paths = collection_config.manifest.entry.map(&:file).compact - - all_schemas = Suma::SchemaConfig::Config.new(path: schemas_all_path) - - document_paths.each do |path| - schemas_yaml = File.join(File.dirname(path), "schemas.yaml") - next unless File.exist?(schemas_yaml) - - schemas_config = Suma::SchemaConfig::Config.from_file(schemas_yaml) - all_schemas.concat(schemas_config) - end - - schemas_only_list = collection_config.manifest.all_express_docs - schemas_only_list.each do |mani| - all_schemas.set_schemas_only(mani.identifier) - end - - Utils.log "Writing #{schemas_all_path}..." - all_schemas.to_file - Utils.log "Done." - end - def run(metanorma_yaml_path:, schemas_all_path:, compile:, output_directory: "_site") Utils.log "Current directory: #{Dir.getwd}" @@ -49,12 +24,20 @@ def run(metanorma_yaml_path:, schemas_all_path:, compile:, output_directory: "_s collection_config.path = collection_config_path collection_config.manifest.expand_schemas_only("plain_schemas") - write_all_schemas(schemas_all_path, collection_config) + 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." col = Suma::SchemaCollection.new( config_yaml: schemas_all_path, + manifest: collection_config.manifest, output_path_docs: "schema_docs", - output_path_schemas: "plain_schemas" + output_path_schemas: "plain_schemas", ) if compile @@ -111,9 +94,9 @@ def run(metanorma_yaml_path:, schemas_all_path:, compile:, output_directory: "_s format: [:html], output_folder: output_directory, compile: { - no_install_fonts: true + no_install_fonts: true, }, - coverpage: "cover.html" + coverpage: "cover.html", } metanorma_collection.render(collection_opts) diff --git a/lib/suma/schema_collection.rb b/lib/suma/schema_collection.rb index 4e01c11..200c401 100644 --- a/lib/suma/schema_collection.rb +++ b/lib/suma/schema_collection.rb @@ -8,11 +8,12 @@ module Suma class SchemaCollection - attr_accessor :config, :schemas, :docs, :output_path_docs, :output_path_schemas + attr_accessor :config, :schemas, :docs, :output_path_docs, :output_path_schemas, + :manifest - def initialize(config: nil, config_yaml: nil, output_path_docs: nil, output_path_schemas: nil) - @schemas = [] - @docs = [] + def initialize(config: nil, config_yaml: nil, output_path_docs: nil, output_path_schemas: nil, manifest: nil) + @schemas = {} + @docs = {} @schema_name_to_docs = {} @output_path_docs = if output_path_docs Pathname.new(output_path_docs).expand_path @@ -30,36 +31,56 @@ def initialize(config: nil, config_yaml: nil, output_path_docs: nil, output_path elsif config_yaml SchemaConfig::Config.from_file(config_yaml) end + + @manifest = manifest end def doc_from_schema_name(schema_name) @schema_name_to_docs[schema_name] end - def finalize - @config.schemas.each do |config_schema| + def process_schemas(schemas, klass) + schemas.each do |config_schema| s = ExpressSchema.new( id: config_schema.id, path: config_schema.path.to_s, output_path: @output_path_schemas.to_s ) - klass = config_schema.schemas_only ? SchemaDocument : SchemaAttachment doc = klass.new( schema: s, output_path: @output_path_docs.join(s.id) ) - @docs << doc - @schemas << s + @docs[s.id] = doc + @schemas[s.id] = s @schema_name_to_docs[s.id] = doc end end + def finalize + # Process each schema in @config.schemas + process_schemas(@config.schemas, SchemaAttachment) + + manifest_entry = @manifest.lookup(:schemas_only, true) + + manifest_entry.each do |entry| + next unless entry.schema_config + + # Process each schema in entry.schema_config.schemas + process_schemas(entry.schema_config.schemas, SchemaDocument) + end + end + def compile finalize - schemas.map(&:save_exp) - docs.each(&:compile) + schemas.each_pair do |schema_id, entry| + entry.save_exp + end + + docs.each_pair do |schema_id, entry| + entry.compile + end # TODO: make this parallel # Utils.log"Starting Ractor processing" diff --git a/lib/suma/schema_config/config.rb b/lib/suma/schema_config/config.rb index b48537d..76e97e0 100644 --- a/lib/suma/schema_config/config.rb +++ b/lib/suma/schema_config/config.rb @@ -11,7 +11,7 @@ class Config < Shale::Mapper attr_accessor :path def initialize(path: nil, **args) - @path = path + @path = path_relative_to_absolute(path) if path super(**args) end @@ -25,7 +25,7 @@ def base_path def self.from_file(path) from_yaml(File.read(path)).tap do |x| - x.path = path + x.set_initial_path(path) end end @@ -35,27 +35,58 @@ def to_file(to_path = path) end end - def set_schemas_only(schema_name) - schema = schemas.detect do |e| - e.id == schema_name + def set_initial_path(new_path) + @path = path_relative_to_absolute(new_path) + schemas.each do |schema| + schema.path = path_relative_to_absolute(schema.path) end - - schema.schemas_only = true end def schemas_from_yaml(model, value) + puts "*"*30 model.schemas = value.map do |k, v| - Schema.new(id: k, path: v["path"], schemas_only: v["schemas-only"]) + Schema.new(id: k, path: path_relative_to_absolute(v["path"])) end end def schemas_to_yaml(model, doc) + puts "^"*30 + pp self doc["schemas"] = model.schemas.sort_by(&:id).to_h do |schema| - [schema.id, { "path" => schema.path, "schemas-only" => schema.schemas_only }] + [schema.id, { "path" => path_absolute_to_relative(schema.path) }] end end + def path_relative_to_absolute(relative_path) + eval_path = Pathname.new(relative_path) + return relative_path if eval_path.absolute? + + # Or based on current working directory? + return relative_path unless @path + + Pathname.new(File.dirname(@path)).join(eval_path).expand_path.to_s + end + + def path_absolute_to_relative(absolute_path) + puts "path_absolute_to_relative 1 #{absolute_path}" + 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 + end + def update_path(new_path) + if @path.nil? + @path = new_path + return @path + end + old_base_path = File.dirname(@path) new_base_path = File.dirname(new_path) @@ -76,8 +107,8 @@ def concat(another_config) raise StandardError, "Can only concatenate a non SchemaConfig::Config object." end - # We need to update the relative paths - if path != another_config.path + # We need to update the relative paths when paths exist + if path && another_config.path && path != another_config.path new_config = another_config.dup new_config.update_path(path) end diff --git a/lib/suma/schema_config/schema.rb b/lib/suma/schema_config/schema.rb index 327e697..7f5c290 100644 --- a/lib/suma/schema_config/schema.rb +++ b/lib/suma/schema_config/schema.rb @@ -7,7 +7,7 @@ module SchemaConfig class Schema < Shale::Mapper attribute :id, Shale::Type::String attribute :path, Shale::Type::String - attribute :schemas_only, Shale::Type::Boolean + # attribute :schemas_only, Shale::Type::Boolean end end end From 03fe5be4fd16776d8371474df63d675d724a5576 Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Sun, 9 Jun 2024 17:52:54 +0800 Subject: [PATCH 10/10] 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)