ceradoc 3.0.1

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.
Files changed (152) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/Rakefile +8 -0
  4. data/exe/ceradoc +24 -0
  5. data/lib/ceradoc/binding_inflater.rb +118 -0
  6. data/lib/ceradoc/ceradoc.rb +164 -0
  7. data/lib/ceradoc/cli.rb +311 -0
  8. data/lib/ceradoc/configurable.rb +530 -0
  9. data/lib/ceradoc/core_model/abstract_block.rb +11 -0
  10. data/lib/ceradoc/core_model/annotation_block.rb +57 -0
  11. data/lib/ceradoc/core_model/attribute_reference_resolver.rb +155 -0
  12. data/lib/ceradoc/core_model/base.rb +281 -0
  13. data/lib/ceradoc/core_model/bibliography.rb +41 -0
  14. data/lib/ceradoc/core_model/bibliography_entry.rb +48 -0
  15. data/lib/ceradoc/core_model/block.rb +69 -0
  16. data/lib/ceradoc/core_model/callout.rb +32 -0
  17. data/lib/ceradoc/core_model/callout_text.rb +35 -0
  18. data/lib/ceradoc/core_model/children_content.rb +72 -0
  19. data/lib/ceradoc/core_model/comment_block.rb +16 -0
  20. data/lib/ceradoc/core_model/comment_line.rb +23 -0
  21. data/lib/ceradoc/core_model/definition_item.rb +68 -0
  22. data/lib/ceradoc/core_model/definition_list.rb +28 -0
  23. data/lib/ceradoc/core_model/element_attribute.rb +14 -0
  24. data/lib/ceradoc/core_model/example_block.rb +12 -0
  25. data/lib/ceradoc/core_model/footnote.rb +92 -0
  26. data/lib/ceradoc/core_model/frontmatter/codec.rb +151 -0
  27. data/lib/ceradoc/core_model/frontmatter/field_transform.rb +75 -0
  28. data/lib/ceradoc/core_model/frontmatter/frontmatter_entry.rb +22 -0
  29. data/lib/ceradoc/core_model/frontmatter/frontmatter_value.rb +78 -0
  30. data/lib/ceradoc/core_model/frontmatter/schema_resolver.rb +57 -0
  31. data/lib/ceradoc/core_model/frontmatter/text_splitter.rb +75 -0
  32. data/lib/ceradoc/core_model/frontmatter.rb +72 -0
  33. data/lib/ceradoc/core_model/has_children.rb +23 -0
  34. data/lib/ceradoc/core_model/horizontal_rule_block.rb +16 -0
  35. data/lib/ceradoc/core_model/id_generator.rb +23 -0
  36. data/lib/ceradoc/core_model/image.rb +76 -0
  37. data/lib/ceradoc/core_model/include.rb +43 -0
  38. data/lib/ceradoc/core_model/include_level_offset.rb +71 -0
  39. data/lib/ceradoc/core_model/include_options.rb +101 -0
  40. data/lib/ceradoc/core_model/inline_content.rb +78 -0
  41. data/lib/ceradoc/core_model/inline_element.rb +209 -0
  42. data/lib/ceradoc/core_model/list_block.rb +137 -0
  43. data/lib/ceradoc/core_model/list_item.rb +129 -0
  44. data/lib/ceradoc/core_model/listing_block.rb +19 -0
  45. data/lib/ceradoc/core_model/literal_block.rb +16 -0
  46. data/lib/ceradoc/core_model/metadata.rb +71 -0
  47. data/lib/ceradoc/core_model/open_block.rb +12 -0
  48. data/lib/ceradoc/core_model/output_artifact.rb +48 -0
  49. data/lib/ceradoc/core_model/paragraph_block.rb +16 -0
  50. data/lib/ceradoc/core_model/partintro_block.rb +11 -0
  51. data/lib/ceradoc/core_model/pass_block.rb +16 -0
  52. data/lib/ceradoc/core_model/quote_block.rb +14 -0
  53. data/lib/ceradoc/core_model/raw_inline_element.rb +16 -0
  54. data/lib/ceradoc/core_model/reviewer_block.rb +12 -0
  55. data/lib/ceradoc/core_model/sidebar_block.rb +12 -0
  56. data/lib/ceradoc/core_model/source_block.rb +16 -0
  57. data/lib/ceradoc/core_model/stem_block.rb +25 -0
  58. data/lib/ceradoc/core_model/structural_element.rb +157 -0
  59. data/lib/ceradoc/core_model/table.rb +148 -0
  60. data/lib/ceradoc/core_model/term.rb +53 -0
  61. data/lib/ceradoc/core_model/text_content.rb +26 -0
  62. data/lib/ceradoc/core_model/toc.rb +99 -0
  63. data/lib/ceradoc/core_model/toc_generator.rb +151 -0
  64. data/lib/ceradoc/core_model/verse_block.rb +14 -0
  65. data/lib/ceradoc/core_model.rb +92 -0
  66. data/lib/ceradoc/dispatch.rb +96 -0
  67. data/lib/ceradoc/errors.rb +387 -0
  68. data/lib/ceradoc/format_catalog.rb +102 -0
  69. data/lib/ceradoc/format_module.rb +78 -0
  70. data/lib/ceradoc/hooks.rb +176 -0
  71. data/lib/ceradoc/include_resolver/filesystem.rb +84 -0
  72. data/lib/ceradoc/include_resolver.rb +67 -0
  73. data/lib/ceradoc/include_selectors/indent.rb +54 -0
  74. data/lib/ceradoc/include_selectors/level_offset.rb +84 -0
  75. data/lib/ceradoc/include_selectors/lines.rb +60 -0
  76. data/lib/ceradoc/include_selectors/tags.rb +136 -0
  77. data/lib/ceradoc/include_selectors.rb +26 -0
  78. data/lib/ceradoc/introspection/element_counter.rb +48 -0
  79. data/lib/ceradoc/introspection.rb +72 -0
  80. data/lib/ceradoc/link_rewriter/identity.rb +13 -0
  81. data/lib/ceradoc/link_rewriter/visitor.rb +148 -0
  82. data/lib/ceradoc/link_rewriter.rb +37 -0
  83. data/lib/ceradoc/lint/registry.rb +33 -0
  84. data/lib/ceradoc/lint/rule.rb +41 -0
  85. data/lib/ceradoc/lint/runner.rb +67 -0
  86. data/lib/ceradoc/lint/template.rb +190 -0
  87. data/lib/ceradoc/lint/violation.rb +16 -0
  88. data/lib/ceradoc/lint.rb +44 -0
  89. data/lib/ceradoc/logger.rb +54 -0
  90. data/lib/ceradoc/metanorma/block_builder.rb +130 -0
  91. data/lib/ceradoc/metanorma/builder.rb +397 -0
  92. data/lib/ceradoc/metanorma/iso_builder.rb +67 -0
  93. data/lib/ceradoc/metanorma.rb +7 -0
  94. data/lib/ceradoc/performance_regression.rb +121 -0
  95. data/lib/ceradoc/pipeline.rb +169 -0
  96. data/lib/ceradoc/query.rb +486 -0
  97. data/lib/ceradoc/reference/address/anchor.rb +48 -0
  98. data/lib/ceradoc/reference/address/doi.rb +40 -0
  99. data/lib/ceradoc/reference/address/isbn.rb +39 -0
  100. data/lib/ceradoc/reference/address/path.rb +66 -0
  101. data/lib/ceradoc/reference/address/scoped_path.rb +47 -0
  102. data/lib/ceradoc/reference/address/url.rb +41 -0
  103. data/lib/ceradoc/reference/address.rb +153 -0
  104. data/lib/ceradoc/reference/catalog/composite.rb +59 -0
  105. data/lib/ceradoc/reference/catalog/local.rb +97 -0
  106. data/lib/ceradoc/reference/catalog/memory_index.rb +59 -0
  107. data/lib/ceradoc/reference/catalog.rb +33 -0
  108. data/lib/ceradoc/reference/edge/citation_options.rb +16 -0
  109. data/lib/ceradoc/reference/edge/footnote_ref_options.rb +13 -0
  110. data/lib/ceradoc/reference/edge/image_ref_options.rb +15 -0
  111. data/lib/ceradoc/reference/edge/include_options.rb +15 -0
  112. data/lib/ceradoc/reference/edge/kind.rb +72 -0
  113. data/lib/ceradoc/reference/edge/link_options.rb +12 -0
  114. data/lib/ceradoc/reference/edge/navigation_options.rb +12 -0
  115. data/lib/ceradoc/reference/edge/options.rb +13 -0
  116. data/lib/ceradoc/reference/edge.rb +80 -0
  117. data/lib/ceradoc/reference/edge_search.rb +128 -0
  118. data/lib/ceradoc/reference/materializer/base.rb +44 -0
  119. data/lib/ceradoc/reference/materializer/passthrough.rb +31 -0
  120. data/lib/ceradoc/reference/materializer/registry.rb +116 -0
  121. data/lib/ceradoc/reference/materializer.rb +14 -0
  122. data/lib/ceradoc/reference/presentation/base.rb +47 -0
  123. data/lib/ceradoc/reference/presentation/custom_hierarchy.rb +104 -0
  124. data/lib/ceradoc/reference/presentation/page.rb +20 -0
  125. data/lib/ceradoc/reference/presentation/single_document.rb +35 -0
  126. data/lib/ceradoc/reference/presentation/split_pages.rb +118 -0
  127. data/lib/ceradoc/reference/presentation.rb +16 -0
  128. data/lib/ceradoc/reference/resolution.rb +184 -0
  129. data/lib/ceradoc/reference/resolver/base.rb +15 -0
  130. data/lib/ceradoc/reference/resolver/caching.rb +37 -0
  131. data/lib/ceradoc/reference/resolver/catalog.rb +69 -0
  132. data/lib/ceradoc/reference/resolver/chain.rb +33 -0
  133. data/lib/ceradoc/reference/resolver.rb +20 -0
  134. data/lib/ceradoc/reference/result/ambiguous.rb +23 -0
  135. data/lib/ceradoc/reference/result/base.rb +46 -0
  136. data/lib/ceradoc/reference/result/missing.rb +14 -0
  137. data/lib/ceradoc/reference/result/resolved.rb +22 -0
  138. data/lib/ceradoc/reference/result.rb +20 -0
  139. data/lib/ceradoc/reference.rb +65 -0
  140. data/lib/ceradoc/registry.rb +156 -0
  141. data/lib/ceradoc/relative_path.rb +32 -0
  142. data/lib/ceradoc/resolve_includes.rb +202 -0
  143. data/lib/ceradoc/source_graph/file_node.rb +147 -0
  144. data/lib/ceradoc/source_graph/graph.rb +165 -0
  145. data/lib/ceradoc/source_graph/include_edges.rb +33 -0
  146. data/lib/ceradoc/source_graph/sidecar.rb +97 -0
  147. data/lib/ceradoc/source_graph.rb +36 -0
  148. data/lib/ceradoc/validation.rb +686 -0
  149. data/lib/ceradoc/version.rb +5 -0
  150. data/lib/ceradoc/visitor.rb +283 -0
  151. data/lib/ceradoc.rb +41 -0
  152. metadata +225 -0
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ceradoc
4
+ module Metanorma
5
+ # ISO has its own Serializable root and bibliographic vocabulary. Construct
6
+ # those types directly; it is not a Standoc root subclass or an XML cast.
7
+ class IsoBuilder < Builder
8
+ ISO = ::Metanorma::Iso::Document
9
+
10
+ def build(document)
11
+ root = super
12
+ root.flavor = flavor_name if root.respond_to?(:flavor=)
13
+ root
14
+ end
15
+
16
+ private
17
+
18
+ def flavor_name
19
+ @root_class.name.to_s[/\AMetanorma::(\w+)::Document::Root\z/, 1]&.downcase || 'iso'
20
+ end
21
+
22
+ def native_root_class?
23
+ self.class.native_root?(@root_class)
24
+ end
25
+
26
+ def sections_class
27
+ ISO::Sections::IsoSections
28
+ end
29
+
30
+ def clause_class
31
+ ISO::Sections::IsoClauseSection
32
+ end
33
+
34
+ def section_model(node, at, parent)
35
+ section = super
36
+ section.type = 'scope' if parent.nil? && node.title.to_s.strip.casecmp('Scope').zero?
37
+ section
38
+ end
39
+
40
+ def metadata_keys
41
+ super + %w[mn-document-class doctype]
42
+ end
43
+
44
+ def bibliography_data(document)
45
+ title, values = document_metadata(document)
46
+ unsupported(document, 'document/id', 'ISO bibdata cannot represent a document anchor') if document.id
47
+ unsupported(document, 'document/attributes', 'mn-document-class does not match ISO root') if values['mn-document-class'] && values['mn-document-class'] != 'iso'
48
+ metadata = ISO::Metadata
49
+ bib = metadata::IsoBibliographicItem.new
50
+ if title
51
+ bib.titles = [metadata::TitleFull.new(_type: 'main', value: title,
52
+ language: values['language'])]
53
+ end
54
+ bib.docnumber = values['docnumber'].to_s if values['docnumber']
55
+ bib.doc_identifier = [metadata::DocIdentifier.new(value: values['docidentifier'])] if values['docidentifier']
56
+ bib.language = [metadata::LanguageElement.new(value: values['language'], current: 'true')] if values['language']
57
+ bib.copyright = copyright_data(document, values)
58
+ if values['doctype']
59
+ bib.ext = metadata::IsoBibDataExtensionType.new(
60
+ doctype_element: [metadata::DoctypeElement.new(value: values['doctype'])]
61
+ )
62
+ end
63
+ bib
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Optional output integration. Generic Ceradoc users do not load the SDK.
4
+ require 'ceradoc'
5
+ require 'metanorma/document'
6
+ require 'metanorma/standoc/document'
7
+ require_relative 'metanorma/builder'
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'benchmark'
4
+ require 'json'
5
+
6
+ module Ceradoc
7
+ module PerformanceRegression
8
+ THRESHOLDS = {
9
+ markdown_parse_small: 2.0,
10
+ markdown_parse_medium: 3.0,
11
+ asciidoc_parse: 5.0,
12
+ html_serialize: 5.0,
13
+ md_to_html: 10.0,
14
+ adoc_to_html: 10.0,
15
+ core_model_creation: 1.0
16
+ }.freeze
17
+
18
+ BenchmarkResult = Struct.new(:name, :duration, :iterations, :threshold, keyword_init: true) do
19
+ def to_h
20
+ { name:, duration:, iterations:, threshold:, passed: passed? }
21
+ end
22
+
23
+ def passed?
24
+ duration < threshold
25
+ end
26
+
27
+ def format_line
28
+ status = passed? ? 'PASS' : 'FAIL'
29
+ " #{status} #{name}: #{duration.round(4)}s (threshold: #{threshold}s)"
30
+ end
31
+ end
32
+
33
+ ComparisonResult = Struct.new(:name, :duration, :baseline, keyword_init: true) do
34
+ def regressed?(pct = 0.2)
35
+ return false if baseline.nil? || baseline.zero?
36
+
37
+ (duration - baseline).abs / baseline > pct
38
+ end
39
+
40
+ def format_line
41
+ status = regressed? ? 'WARN' : 'OK'
42
+ baseline_str = baseline ? "(baseline: #{baseline.round(4)}s)" : '(no baseline)'
43
+ " #{status} #{name}: #{duration.round(4)}s #{baseline_str}"
44
+ end
45
+ end
46
+
47
+ class << self
48
+ def run_all(iterations: 3)
49
+ benchmarks = build_benchmarks
50
+ benchmarks.map do |name, threshold, block|
51
+ times = []
52
+ iterations.times { times << Benchmark.realtime { block.call } }
53
+ avg = times.sum / times.size
54
+ BenchmarkResult.new(name:, duration: avg, iterations:, threshold:)
55
+ end
56
+ end
57
+
58
+ def run_all_with_summary(iterations: 3)
59
+ results = run_all(iterations:)
60
+ failed = results.count { |r| !r.passed? }
61
+ { results:, failed_count: failed, total: results.size }
62
+ end
63
+
64
+ def print_results(summary_or_results)
65
+ if summary_or_results.is_a?(Hash)
66
+ summary = summary_or_results
67
+ summary[:results].each { |r| puts r.format_line }
68
+ puts
69
+ puts "#{summary[:total] - summary[:failed_count]}/#{summary[:total]} passed"
70
+ else
71
+ Array(summary_or_results).each { |r| puts r.format_line }
72
+ end
73
+ end
74
+
75
+ def compare_with_baseline(baseline_path, iterations: 3)
76
+ return [] unless File.exist?(baseline_path)
77
+
78
+ baseline_data = JSON.parse(File.read(baseline_path))
79
+ baseline_map = baseline_data.each_with_object({}) { |d, m| m[d['name']] = d['duration'] }
80
+
81
+ results = run_all(iterations:)
82
+ results.map do |r|
83
+ ComparisonResult.new(name: r.name, duration: r.duration, baseline: baseline_map[r.name])
84
+ end
85
+ end
86
+
87
+ private
88
+
89
+ def build_benchmarks
90
+ sample = {
91
+ markdown_small: "# Title\n\n#{'Paragraph ' * 10}",
92
+ markdown_medium: "# Title\n\n#{'Content. ' * 100}\n\n## Section\n\n#{'More. ' * 100}",
93
+ asciidoc: "= Title\nAuthor\n\n== Section\n\n#{'Paragraph content. ' * 50}",
94
+ html_doc: Ceradoc::CoreModel::DocumentElement.new(
95
+ title: 'Bench',
96
+ children: [
97
+ Ceradoc::CoreModel::Block.new(element_type: 'paragraph', content: 'Test')
98
+ ]
99
+ )
100
+ }
101
+
102
+ [
103
+ [:markdown_parse_small, THRESHOLDS[:markdown_parse_small],
104
+ -> { Ceradoc::Markdown.parse(sample[:markdown_small]) }],
105
+ [:markdown_parse_medium, THRESHOLDS[:markdown_parse_medium],
106
+ -> { Ceradoc::Markdown.parse(sample[:markdown_medium]) }],
107
+ [:asciidoc_parse, THRESHOLDS[:asciidoc_parse],
108
+ -> { Ceradoc.parse(sample[:asciidoc], format: :asciidoc) }],
109
+ [:html_serialize, THRESHOLDS[:html_serialize],
110
+ -> { Ceradoc::Html.serialize_static(sample[:html_doc]) }],
111
+ [:md_to_html, THRESHOLDS[:md_to_html],
112
+ -> { Ceradoc.convert(sample[:markdown_small], from: :markdown, to: :html) }],
113
+ [:adoc_to_html, THRESHOLDS[:adoc_to_html],
114
+ -> { Ceradoc.convert(sample[:asciidoc], from: :asciidoc, to: :html) }],
115
+ [:core_model_creation, THRESHOLDS[:core_model_creation],
116
+ -> { 1000.times { Ceradoc::CoreModel::Block.new(element_type: 'paragraph', content: 'x') } }]
117
+ ]
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,169 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ceradoc
4
+ # Parse / serialize / convert pipeline. Single source of truth for
5
+ # the document transformation flow, extracted from the top-level
6
+ # Ceradoc façade so pipeline logic has its own home and its own
7
+ # spec surface. Public API on +Ceradoc+ delegates here.
8
+ module Pipeline
9
+ class << self
10
+ # Parse text to a document model. Graph mode: +include::+
11
+ # directives survive as +CoreModel::Include+ link nodes — no
12
+ # file I/O happens during parse. Splicing included content is
13
+ # a separate, explicit step (see +Ceradoc.resolve_includes+).
14
+ def parse(text, format:)
15
+ format_module = FormatCatalog.get_format(format)
16
+ unless format_module
17
+ raise UnsupportedFormatError.new(format,
18
+ available: FormatCatalog.registered_formats)
19
+ end
20
+
21
+ text = Hooks.invoke(:before_parse, text, format: format)
22
+ result = format_module.parse_to_core(text)
23
+ Hooks.invoke(:after_parse, result, format: format)
24
+ end
25
+
26
+ def resolve_includes(document, base_dir:,
27
+ missing_include: :error,
28
+ max_depth: Ceradoc::ResolveIncludes::DEFAULT_MAX_DEPTH,
29
+ allow_unsafe: false,
30
+ resolver: nil)
31
+ resolver = Ceradoc::IncludeResolver.coerce(
32
+ resolver,
33
+ base_dir: base_dir,
34
+ allow_unsafe: allow_unsafe
35
+ )
36
+ Ceradoc::ResolveIncludes.call(
37
+ document,
38
+ resolver: resolver,
39
+ base_dir: base_dir,
40
+ missing_include: missing_include,
41
+ max_depth: max_depth
42
+ )
43
+ end
44
+
45
+ def rewrite_links(document, rewriter: nil, &)
46
+ Ceradoc::LinkRewriter.rewrite(document, rewriter: rewriter, &)
47
+ end
48
+
49
+ # Resolve every reference (xref, citation, link, include, image,
50
+ # footnote) in a parsed document using a unified content-graph
51
+ # model. Mirrors +resolve_includes+ in shape: two-step, immutable.
52
+ #
53
+ # Step one always runs: every Edge is resolved through the
54
+ # Resolver and the +missing+/+ambiguous+ policies are enforced
55
+ # (raise or warn). Step two is opt-in: with +materialize: true+
56
+ # the tree is rebuilt with each Edge replaced by the output of
57
+ # the Materializer registered for its
58
+ # [kind, presentation, format] tuple.
59
+ #
60
+ # The input document is never mutated. With +materialize: false+
61
+ # the input document itself is returned; with +materialize: true+
62
+ # a new document is returned that structurally shares untouched
63
+ # subtrees with the input (treat both as immutable). Nodes whose
64
+ # kind has no registered materializer are preserved unchanged.
65
+ #
66
+ # @param document [CoreModel::Base] parsed document
67
+ # @param catalog [Reference::Catalog::*] index of addressable Content
68
+ # @param presentation [Reference::Presentation::Base] slicing and ordering
69
+ # @param resolver [Reference::Resolver::Base, nil] defaults to CatalogResolver
70
+ # @param missing [Symbol] :warn (default), :silent, :error, :passthrough
71
+ # @param ambiguous [Symbol] :disambiguate (default), :first, :error
72
+ # @param materialize [Boolean] when true, replace edges with rendered inlines
73
+ # @param format [Symbol, nil] target format for materializer lookup
74
+ # (:html, :asciidoc, ...); nil matches format-agnostic materializers
75
+ # @return [CoreModel::Base] the input document (validation only) or
76
+ # a new materialized document
77
+ #
78
+ # @example Resolve cross-references into HTML links
79
+ # doc = Ceradoc.parse(text, format: :asciidoc)
80
+ # catalog = Ceradoc::Reference::Catalog::Local.from_doc(doc)
81
+ # presentation = Ceradoc::Reference::Presentation::SingleDocument.new
82
+ # resolved = Ceradoc.resolve_references(
83
+ # doc,
84
+ # catalog: catalog,
85
+ # presentation: presentation,
86
+ # materialize: true,
87
+ # format: :html
88
+ # )
89
+ def resolve_references(document, catalog:, presentation:,
90
+ resolver: nil,
91
+ missing: :warn,
92
+ ambiguous: :disambiguate,
93
+ materialize: false,
94
+ format: nil)
95
+ Ceradoc::Reference::Resolution.new(
96
+ catalog: catalog,
97
+ presentation: presentation,
98
+ resolver: resolver,
99
+ missing: missing,
100
+ ambiguous: ambiguous,
101
+ materialize: materialize,
102
+ format: format
103
+ ).call(document)
104
+ end
105
+
106
+ def convert(text, from:, to:, **)
107
+ core = parse(text, format: from)
108
+ serialize(core, to: to, **)
109
+ end
110
+
111
+ def to_core(model)
112
+ return model if model.is_a?(CoreModel::Base)
113
+
114
+ FormatCatalog.registry.each_value do |format_module|
115
+ next unless format_module.handles_model?(model)
116
+
117
+ return format_module.to_core(model)
118
+ end
119
+
120
+ raise TransformationError, "No transformer found for #{model.class}"
121
+ end
122
+
123
+ def serialize(model, to:, allow_unresolved_includes: false, **)
124
+ format_module = FormatCatalog.get_format(to)
125
+ raise UnsupportedFormatError.new(to, available: FormatCatalog.registered_formats) unless format_module
126
+
127
+ Ceradoc::Validation.guard_unresolved_includes!(model, format_module) unless allow_unresolved_includes
128
+
129
+ model = Hooks.invoke(:before_serialize, model, format: to)
130
+ result = format_module.serialize(model, **)
131
+ Hooks.invoke(:after_serialize, result, format: to)
132
+ end
133
+
134
+ def build(&)
135
+ CoreModel::DocumentElement.build(children: [], &)
136
+ end
137
+
138
+ def parse_file(path, format: nil)
139
+ raise FileNotFoundError, path unless File.exist?(path)
140
+
141
+ source_format = format || FormatCatalog.detect_format(path)
142
+ raise UnsupportedFormatError, "Could not detect format for: #{path}" unless source_format
143
+
144
+ format_module = FormatCatalog.get_format(source_format)
145
+ unless format_module
146
+ raise UnsupportedFormatError.new(source_format,
147
+ available: FormatCatalog.registered_formats)
148
+ end
149
+
150
+ if FormatCatalog.binary_format?(source_format)
151
+ format_module.parse_to_core(path)
152
+ else
153
+ content = File.read(path)
154
+ content = Hooks.invoke(:before_parse, content, format: source_format)
155
+ result = format_module.parse_file_to_core(path, content)
156
+ Hooks.invoke(:after_parse, result, format: source_format)
157
+ end
158
+ end
159
+
160
+ def convert_file(path, to:, from: nil, **)
161
+ source_format = from || FormatCatalog.detect_format(path)
162
+ raise UnsupportedFormatError, "Could not detect format for: #{path}" unless source_format
163
+
164
+ core = parse_file(path, format: source_format)
165
+ serialize(core, to: to, **)
166
+ end
167
+ end
168
+ end
169
+ end