coridoc 3.0.0
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.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/Rakefile +8 -0
- data/exe/coridoc +24 -0
- data/lib/coridoc/binding_inflater.rb +118 -0
- data/lib/coridoc/cli.rb +311 -0
- data/lib/coridoc/configurable.rb +530 -0
- data/lib/coridoc/core_model/abstract_block.rb +11 -0
- data/lib/coridoc/core_model/annotation_block.rb +57 -0
- data/lib/coridoc/core_model/attribute_reference_resolver.rb +155 -0
- data/lib/coridoc/core_model/base.rb +281 -0
- data/lib/coridoc/core_model/bibliography.rb +41 -0
- data/lib/coridoc/core_model/bibliography_entry.rb +48 -0
- data/lib/coridoc/core_model/block.rb +69 -0
- data/lib/coridoc/core_model/callout.rb +32 -0
- data/lib/coridoc/core_model/callout_text.rb +35 -0
- data/lib/coridoc/core_model/children_content.rb +72 -0
- data/lib/coridoc/core_model/comment_block.rb +16 -0
- data/lib/coridoc/core_model/comment_line.rb +23 -0
- data/lib/coridoc/core_model/definition_item.rb +68 -0
- data/lib/coridoc/core_model/definition_list.rb +28 -0
- data/lib/coridoc/core_model/element_attribute.rb +14 -0
- data/lib/coridoc/core_model/example_block.rb +12 -0
- data/lib/coridoc/core_model/footnote.rb +92 -0
- data/lib/coridoc/core_model/frontmatter/codec.rb +151 -0
- data/lib/coridoc/core_model/frontmatter/field_transform.rb +75 -0
- data/lib/coridoc/core_model/frontmatter/frontmatter_entry.rb +22 -0
- data/lib/coridoc/core_model/frontmatter/frontmatter_value.rb +78 -0
- data/lib/coridoc/core_model/frontmatter/schema_resolver.rb +57 -0
- data/lib/coridoc/core_model/frontmatter/text_splitter.rb +75 -0
- data/lib/coridoc/core_model/frontmatter.rb +72 -0
- data/lib/coridoc/core_model/has_children.rb +23 -0
- data/lib/coridoc/core_model/horizontal_rule_block.rb +16 -0
- data/lib/coridoc/core_model/id_generator.rb +23 -0
- data/lib/coridoc/core_model/image.rb +69 -0
- data/lib/coridoc/core_model/include.rb +43 -0
- data/lib/coridoc/core_model/include_level_offset.rb +71 -0
- data/lib/coridoc/core_model/include_options.rb +101 -0
- data/lib/coridoc/core_model/inline_content.rb +78 -0
- data/lib/coridoc/core_model/inline_element.rb +198 -0
- data/lib/coridoc/core_model/list_block.rb +137 -0
- data/lib/coridoc/core_model/list_item.rb +129 -0
- data/lib/coridoc/core_model/listing_block.rb +19 -0
- data/lib/coridoc/core_model/literal_block.rb +16 -0
- data/lib/coridoc/core_model/metadata.rb +71 -0
- data/lib/coridoc/core_model/open_block.rb +12 -0
- data/lib/coridoc/core_model/output_artifact.rb +48 -0
- data/lib/coridoc/core_model/paragraph_block.rb +16 -0
- data/lib/coridoc/core_model/partintro_block.rb +11 -0
- data/lib/coridoc/core_model/pass_block.rb +16 -0
- data/lib/coridoc/core_model/quote_block.rb +14 -0
- data/lib/coridoc/core_model/raw_inline_element.rb +16 -0
- data/lib/coridoc/core_model/reviewer_block.rb +12 -0
- data/lib/coridoc/core_model/sidebar_block.rb +12 -0
- data/lib/coridoc/core_model/source_block.rb +16 -0
- data/lib/coridoc/core_model/stem_block.rb +25 -0
- data/lib/coridoc/core_model/structural_element.rb +157 -0
- data/lib/coridoc/core_model/table.rb +148 -0
- data/lib/coridoc/core_model/term.rb +53 -0
- data/lib/coridoc/core_model/text_content.rb +26 -0
- data/lib/coridoc/core_model/toc.rb +99 -0
- data/lib/coridoc/core_model/toc_generator.rb +151 -0
- data/lib/coridoc/core_model/verse_block.rb +14 -0
- data/lib/coridoc/core_model.rb +92 -0
- data/lib/coridoc/coridoc.rb +164 -0
- data/lib/coridoc/dispatch.rb +96 -0
- data/lib/coridoc/errors.rb +387 -0
- data/lib/coridoc/format_catalog.rb +102 -0
- data/lib/coridoc/format_module.rb +78 -0
- data/lib/coridoc/hooks.rb +176 -0
- data/lib/coridoc/include_resolver/filesystem.rb +84 -0
- data/lib/coridoc/include_resolver.rb +67 -0
- data/lib/coridoc/include_selectors/indent.rb +54 -0
- data/lib/coridoc/include_selectors/level_offset.rb +84 -0
- data/lib/coridoc/include_selectors/lines.rb +60 -0
- data/lib/coridoc/include_selectors/tags.rb +136 -0
- data/lib/coridoc/include_selectors.rb +26 -0
- data/lib/coridoc/introspection/element_counter.rb +48 -0
- data/lib/coridoc/introspection.rb +72 -0
- data/lib/coridoc/link_rewriter/identity.rb +13 -0
- data/lib/coridoc/link_rewriter/visitor.rb +148 -0
- data/lib/coridoc/link_rewriter.rb +37 -0
- data/lib/coridoc/lint/registry.rb +33 -0
- data/lib/coridoc/lint/rule.rb +41 -0
- data/lib/coridoc/lint/runner.rb +67 -0
- data/lib/coridoc/lint/template.rb +190 -0
- data/lib/coridoc/lint/violation.rb +16 -0
- data/lib/coridoc/lint.rb +44 -0
- data/lib/coridoc/logger.rb +54 -0
- data/lib/coridoc/performance_regression.rb +121 -0
- data/lib/coridoc/pipeline.rb +169 -0
- data/lib/coridoc/query.rb +486 -0
- data/lib/coridoc/reference/address/anchor.rb +48 -0
- data/lib/coridoc/reference/address/doi.rb +40 -0
- data/lib/coridoc/reference/address/isbn.rb +39 -0
- data/lib/coridoc/reference/address/path.rb +66 -0
- data/lib/coridoc/reference/address/scoped_path.rb +47 -0
- data/lib/coridoc/reference/address/url.rb +41 -0
- data/lib/coridoc/reference/address.rb +153 -0
- data/lib/coridoc/reference/catalog/composite.rb +59 -0
- data/lib/coridoc/reference/catalog/local.rb +97 -0
- data/lib/coridoc/reference/catalog/memory_index.rb +59 -0
- data/lib/coridoc/reference/catalog.rb +33 -0
- data/lib/coridoc/reference/edge/citation_options.rb +16 -0
- data/lib/coridoc/reference/edge/footnote_ref_options.rb +13 -0
- data/lib/coridoc/reference/edge/image_ref_options.rb +15 -0
- data/lib/coridoc/reference/edge/include_options.rb +15 -0
- data/lib/coridoc/reference/edge/kind.rb +72 -0
- data/lib/coridoc/reference/edge/link_options.rb +12 -0
- data/lib/coridoc/reference/edge/navigation_options.rb +12 -0
- data/lib/coridoc/reference/edge/options.rb +13 -0
- data/lib/coridoc/reference/edge.rb +80 -0
- data/lib/coridoc/reference/edge_search.rb +128 -0
- data/lib/coridoc/reference/materializer/base.rb +44 -0
- data/lib/coridoc/reference/materializer/passthrough.rb +31 -0
- data/lib/coridoc/reference/materializer/registry.rb +116 -0
- data/lib/coridoc/reference/materializer.rb +14 -0
- data/lib/coridoc/reference/presentation/base.rb +47 -0
- data/lib/coridoc/reference/presentation/custom_hierarchy.rb +104 -0
- data/lib/coridoc/reference/presentation/page.rb +20 -0
- data/lib/coridoc/reference/presentation/single_document.rb +35 -0
- data/lib/coridoc/reference/presentation/split_pages.rb +118 -0
- data/lib/coridoc/reference/presentation.rb +16 -0
- data/lib/coridoc/reference/resolution.rb +184 -0
- data/lib/coridoc/reference/resolver/base.rb +15 -0
- data/lib/coridoc/reference/resolver/caching.rb +37 -0
- data/lib/coridoc/reference/resolver/catalog.rb +69 -0
- data/lib/coridoc/reference/resolver/chain.rb +33 -0
- data/lib/coridoc/reference/resolver.rb +20 -0
- data/lib/coridoc/reference/result/ambiguous.rb +23 -0
- data/lib/coridoc/reference/result/base.rb +46 -0
- data/lib/coridoc/reference/result/missing.rb +14 -0
- data/lib/coridoc/reference/result/resolved.rb +22 -0
- data/lib/coridoc/reference/result.rb +20 -0
- data/lib/coridoc/reference.rb +65 -0
- data/lib/coridoc/registry.rb +156 -0
- data/lib/coridoc/relative_path.rb +32 -0
- data/lib/coridoc/resolve_includes.rb +202 -0
- data/lib/coridoc/source_graph/file_node.rb +147 -0
- data/lib/coridoc/source_graph/graph.rb +165 -0
- data/lib/coridoc/source_graph/include_edges.rb +33 -0
- data/lib/coridoc/source_graph/sidecar.rb +97 -0
- data/lib/coridoc/source_graph.rb +36 -0
- data/lib/coridoc/validation.rb +686 -0
- data/lib/coridoc/version.rb +5 -0
- data/lib/coridoc/visitor.rb +283 -0
- data/lib/coridoc.rb +41 -0
- metadata +221 -0
data/lib/coridoc/lint.rb
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
# Model-driven linting (#165).
|
|
5
|
+
#
|
|
6
|
+
# Rules live in the FORMAT gems and check the format-specific model
|
|
7
|
+
# tree — never raw text, never a homogenized CoreModel. Each rule
|
|
8
|
+
# declares its format and its check; the framework selects rules by
|
|
9
|
+
# format and runs them against the parsed tree a SourceGraph node
|
|
10
|
+
# exposes (parsing lazily, only files actually linted).
|
|
11
|
+
#
|
|
12
|
+
# require "coridoc/asciidoc" # or any format gem you lint
|
|
13
|
+
# violations = Coridoc::Lint.run("doc.adoc")
|
|
14
|
+
# violations.each { |v| puts "#{v.path}:#{v.line} [#{v.rule_id}] #{v.message}" }
|
|
15
|
+
#
|
|
16
|
+
# Format gems must be required (or +format:+ passed) — extension
|
|
17
|
+
# detection only sees registered formats, like the rest of Coridoc.
|
|
18
|
+
module Lint
|
|
19
|
+
autoload :Violation, "#{__dir__}/lint/violation"
|
|
20
|
+
autoload :Rule, "#{__dir__}/lint/rule"
|
|
21
|
+
autoload :Template, "#{__dir__}/lint/template"
|
|
22
|
+
autoload :Registry, "#{__dir__}/lint/registry"
|
|
23
|
+
autoload :Runner, "#{__dir__}/lint/runner"
|
|
24
|
+
|
|
25
|
+
class << self
|
|
26
|
+
def registry
|
|
27
|
+
@registry ||= Registry.new
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Lint files (or an existing SourceGraph) with the rules
|
|
31
|
+
# registered for each file's format.
|
|
32
|
+
#
|
|
33
|
+
# @param sources [String, Array<String>, SourceGraph::Graph]
|
|
34
|
+
# @param format [Symbol, nil] force a format instead of
|
|
35
|
+
# detecting from the extension
|
|
36
|
+
# @param only [Array<Symbol>] restrict to these rule ids
|
|
37
|
+
# @param except [Array<Symbol>] skip these rule ids
|
|
38
|
+
# @return [Array<Violation>]
|
|
39
|
+
def run(sources, format: nil, only: nil, except: nil)
|
|
40
|
+
Runner.new(format: format, only: only, except: except).run(sources)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
class Logger
|
|
5
|
+
BADGE = 'Coridoc'
|
|
6
|
+
|
|
7
|
+
COLORS = {
|
|
8
|
+
error: "\e[31m", # Red
|
|
9
|
+
info: "\e[34m", # Blue
|
|
10
|
+
reset: "\e[m", # Reset
|
|
11
|
+
success: "\e[32m", # Green
|
|
12
|
+
warn: "\e[33m", # Yellow
|
|
13
|
+
bold: "\e[1m",
|
|
14
|
+
unbold: "\e[22m"
|
|
15
|
+
}.freeze
|
|
16
|
+
|
|
17
|
+
def self.error(message)
|
|
18
|
+
log(message, :error)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.info(message)
|
|
22
|
+
log(message, :info)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.success(message)
|
|
26
|
+
log(message, :success)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.warn(message)
|
|
30
|
+
log(message, :warn)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.log(message, type)
|
|
34
|
+
Warning.warn format_message(message, type)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.format_message(message, type)
|
|
38
|
+
colorize(
|
|
39
|
+
"\n[#{BADGE}] #{COLORS[:bold]}#{type.upcase}#{COLORS[:unbold]}" \
|
|
40
|
+
": #{message}\n",
|
|
41
|
+
type
|
|
42
|
+
)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.colorize(message, type)
|
|
46
|
+
io = type == :warn ? $stderr : $stdout
|
|
47
|
+
return message unless io.tty?
|
|
48
|
+
|
|
49
|
+
"#{COLORS[type]}#{message}#{COLORS[:reset]}"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private_class_method :log, :format_message, :colorize
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'benchmark'
|
|
4
|
+
require 'json'
|
|
5
|
+
|
|
6
|
+
module Coridoc
|
|
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: Coridoc::CoreModel::DocumentElement.new(
|
|
95
|
+
title: 'Bench',
|
|
96
|
+
children: [
|
|
97
|
+
Coridoc::CoreModel::Block.new(element_type: 'paragraph', content: 'Test')
|
|
98
|
+
]
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
[
|
|
103
|
+
[:markdown_parse_small, THRESHOLDS[:markdown_parse_small],
|
|
104
|
+
-> { Coridoc::Markdown.parse(sample[:markdown_small]) }],
|
|
105
|
+
[:markdown_parse_medium, THRESHOLDS[:markdown_parse_medium],
|
|
106
|
+
-> { Coridoc::Markdown.parse(sample[:markdown_medium]) }],
|
|
107
|
+
[:asciidoc_parse, THRESHOLDS[:asciidoc_parse],
|
|
108
|
+
-> { Coridoc.parse(sample[:asciidoc], format: :asciidoc) }],
|
|
109
|
+
[:html_serialize, THRESHOLDS[:html_serialize],
|
|
110
|
+
-> { Coridoc::Html.serialize_static(sample[:html_doc]) }],
|
|
111
|
+
[:md_to_html, THRESHOLDS[:md_to_html],
|
|
112
|
+
-> { Coridoc.convert(sample[:markdown_small], from: :markdown, to: :html) }],
|
|
113
|
+
[:adoc_to_html, THRESHOLDS[:adoc_to_html],
|
|
114
|
+
-> { Coridoc.convert(sample[:asciidoc], from: :asciidoc, to: :html) }],
|
|
115
|
+
[:core_model_creation, THRESHOLDS[:core_model_creation],
|
|
116
|
+
-> { 1000.times { Coridoc::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 Coridoc
|
|
4
|
+
# Parse / serialize / convert pipeline. Single source of truth for
|
|
5
|
+
# the document transformation flow, extracted from the top-level
|
|
6
|
+
# Coridoc façade so pipeline logic has its own home and its own
|
|
7
|
+
# spec surface. Public API on +Coridoc+ 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 +Coridoc.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: Coridoc::ResolveIncludes::DEFAULT_MAX_DEPTH,
|
|
29
|
+
allow_unsafe: false,
|
|
30
|
+
resolver: nil)
|
|
31
|
+
resolver = Coridoc::IncludeResolver.coerce(
|
|
32
|
+
resolver,
|
|
33
|
+
base_dir: base_dir,
|
|
34
|
+
allow_unsafe: allow_unsafe
|
|
35
|
+
)
|
|
36
|
+
Coridoc::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
|
+
Coridoc::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 = Coridoc.parse(text, format: :asciidoc)
|
|
80
|
+
# catalog = Coridoc::Reference::Catalog::Local.from_doc(doc)
|
|
81
|
+
# presentation = Coridoc::Reference::Presentation::SingleDocument.new
|
|
82
|
+
# resolved = Coridoc.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
|
+
Coridoc::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
|
+
Coridoc::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
|