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.
Files changed (148) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/Rakefile +8 -0
  4. data/exe/coridoc +24 -0
  5. data/lib/coridoc/binding_inflater.rb +118 -0
  6. data/lib/coridoc/cli.rb +311 -0
  7. data/lib/coridoc/configurable.rb +530 -0
  8. data/lib/coridoc/core_model/abstract_block.rb +11 -0
  9. data/lib/coridoc/core_model/annotation_block.rb +57 -0
  10. data/lib/coridoc/core_model/attribute_reference_resolver.rb +155 -0
  11. data/lib/coridoc/core_model/base.rb +281 -0
  12. data/lib/coridoc/core_model/bibliography.rb +41 -0
  13. data/lib/coridoc/core_model/bibliography_entry.rb +48 -0
  14. data/lib/coridoc/core_model/block.rb +69 -0
  15. data/lib/coridoc/core_model/callout.rb +32 -0
  16. data/lib/coridoc/core_model/callout_text.rb +35 -0
  17. data/lib/coridoc/core_model/children_content.rb +72 -0
  18. data/lib/coridoc/core_model/comment_block.rb +16 -0
  19. data/lib/coridoc/core_model/comment_line.rb +23 -0
  20. data/lib/coridoc/core_model/definition_item.rb +68 -0
  21. data/lib/coridoc/core_model/definition_list.rb +28 -0
  22. data/lib/coridoc/core_model/element_attribute.rb +14 -0
  23. data/lib/coridoc/core_model/example_block.rb +12 -0
  24. data/lib/coridoc/core_model/footnote.rb +92 -0
  25. data/lib/coridoc/core_model/frontmatter/codec.rb +151 -0
  26. data/lib/coridoc/core_model/frontmatter/field_transform.rb +75 -0
  27. data/lib/coridoc/core_model/frontmatter/frontmatter_entry.rb +22 -0
  28. data/lib/coridoc/core_model/frontmatter/frontmatter_value.rb +78 -0
  29. data/lib/coridoc/core_model/frontmatter/schema_resolver.rb +57 -0
  30. data/lib/coridoc/core_model/frontmatter/text_splitter.rb +75 -0
  31. data/lib/coridoc/core_model/frontmatter.rb +72 -0
  32. data/lib/coridoc/core_model/has_children.rb +23 -0
  33. data/lib/coridoc/core_model/horizontal_rule_block.rb +16 -0
  34. data/lib/coridoc/core_model/id_generator.rb +23 -0
  35. data/lib/coridoc/core_model/image.rb +69 -0
  36. data/lib/coridoc/core_model/include.rb +43 -0
  37. data/lib/coridoc/core_model/include_level_offset.rb +71 -0
  38. data/lib/coridoc/core_model/include_options.rb +101 -0
  39. data/lib/coridoc/core_model/inline_content.rb +78 -0
  40. data/lib/coridoc/core_model/inline_element.rb +198 -0
  41. data/lib/coridoc/core_model/list_block.rb +137 -0
  42. data/lib/coridoc/core_model/list_item.rb +129 -0
  43. data/lib/coridoc/core_model/listing_block.rb +19 -0
  44. data/lib/coridoc/core_model/literal_block.rb +16 -0
  45. data/lib/coridoc/core_model/metadata.rb +71 -0
  46. data/lib/coridoc/core_model/open_block.rb +12 -0
  47. data/lib/coridoc/core_model/output_artifact.rb +48 -0
  48. data/lib/coridoc/core_model/paragraph_block.rb +16 -0
  49. data/lib/coridoc/core_model/partintro_block.rb +11 -0
  50. data/lib/coridoc/core_model/pass_block.rb +16 -0
  51. data/lib/coridoc/core_model/quote_block.rb +14 -0
  52. data/lib/coridoc/core_model/raw_inline_element.rb +16 -0
  53. data/lib/coridoc/core_model/reviewer_block.rb +12 -0
  54. data/lib/coridoc/core_model/sidebar_block.rb +12 -0
  55. data/lib/coridoc/core_model/source_block.rb +16 -0
  56. data/lib/coridoc/core_model/stem_block.rb +25 -0
  57. data/lib/coridoc/core_model/structural_element.rb +157 -0
  58. data/lib/coridoc/core_model/table.rb +148 -0
  59. data/lib/coridoc/core_model/term.rb +53 -0
  60. data/lib/coridoc/core_model/text_content.rb +26 -0
  61. data/lib/coridoc/core_model/toc.rb +99 -0
  62. data/lib/coridoc/core_model/toc_generator.rb +151 -0
  63. data/lib/coridoc/core_model/verse_block.rb +14 -0
  64. data/lib/coridoc/core_model.rb +92 -0
  65. data/lib/coridoc/coridoc.rb +164 -0
  66. data/lib/coridoc/dispatch.rb +96 -0
  67. data/lib/coridoc/errors.rb +387 -0
  68. data/lib/coridoc/format_catalog.rb +102 -0
  69. data/lib/coridoc/format_module.rb +78 -0
  70. data/lib/coridoc/hooks.rb +176 -0
  71. data/lib/coridoc/include_resolver/filesystem.rb +84 -0
  72. data/lib/coridoc/include_resolver.rb +67 -0
  73. data/lib/coridoc/include_selectors/indent.rb +54 -0
  74. data/lib/coridoc/include_selectors/level_offset.rb +84 -0
  75. data/lib/coridoc/include_selectors/lines.rb +60 -0
  76. data/lib/coridoc/include_selectors/tags.rb +136 -0
  77. data/lib/coridoc/include_selectors.rb +26 -0
  78. data/lib/coridoc/introspection/element_counter.rb +48 -0
  79. data/lib/coridoc/introspection.rb +72 -0
  80. data/lib/coridoc/link_rewriter/identity.rb +13 -0
  81. data/lib/coridoc/link_rewriter/visitor.rb +148 -0
  82. data/lib/coridoc/link_rewriter.rb +37 -0
  83. data/lib/coridoc/lint/registry.rb +33 -0
  84. data/lib/coridoc/lint/rule.rb +41 -0
  85. data/lib/coridoc/lint/runner.rb +67 -0
  86. data/lib/coridoc/lint/template.rb +190 -0
  87. data/lib/coridoc/lint/violation.rb +16 -0
  88. data/lib/coridoc/lint.rb +44 -0
  89. data/lib/coridoc/logger.rb +54 -0
  90. data/lib/coridoc/performance_regression.rb +121 -0
  91. data/lib/coridoc/pipeline.rb +169 -0
  92. data/lib/coridoc/query.rb +486 -0
  93. data/lib/coridoc/reference/address/anchor.rb +48 -0
  94. data/lib/coridoc/reference/address/doi.rb +40 -0
  95. data/lib/coridoc/reference/address/isbn.rb +39 -0
  96. data/lib/coridoc/reference/address/path.rb +66 -0
  97. data/lib/coridoc/reference/address/scoped_path.rb +47 -0
  98. data/lib/coridoc/reference/address/url.rb +41 -0
  99. data/lib/coridoc/reference/address.rb +153 -0
  100. data/lib/coridoc/reference/catalog/composite.rb +59 -0
  101. data/lib/coridoc/reference/catalog/local.rb +97 -0
  102. data/lib/coridoc/reference/catalog/memory_index.rb +59 -0
  103. data/lib/coridoc/reference/catalog.rb +33 -0
  104. data/lib/coridoc/reference/edge/citation_options.rb +16 -0
  105. data/lib/coridoc/reference/edge/footnote_ref_options.rb +13 -0
  106. data/lib/coridoc/reference/edge/image_ref_options.rb +15 -0
  107. data/lib/coridoc/reference/edge/include_options.rb +15 -0
  108. data/lib/coridoc/reference/edge/kind.rb +72 -0
  109. data/lib/coridoc/reference/edge/link_options.rb +12 -0
  110. data/lib/coridoc/reference/edge/navigation_options.rb +12 -0
  111. data/lib/coridoc/reference/edge/options.rb +13 -0
  112. data/lib/coridoc/reference/edge.rb +80 -0
  113. data/lib/coridoc/reference/edge_search.rb +128 -0
  114. data/lib/coridoc/reference/materializer/base.rb +44 -0
  115. data/lib/coridoc/reference/materializer/passthrough.rb +31 -0
  116. data/lib/coridoc/reference/materializer/registry.rb +116 -0
  117. data/lib/coridoc/reference/materializer.rb +14 -0
  118. data/lib/coridoc/reference/presentation/base.rb +47 -0
  119. data/lib/coridoc/reference/presentation/custom_hierarchy.rb +104 -0
  120. data/lib/coridoc/reference/presentation/page.rb +20 -0
  121. data/lib/coridoc/reference/presentation/single_document.rb +35 -0
  122. data/lib/coridoc/reference/presentation/split_pages.rb +118 -0
  123. data/lib/coridoc/reference/presentation.rb +16 -0
  124. data/lib/coridoc/reference/resolution.rb +184 -0
  125. data/lib/coridoc/reference/resolver/base.rb +15 -0
  126. data/lib/coridoc/reference/resolver/caching.rb +37 -0
  127. data/lib/coridoc/reference/resolver/catalog.rb +69 -0
  128. data/lib/coridoc/reference/resolver/chain.rb +33 -0
  129. data/lib/coridoc/reference/resolver.rb +20 -0
  130. data/lib/coridoc/reference/result/ambiguous.rb +23 -0
  131. data/lib/coridoc/reference/result/base.rb +46 -0
  132. data/lib/coridoc/reference/result/missing.rb +14 -0
  133. data/lib/coridoc/reference/result/resolved.rb +22 -0
  134. data/lib/coridoc/reference/result.rb +20 -0
  135. data/lib/coridoc/reference.rb +65 -0
  136. data/lib/coridoc/registry.rb +156 -0
  137. data/lib/coridoc/relative_path.rb +32 -0
  138. data/lib/coridoc/resolve_includes.rb +202 -0
  139. data/lib/coridoc/source_graph/file_node.rb +147 -0
  140. data/lib/coridoc/source_graph/graph.rb +165 -0
  141. data/lib/coridoc/source_graph/include_edges.rb +33 -0
  142. data/lib/coridoc/source_graph/sidecar.rb +97 -0
  143. data/lib/coridoc/source_graph.rb +36 -0
  144. data/lib/coridoc/validation.rb +686 -0
  145. data/lib/coridoc/version.rb +5 -0
  146. data/lib/coridoc/visitor.rb +283 -0
  147. data/lib/coridoc.rb +41 -0
  148. metadata +221 -0
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module CoreModel
5
+ # Table of Contents Generator for CoreModel documents
6
+ #
7
+ # Generates a Toc model from CoreModel::StructuralElement documents.
8
+ # Supports configurable level ranges and section numbering.
9
+ #
10
+ # @example Basic usage
11
+ # doc = Coridoc::CoreModel::StructuralElement.new(
12
+ # element_type: 'document',
13
+ # children: [section1, section2]
14
+ # )
15
+ # toc = CoreModel::TocGenerator.generate(doc)
16
+ #
17
+ # @example With options
18
+ # toc = CoreModel::TocGenerator.generate(doc,
19
+ # min_level: 2,
20
+ # max_level: 4,
21
+ # numbered: true
22
+ # )
23
+ #
24
+ class TocGenerator
25
+ # Default options for TOC generation
26
+ DEFAULT_OPTIONS = {
27
+ min_level: 1,
28
+ max_level: 6,
29
+ numbered: false,
30
+ styled: false
31
+ }.freeze
32
+
33
+ class << self
34
+ # Generate a Toc from a CoreModel document
35
+ #
36
+ # @param document [CoreModel::StructuralElement] The document to process
37
+ # @param options [Hash] Generation options
38
+ # @option options [Integer] :min_level (1) Minimum heading level
39
+ # @option options [Integer] :max_level (6) Maximum heading level
40
+ # @option options [Boolean] :numbered (false) Add section numbers
41
+ # @option options [Boolean] :styled (false) Add styling
42
+ # @return [CoreModel::Toc, nil] The generated TOC or nil if no sections
43
+ def generate(document, options = {})
44
+ opts = DEFAULT_OPTIONS.merge(options)
45
+ entries = extract_toc_entries(document, opts)
46
+
47
+ return nil if entries.empty?
48
+
49
+ CoreModel::Toc.new(
50
+ entries: entries,
51
+ min_level: opts[:min_level],
52
+ max_level: opts[:max_level],
53
+ numbered: opts[:numbered],
54
+ styled: opts[:styled]
55
+ )
56
+ end
57
+
58
+ private
59
+
60
+ # Extract TOC entries from a document
61
+ def extract_toc_entries(document, options)
62
+ sections = find_sections(document, options[:min_level], options[:max_level])
63
+ return [] if sections.empty?
64
+
65
+ # Track section numbers
66
+ counters = {}
67
+
68
+ build_entries(sections, options, counters)
69
+ end
70
+
71
+ # Find all sections in the document within level range
72
+ def find_sections(element, min_level, max_level)
73
+ sections = []
74
+ return sections unless element.is_a?(CoreModel::StructuralElement)
75
+
76
+ Array(element.children).each do |child|
77
+ next unless child.is_a?(CoreModel::StructuralElement)
78
+
79
+ if child.section?
80
+ level = child.heading_level
81
+ sections << { element: child, level: level } if level >= min_level && level <= max_level
82
+
83
+ # Also search nested sections
84
+ else
85
+ # Search children for sections
86
+ end
87
+ sections.concat(find_sections(child, min_level, max_level))
88
+ end
89
+
90
+ sections
91
+ end
92
+
93
+ # Build hierarchical TOC entries from flat section list
94
+ def build_entries(sections, options, counters)
95
+ return [] if sections.empty?
96
+
97
+ entries = []
98
+ options[:min_level]
99
+
100
+ # Stack for tracking parent entries at each level
101
+ stack = { options[:min_level] - 1 => nil }
102
+
103
+ sections.each do |section_info|
104
+ element = section_info[:element]
105
+ level = section_info[:level]
106
+
107
+ # Update counters
108
+ counters[level] ||= 0
109
+ counters[level] += 1
110
+ # Reset deeper level counters
111
+ (level + 1..6).each { |l| counters[l] = 0 }
112
+
113
+ # Generate number if needed
114
+ number = (generate_number(counters, options[:min_level], level) if options[:numbered])
115
+
116
+ entry = CoreModel::TocEntry.new(
117
+ id: element.id,
118
+ title: element.title,
119
+ level: level,
120
+ number: number,
121
+ children: []
122
+ )
123
+
124
+ # Find parent at previous level
125
+ parent_level = level - 1
126
+ parent_level -= 1 while parent_level >= options[:min_level] - 1 && !stack.key?(parent_level)
127
+
128
+ if stack[parent_level]
129
+ stack[parent_level].children << entry
130
+ else
131
+ entries << entry
132
+ end
133
+
134
+ # Add to stack for potential children
135
+ stack[level] = entry
136
+ end
137
+
138
+ entries
139
+ end
140
+
141
+ def generate_number(counters, min_level, current_level)
142
+ parts = []
143
+ (min_level..current_level).each do |l|
144
+ parts << counters[l] if counters[l]&.positive?
145
+ end
146
+ parts.join('.')
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module CoreModel
5
+ # Verse block — a block for verse/poetry with optional attribution
6
+ class VerseBlock < Block
7
+ def self.semantic_type
8
+ :verse
9
+ end
10
+
11
+ attribute :attribution, :string
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ # CoreModel namespace for format-agnostic document models
5
+ #
6
+ # CoreModel provides the canonical document representation used as the hub
7
+ # in the hub-and-spoke architecture. Format-specific builders live in their
8
+ # respective format gems (e.g., Coridoc::AsciiDoc::Builder).
9
+ #
10
+ # @see CoreModel::Base for base functionality
11
+ module CoreModel
12
+ # Autoload submodules lazily using relative paths
13
+ autoload :Base, "#{__dir__}/core_model/base"
14
+ autoload :ChildrenContent, "#{__dir__}/core_model/children_content"
15
+ autoload :HasChildren, "#{__dir__}/core_model/has_children"
16
+ autoload :InlineContent, "#{__dir__}/core_model/inline_content"
17
+ autoload :Callout, "#{__dir__}/core_model/callout"
18
+ autoload :CalloutText, "#{__dir__}/core_model/callout_text"
19
+ autoload :Block, "#{__dir__}/core_model/block"
20
+ autoload :AnnotationBlock, "#{__dir__}/core_model/annotation_block"
21
+ autoload :ListBlock, "#{__dir__}/core_model/list_block"
22
+ autoload :ListItem, "#{__dir__}/core_model/list_item"
23
+ autoload :InlineElement, "#{__dir__}/core_model/inline_element"
24
+ autoload :BoldElement, "#{__dir__}/core_model/inline_element"
25
+ autoload :ItalicElement, "#{__dir__}/core_model/inline_element"
26
+ autoload :MonospaceElement, "#{__dir__}/core_model/inline_element"
27
+ autoload :UnderlineElement, "#{__dir__}/core_model/inline_element"
28
+ autoload :StrikethroughElement, "#{__dir__}/core_model/inline_element"
29
+ autoload :SubscriptElement, "#{__dir__}/core_model/inline_element"
30
+ autoload :SuperscriptElement, "#{__dir__}/core_model/inline_element"
31
+ autoload :HighlightElement, "#{__dir__}/core_model/inline_element"
32
+ autoload :LinkElement, "#{__dir__}/core_model/inline_element"
33
+ autoload :CrossReferenceElement, "#{__dir__}/core_model/inline_element"
34
+ autoload :StemElement, "#{__dir__}/core_model/inline_element"
35
+ autoload :FootnoteElement, "#{__dir__}/core_model/inline_element"
36
+ autoload :HardLineBreakElement, "#{__dir__}/core_model/inline_element"
37
+ autoload :TextElement, "#{__dir__}/core_model/inline_element"
38
+ autoload :SpanElement, "#{__dir__}/core_model/inline_element"
39
+ autoload :TermElement, "#{__dir__}/core_model/inline_element"
40
+ autoload :LineBreakElement, "#{__dir__}/core_model/inline_element"
41
+ autoload :RawInlineElement, "#{__dir__}/core_model/raw_inline_element"
42
+ autoload :StructuralElement, "#{__dir__}/core_model/structural_element"
43
+ autoload :DocumentElement, "#{__dir__}/core_model/structural_element"
44
+ autoload :SectionElement, "#{__dir__}/core_model/structural_element"
45
+ autoload :PreambleElement, "#{__dir__}/core_model/structural_element"
46
+ autoload :HeaderElement, "#{__dir__}/core_model/structural_element"
47
+
48
+ autoload :Table, "#{__dir__}/core_model/table"
49
+ autoload :TableCell, "#{__dir__}/core_model/table"
50
+ autoload :TableRow, "#{__dir__}/core_model/table"
51
+ autoload :TextContent, "#{__dir__}/core_model/text_content"
52
+ autoload :Image, "#{__dir__}/core_model/image"
53
+ autoload :Term, "#{__dir__}/core_model/term"
54
+ autoload :ElementAttribute, "#{__dir__}/core_model/element_attribute"
55
+ autoload :Metadata, "#{__dir__}/core_model/metadata"
56
+ autoload :MetadataEntry, "#{__dir__}/core_model/metadata"
57
+ autoload :FrontmatterBlock, "#{__dir__}/core_model/frontmatter"
58
+ autoload :Footnote, "#{__dir__}/core_model/footnote"
59
+ autoload :FootnoteReference, "#{__dir__}/core_model/footnote"
60
+ autoload :Abbreviation, "#{__dir__}/core_model/footnote"
61
+ autoload :DefinitionItem, "#{__dir__}/core_model/definition_item"
62
+ autoload :DefinitionList, "#{__dir__}/core_model/definition_list"
63
+ autoload :Toc, "#{__dir__}/core_model/toc"
64
+ autoload :TocEntry, "#{__dir__}/core_model/toc"
65
+ autoload :TocGenerator, "#{__dir__}/core_model/toc_generator"
66
+ autoload :Bibliography, "#{__dir__}/core_model/bibliography"
67
+ autoload :BibliographyEntry, "#{__dir__}/core_model/bibliography_entry"
68
+ autoload :SourceBlock, "#{__dir__}/core_model/source_block"
69
+ autoload :ExampleBlock, "#{__dir__}/core_model/example_block"
70
+ autoload :QuoteBlock, "#{__dir__}/core_model/quote_block"
71
+ autoload :SidebarBlock, "#{__dir__}/core_model/sidebar_block"
72
+ autoload :AbstractBlock, "#{__dir__}/core_model/abstract_block"
73
+ autoload :PartintroBlock, "#{__dir__}/core_model/partintro_block"
74
+ autoload :LiteralBlock, "#{__dir__}/core_model/literal_block"
75
+ autoload :PassBlock, "#{__dir__}/core_model/pass_block"
76
+ autoload :StemBlock, "#{__dir__}/core_model/stem_block"
77
+ autoload :ListingBlock, "#{__dir__}/core_model/listing_block"
78
+ autoload :OpenBlock, "#{__dir__}/core_model/open_block"
79
+ autoload :VerseBlock, "#{__dir__}/core_model/verse_block"
80
+ autoload :ReviewerBlock, "#{__dir__}/core_model/reviewer_block"
81
+ autoload :ParagraphBlock, "#{__dir__}/core_model/paragraph_block"
82
+ autoload :CommentBlock, "#{__dir__}/core_model/comment_block"
83
+ autoload :CommentLine, "#{__dir__}/core_model/comment_line"
84
+ autoload :HorizontalRuleBlock, "#{__dir__}/core_model/horizontal_rule_block"
85
+ autoload :IdGenerator, "#{__dir__}/core_model/id_generator"
86
+ autoload :AttributeReferenceResolver, "#{__dir__}/core_model/attribute_reference_resolver"
87
+ autoload :Include, "#{__dir__}/core_model/include"
88
+ autoload :IncludeOptions, "#{__dir__}/core_model/include_options"
89
+ autoload :IncludeLevelOffset, "#{__dir__}/core_model/include_level_offset"
90
+ autoload :OutputArtifact, "#{__dir__}/core_model/output_artifact"
91
+ end
92
+ end
@@ -0,0 +1,164 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lutaml/model'
4
+
5
+ # Coridoc - A hub-and-spoke document transformation library
6
+ #
7
+ # Coridoc provides a unified document model (CoreModel) and transformation
8
+ # infrastructure for converting between document formats such as AsciiDoc,
9
+ # HTML, and Markdown.
10
+ #
11
+ # ## Architecture
12
+ #
13
+ # Coridoc uses a hub-and-spoke architecture where CoreModel acts as the
14
+ # canonical document representation. Each format (AsciiDoc, HTML, Markdown)
15
+ # has its own model and transformers to/from CoreModel.
16
+ #
17
+ # ```
18
+ # Source Format → Source Model → CoreModel → Target Model → Target Format
19
+ # ```
20
+ #
21
+ # ## Quick Start
22
+ #
23
+ # @example Parsing documents
24
+ # require 'coridoc'
25
+ #
26
+ # # Parse AsciiDoc to CoreModel
27
+ # doc = Coridoc.parse("= Title\n\nContent", format: :asciidoc)
28
+ #
29
+ # @example Converting between formats
30
+ # # Convert AsciiDoc to HTML
31
+ # html = Coridoc.convert(adoc_text, from: :asciidoc, to: :html)
32
+ #
33
+ # # Convert Markdown to AsciiDoc
34
+ # adoc = Coridoc.convert(md_text, from: :markdown, to: :asciidoc)
35
+ #
36
+ # @example Using the hooks system
37
+ # Coridoc::Hooks.register(:before_parse) do |content, format:|
38
+ # puts "Parsing #{format} document..."
39
+ # content
40
+ # end
41
+ #
42
+ # @see Coridoc::CoreModel The canonical document model
43
+ # @see Coridoc::Hooks Plugin lifecycle hooks system
44
+ # @see Coridoc::FormatModule Interface contract for format modules
45
+ #
46
+ module Coridoc
47
+ # Base error class - defined in errors.rb
48
+ # @see Coridoc::Error Base error class
49
+ # @see Coridoc::ParseError Parsing errors with source context
50
+ # @see Coridoc::ValidationError Document validation errors
51
+ # @see Coridoc::TransformationError Model transformation errors
52
+ # @see Coridoc::UnsupportedFormatError Unsupported format errors
53
+
54
+ class << self
55
+ # ---- Format registry (delegates to FormatCatalog) ----
56
+
57
+ def registry = FormatCatalog.registry
58
+
59
+ def register_format(format_name, format_module, **)
60
+ FormatCatalog.register_format(format_name, format_module, **)
61
+ end
62
+
63
+ def get_format(format_name) = FormatCatalog.get_format(format_name)
64
+
65
+ def registered_formats = FormatCatalog.registered_formats
66
+
67
+ # ---- Pipeline (delegates to Pipeline) ----
68
+
69
+ def parse(text, format:) = Pipeline.parse(text, format: format)
70
+
71
+ def resolve_includes(document, **) = Pipeline.resolve_includes(document, **)
72
+
73
+ def resolve_references(document, **) = Pipeline.resolve_references(document, **)
74
+
75
+ def rewrite_links(...) = Pipeline.rewrite_links(...)
76
+
77
+ def convert(text, **) = Pipeline.convert(text, **)
78
+
79
+ def to_core(model) = Pipeline.to_core(model)
80
+
81
+ def serialize(model, **) = Pipeline.serialize(model, **)
82
+
83
+ def build(...) = Pipeline.build(...)
84
+
85
+ def parse_file(path, **) = Pipeline.parse_file(path, **)
86
+
87
+ def convert_file(path, **) = Pipeline.convert_file(path, **)
88
+
89
+ # ---- Format detection (delegates to FormatCatalog) ----
90
+
91
+ def detect_format(filename) = FormatCatalog.detect_format(filename)
92
+
93
+ def binary_format?(format) = FormatCatalog.binary_format?(format)
94
+
95
+ def normalize_format(name) = FormatCatalog.normalize_format(name)
96
+
97
+ def serialize_format?(format) = FormatCatalog.serialize_format?(format)
98
+
99
+ def parse_format?(format) = FormatCatalog.parse_format?(format)
100
+
101
+ def format_capabilities = FormatCatalog.capabilities
102
+
103
+ def resolve_output_format(output_file, **) = FormatCatalog.resolve_output_format(output_file, **)
104
+
105
+ # ---- Introspection (delegates to Introspection) ----
106
+
107
+ def file_info(path) = Introspection.file_info(path)
108
+
109
+ def validate_file(path, **) = Introspection.validate_file(path, **)
110
+
111
+ def document_stats(doc) = Introspection.document_stats(doc)
112
+
113
+ def describe_element(elem) = Introspection.describe_element(elem)
114
+
115
+ # ---- Utilities that stay on the top-level façade ----
116
+
117
+ # Strip unicode whitespace from a string.
118
+ #
119
+ # @param string [String] the string to strip
120
+ # @param only [Symbol, nil] what to strip: :begin, :end, or nil for both
121
+ # @return [String] the stripped string
122
+ def strip_unicode(string, only: nil)
123
+ return string if string.nil?
124
+
125
+ case only
126
+ when :begin
127
+ string.sub(/^\p{Zs}+/, '')
128
+ when :end
129
+ string.sub(/\p{Zs}+$/, '')
130
+ else
131
+ string.sub(/^\p{Zs}+/, '').sub(/\p{Zs}+$/, '')
132
+ end
133
+ end
134
+ end
135
+
136
+ autoload :Error, "#{__dir__}/errors"
137
+ autoload :UnsupportedFormatError, "#{__dir__}/errors"
138
+ autoload :Version, "#{__dir__}/version"
139
+ autoload :Logger, "#{__dir__}/logger"
140
+ autoload :Hooks, "#{__dir__}/hooks"
141
+ autoload :Query, "#{__dir__}/query"
142
+ autoload :Validation, "#{__dir__}/validation"
143
+ autoload :Configurable, "#{__dir__}/configurable"
144
+ autoload :FormatModule, "#{__dir__}/format_module"
145
+ autoload :CoreModel, "#{__dir__}/core_model"
146
+ autoload :Registry, "#{__dir__}/registry"
147
+ autoload :Visitor, "#{__dir__}/visitor"
148
+ autoload :PerformanceRegression, "#{__dir__}/performance_regression"
149
+ autoload :IncludeResolver, "#{__dir__}/include_resolver"
150
+ autoload :IncludeSelectors, "#{__dir__}/include_selectors"
151
+ autoload :ResolveIncludes, "#{__dir__}/resolve_includes"
152
+ autoload :SourceGraph, "#{__dir__}/source_graph"
153
+ autoload :Lint, "#{__dir__}/lint"
154
+ autoload :Pipeline, "#{__dir__}/pipeline"
155
+ autoload :FormatCatalog, "#{__dir__}/format_catalog"
156
+ autoload :Introspection, "#{__dir__}/introspection"
157
+ autoload :Dispatch, "#{__dir__}/dispatch"
158
+ autoload :Reference, "#{__dir__}/reference"
159
+ end
160
+
161
+ # Format gems self-register via Coridoc.register_format when they are required.
162
+ # No hardcoded registration needed here — each gem's entry file handles its own
163
+ # registration (e.g., coridoc-adoc/lib/coridoc/asciidoc.rb calls
164
+ # Coridoc.register_format(:asciidoc, Coridoc::AsciiDoc)).
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ # Type-keyed handler dispatch.
5
+ #
6
+ # Replaces the bespoke `register/lookup` hashes that recur across gems with
7
+ # one cohesive registry. Each gem that needs type-keyed dispatch configures
8
+ # a single Dispatch instance and exposes its legacy DSL as thin delegates.
9
+ #
10
+ # Two resolution policies cover the common shapes:
11
+ #
12
+ # - `Coridoc::Dispatch.strict` — exact key match, raises on miss
13
+ # - `Coridoc::Dispatch.hierarchical` — walks ancestors on miss, returns nil
14
+ #
15
+ # Registries that need priority ordering, predicate matching, or lazy
16
+ # loading do not fit this shape and stay as they are; the friction there is
17
+ # genuine semantic difference, not duplicated mechanism.
18
+ class Dispatch
19
+ TERMINAL_ANCESTORS = [Object, BasicObject].freeze
20
+ private_constant :TERMINAL_ANCESTORS
21
+
22
+ class << self
23
+ # Exact key match; raises if no entry. Used by registries that map a
24
+ # concrete type to its sole handler (e.g. AsciiDoc ElementRegistry).
25
+ def strict = new(walk_ancestors: false)
26
+
27
+ # Walks the key's class ancestors on miss; returns nil if no entry.
28
+ # Used by registries that want base-class handlers to apply to all
29
+ # subclasses (e.g. Mirror HandlerRegistry).
30
+ def hierarchical = new(walk_ancestors: true, miss: :return_nil)
31
+ end
32
+
33
+ def initialize(walk_ancestors: false, miss: :raise, &default)
34
+ @walk_ancestors = walk_ancestors
35
+ @miss = miss
36
+ @default = default
37
+ @entries = {}
38
+ end
39
+
40
+ def register(key, handler)
41
+ @entries[key] = handler
42
+ end
43
+
44
+ # Replace the handler for an existing key. Returns the previous handler
45
+ # so wrappers can chain: original = dispatch.override(K, Wrapper.new(original))
46
+ def override(key, handler)
47
+ previous = @entries[key]
48
+ @entries[key] = handler
49
+ previous
50
+ end
51
+
52
+ def unregister(key)
53
+ @entries.delete(key)
54
+ end
55
+
56
+ # Resolve the handler for a key. Returns nil if no handler matches
57
+ # unless the dispatch is configured to raise.
58
+ def lookup(key)
59
+ exact = @entries[key]
60
+ return exact if exact
61
+ return walk(key) if @walk_ancestors && key.is_a?(Class)
62
+
63
+ apply_default(key)
64
+ end
65
+
66
+ # Resolve the handler, raising Coridoc::Error on miss.
67
+ def lookup!(key)
68
+ lookup(key) || raise(Coridoc::Error, "no handler registered for #{key.inspect}")
69
+ end
70
+
71
+ def registered?(key) = @entries.key?(key)
72
+
73
+ def registered_keys = @entries.keys
74
+
75
+ def clear! = @entries.clear
76
+
77
+ private
78
+
79
+ def walk(klass)
80
+ klass.ancestors.each do |ancestor|
81
+ next if ancestor == klass
82
+ break if TERMINAL_ANCESTORS.include?(ancestor)
83
+
84
+ entry = @entries[ancestor]
85
+ return entry if entry
86
+ end
87
+ apply_default(klass)
88
+ end
89
+
90
+ def apply_default(key)
91
+ return @default.call(key) if @default
92
+
93
+ nil
94
+ end
95
+ end
96
+ end