metanorma-document 0.5.2 → 0.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,122 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "metanorma/mko"
4
- require "metanorma-core"
5
- require "nokogiri"
6
-
7
- module Metanorma
8
- # The MODEL side of MKO (MN 116), reopening the format gem's module:
9
- # the projection walk (Project), collection orchestration
10
- # (Collection), the compile-pipeline exporter, flavor resolution and
11
- # registry seeding, and the export entry points. The wire contract —
12
- # schema, bundle layout, assets, diffs, alignment, JSON Schemas, the
13
- # MCP server — lives in the metanorma-mko gem.
14
- module Mko
15
- autoload :Project, "metanorma/document/mko/project"
16
- autoload :Collection, "metanorma/document/mko/collection"
17
- autoload :Exporter, "metanorma/document/mko/exporter"
18
-
19
- class << self
20
- # Export a document model (or semantic/presentation XML string)
21
- # as an MKO bundle directory (or .mko.zip with zip: true).
22
- # Returns an Export (path + projection Result); the Export
23
- # ducktypes as the bundle path String for existing callers.
24
- def export(source, to:, presentation_xml: nil, zip: false,
25
- assets_from: nil)
26
- seed_flavors!
27
- model = source.is_a?(String) ? model_from_xml(source) : source
28
- presentation = case presentation_xml
29
- when String then model_from_xml(presentation_xml)
30
- when nil then nil
31
- else presentation_xml
32
- end
33
- projection = Project.call(model, presentation: presentation,
34
- assets: Assets.new(source_dir: assets_from))
35
- Export.new(Writer.write(projection, to: to, zip: zip), projection)
36
- end
37
-
38
- # The format adapter over the central flavor registry (mirrors
39
- # Html::Generator): a flavor entry may register its own :mko
40
- # renderer; with none, the harness Exporter serves any model tree
41
- # — the walk is class/attribute-driven, zero flavor knowledge.
42
- def generate(document, **)
43
- seed_flavors!
44
- renderer_for(document, **)
45
- .new.generate_full_document(document, **)
46
- end
47
-
48
- def renderer_for(document, **)
49
- seed_flavors!
50
- Metanorma::Core::Flavors.renderer_for(document, format: :mko,
51
- **) || Exporter
52
- end
53
-
54
- private
55
-
56
- def model_from_xml(xml)
57
- root = Nokogiri::XML(xml).root
58
- flavor = root["flavor"]
59
- klass = nil
60
- Metanorma::Core::Flavors.table.reverse_each do |entry|
61
- next if entry.taste?
62
-
63
- candidate = entry.model_root_class or next
64
- name = entry.name.to_s
65
- next if flavor && !flavor.empty? && name != flavor
66
-
67
- klass = candidate
68
- break
69
- end
70
- unless klass
71
- raise ArgumentError,
72
- "no document model registered for flavor #{flavor.inspect}; " \
73
- "require the flavor gem (metanorma-standoc or a flavor) " \
74
- "before exporting"
75
- end
76
-
77
- klass.from_xml(xml)
78
- end
79
- end
80
-
81
- # Same seeding pattern as Html in html.rb: contribute the mko
82
- # renderer for the two harness model trees. Flavor models do not
83
- # subclass the harness roots (they include RootAttributes), so
84
- # renderer resolution for :mko goes through .renderer_for below with
85
- # a harness fallback — the Html::Generator pattern. Core::Flavors
86
- # has no add_renderer API yet — attach to the entry html.rb
87
- # registered when present, register a standalone entry otherwise
88
- # (mko loaded without html); delete the attach branch when core
89
- # gains the mutator.
90
- SEED_MODEL_ROOTS = {
91
- document: Metanorma::Document::Root,
92
- standoc: "Metanorma::Standoc::Document::Root",
93
- }.freeze
94
-
95
- # Lazy: seeding touches the core flavor registry, which only the
96
- # flavor-table line of metanorma-core defines. Requiring this gem
97
- # must never depend on it — seeds on first use, no-op without it.
98
- class << self
99
- def seed_flavors!
100
- return unless defined?(Metanorma::Core::Flavors)
101
- return if @flavors_seeded
102
-
103
- @flavors_seeded = true
104
- SEED_MODEL_ROOTS.each do |seed, model_root|
105
- entry = Metanorma::Core::Flavors.find(seed)
106
- if entry
107
- entry.renderers[:mko] ||= Exporter
108
- else
109
- Metanorma::Core::Flavors.register(Metanorma::Core::Flavor.new(
110
- name: seed,
111
- model_root: model_root,
112
- renderers: { mko: Exporter },
113
- ))
114
- end
115
- end
116
- end
117
- end
118
- end
119
-
120
- # document.rb keeps the lazy entry that pulls the model side in
121
- Document.autoload :MkoIntegration, "metanorma/document/mko" if false
122
- end