Skip to content

Commit

Permalink
fix: make SchemaDocument work
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldtse committed Jun 9, 2024
1 parent 4259b99 commit 03fe5be
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 55 deletions.
68 changes: 29 additions & 39 deletions lib/suma/collection_manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -36,73 +36,63 @@ 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)
@schema_config = Suma::SchemaConfig::Config.from_file(schemas_yaml_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

Expand Down
3 changes: 1 addition & 2 deletions lib/suma/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand All @@ -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."
Expand Down
31 changes: 17 additions & 14 deletions lib/suma/schema_config/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 03fe5be

Please sign in to comment.