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
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
# Base error class for all Coridoc errors
|
|
5
|
+
class Error < StandardError; end
|
|
6
|
+
|
|
7
|
+
# Suggestion patterns for common parsing errors
|
|
8
|
+
#
|
|
9
|
+
# These patterns are matched against error messages and source content
|
|
10
|
+
# to provide helpful suggestions for fixing common issues.
|
|
11
|
+
ERROR_SUGGESTIONS = [
|
|
12
|
+
{
|
|
13
|
+
pattern: /unterminated.*string|unexpected.*end.*of.*input|expected.*["']/i,
|
|
14
|
+
suggestion: 'Check for unclosed quotes or strings',
|
|
15
|
+
examples: ["'text'", '"text"']
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
pattern: /unexpected.*indentation|indentation.*error|inconsistent.*indent/i,
|
|
19
|
+
suggestion: 'Check indentation - use consistent spaces or tabs',
|
|
20
|
+
examples: [' indented line', ' nested item']
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
pattern: /missing.*separator|expected.*delimiter|missing.*comma/i,
|
|
24
|
+
suggestion: 'Add missing separator between elements',
|
|
25
|
+
examples: ['item1, item2', 'key: value']
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
pattern: /invalid.*attribute|unknown.*attribute|attribute.*not.*allowed/i,
|
|
29
|
+
suggestion: 'Check attribute spelling and allowed values',
|
|
30
|
+
examples: ['[role=example]', '[source,ruby]']
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
pattern: /invalid.*heading|heading.*level|expected.*heading/i,
|
|
34
|
+
suggestion: 'Use valid heading syntax with = or # markers',
|
|
35
|
+
examples: ['= Level 1', '== Level 2', '### Level 3']
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
pattern: /invalid.*list|list.*marker|expected.*list.*item/i,
|
|
39
|
+
suggestion: 'Use correct list markers (*, -, ., or numbered)',
|
|
40
|
+
examples: ['* bullet', '. ordered', 'term:: definition']
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
pattern: /invalid.*link|malformed.*url|link.*syntax/i,
|
|
44
|
+
suggestion: 'Use correct link syntax: text[url] or link:url[]',
|
|
45
|
+
examples: ['Google[https://google.com]', 'link:file.adoc[]']
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
pattern: /invalid.*table|table.*delimiter|expected.*separator/i,
|
|
49
|
+
suggestion: 'Check table syntax with | delimiters',
|
|
50
|
+
examples: ["|===\n| Cell 1 | Cell 2\n|==="]
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
pattern: /invalid.*block|block.*delimiter|unterminated.*block/i,
|
|
54
|
+
suggestion: 'Ensure block delimiters match (----, ****, ====, etc.)',
|
|
55
|
+
examples: ["----\ncode\n----", "====\nexample\n===="]
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
pattern: /invalid.*macro|unknown.*macro|macro.*syntax/i,
|
|
59
|
+
suggestion: 'Check macro syntax: name:target[attributes]',
|
|
60
|
+
examples: ['include::file.adoc[]', 'image::image.png[]']
|
|
61
|
+
}
|
|
62
|
+
].freeze
|
|
63
|
+
|
|
64
|
+
# Enhanced error classes with source context support
|
|
65
|
+
#
|
|
66
|
+
# These error classes provide additional context such as line numbers,
|
|
67
|
+
# column positions, source snippets, and suggestions to help users debug issues.
|
|
68
|
+
#
|
|
69
|
+
# @example Raising a parse error with context
|
|
70
|
+
# raise ParseError.new(
|
|
71
|
+
# "Unexpected token",
|
|
72
|
+
# source: content,
|
|
73
|
+
# line: 10,
|
|
74
|
+
# column: 5
|
|
75
|
+
# )
|
|
76
|
+
#
|
|
77
|
+
# @example Handling errors with context
|
|
78
|
+
# begin
|
|
79
|
+
# Coridoc.parse(text, format: :markdown)
|
|
80
|
+
# rescue Coridoc::ParseError => e
|
|
81
|
+
# puts e.message_with_context
|
|
82
|
+
# puts e.suggestion if e.suggestion
|
|
83
|
+
# end
|
|
84
|
+
#
|
|
85
|
+
class ParseError < Error
|
|
86
|
+
attr_reader :source, :line, :column, :snippet_lines, :suggestion
|
|
87
|
+
|
|
88
|
+
# Create a new parse error with optional source context
|
|
89
|
+
#
|
|
90
|
+
# @param message [String] The error message
|
|
91
|
+
# @param source [String, nil] The source text being parsed
|
|
92
|
+
# @param line [Integer, nil] The line number (1-indexed)
|
|
93
|
+
# @param column [Integer, nil] The column number (1-indexed)
|
|
94
|
+
# @param snippet_lines [Integer] Number of context lines to show (default: 3)
|
|
95
|
+
# @param suggestion [String, nil] Optional suggestion for fixing the error
|
|
96
|
+
def initialize(message, source: nil, line: nil, column: nil, snippet_lines: 3,
|
|
97
|
+
suggestion: nil)
|
|
98
|
+
@source = source
|
|
99
|
+
@line = line
|
|
100
|
+
@column = column
|
|
101
|
+
@snippet_lines = snippet_lines
|
|
102
|
+
@suggestion = suggestion || find_suggestion(message, source, line)
|
|
103
|
+
super(build_message(message))
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Returns the error message with full context
|
|
107
|
+
#
|
|
108
|
+
# @return [String] Formatted error message with source snippet
|
|
109
|
+
def message_with_context
|
|
110
|
+
return message unless source && line
|
|
111
|
+
|
|
112
|
+
msg = message
|
|
113
|
+
msg += "\n\n"
|
|
114
|
+
msg += source_snippet
|
|
115
|
+
if suggestion
|
|
116
|
+
msg += "\n\n"
|
|
117
|
+
msg += "Suggestion: #{suggestion}"
|
|
118
|
+
end
|
|
119
|
+
msg
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Returns the source snippet around the error location
|
|
123
|
+
#
|
|
124
|
+
# @return [String] Formatted source snippet with line numbers
|
|
125
|
+
def source_snippet
|
|
126
|
+
return '' unless source && line
|
|
127
|
+
|
|
128
|
+
lines = source.lines
|
|
129
|
+
start_line = [1, line - snippet_lines].max
|
|
130
|
+
end_line = [lines.length, line + snippet_lines].min
|
|
131
|
+
|
|
132
|
+
snippet = []
|
|
133
|
+
(start_line..end_line).each do |i|
|
|
134
|
+
prefix = i == line ? '>>> ' : ' '
|
|
135
|
+
snippet_line = lines[i - 1]&.chomp || ''
|
|
136
|
+
snippet << "#{prefix}#{i.to_s.rjust(4)}: #{snippet_line}"
|
|
137
|
+
|
|
138
|
+
# Add column indicator on the error line
|
|
139
|
+
if i == line && column
|
|
140
|
+
indicator = "#{' ' * (prefix.length + 6 + column - 1)}^"
|
|
141
|
+
snippet << indicator
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
snippet.join("\n")
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Returns all suggestions that match this error
|
|
149
|
+
#
|
|
150
|
+
# @return [Array<String>] List of applicable suggestions
|
|
151
|
+
def all_suggestions
|
|
152
|
+
return [] unless message || source
|
|
153
|
+
|
|
154
|
+
suggestions = []
|
|
155
|
+
ERROR_SUGGESTIONS.each do |entry|
|
|
156
|
+
suggestions << format_suggestion(entry) if entry[:pattern].match?(message)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# Also check source line if available
|
|
160
|
+
if source && line
|
|
161
|
+
source_line = source.lines[line - 1]
|
|
162
|
+
if source_line
|
|
163
|
+
ERROR_SUGGESTIONS.each do |entry|
|
|
164
|
+
suggestions << format_suggestion(entry) if entry[:pattern].match?(source_line)
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
suggestions.uniq
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
private
|
|
173
|
+
|
|
174
|
+
def build_message(message)
|
|
175
|
+
context = []
|
|
176
|
+
context << "line #{line}" if line
|
|
177
|
+
context << "column #{column}" if column
|
|
178
|
+
|
|
179
|
+
if context.any?
|
|
180
|
+
"#{message} (at #{context.join(', ')})"
|
|
181
|
+
else
|
|
182
|
+
message
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def find_suggestion(message, source, line)
|
|
187
|
+
return nil unless message || source
|
|
188
|
+
|
|
189
|
+
# Check message against patterns
|
|
190
|
+
ERROR_SUGGESTIONS.each do |entry|
|
|
191
|
+
return format_suggestion(entry) if entry[:pattern].match?(message)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# Check source line if available
|
|
195
|
+
if source && line
|
|
196
|
+
source_line = source.lines[line - 1]
|
|
197
|
+
if source_line
|
|
198
|
+
ERROR_SUGGESTIONS.each do |entry|
|
|
199
|
+
return format_suggestion(entry) if entry[:pattern].match?(source_line)
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
nil
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def format_suggestion(entry)
|
|
208
|
+
result = entry[:suggestion]
|
|
209
|
+
result += " (e.g., #{entry[:examples].first(2).join(', ')})" if entry[:examples]&.any?
|
|
210
|
+
result
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# Error raised when validation fails
|
|
215
|
+
#
|
|
216
|
+
# @example
|
|
217
|
+
# raise ValidationError.new(
|
|
218
|
+
# "Invalid document structure",
|
|
219
|
+
# errors: ["Missing title", "Empty section"]
|
|
220
|
+
# )
|
|
221
|
+
#
|
|
222
|
+
class ValidationError < Error
|
|
223
|
+
attr_reader :errors
|
|
224
|
+
|
|
225
|
+
# Create a new validation error
|
|
226
|
+
#
|
|
227
|
+
# @param message [String] The error message
|
|
228
|
+
# @param errors [Array<String>] List of specific validation errors
|
|
229
|
+
def initialize(message, errors: [])
|
|
230
|
+
@errors = errors
|
|
231
|
+
super(build_message(message))
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
private
|
|
235
|
+
|
|
236
|
+
def build_message(message)
|
|
237
|
+
return message if errors.empty?
|
|
238
|
+
|
|
239
|
+
"#{message}\n - #{errors.join("\n - ")}"
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
# Error raised when transformation fails
|
|
244
|
+
#
|
|
245
|
+
# @example
|
|
246
|
+
# raise TransformationError.new(
|
|
247
|
+
# "Cannot convert element",
|
|
248
|
+
# source_type: "Paragraph",
|
|
249
|
+
# target_type: "CoreModel::Block"
|
|
250
|
+
# )
|
|
251
|
+
#
|
|
252
|
+
class TransformationError < Error
|
|
253
|
+
attr_reader :source_type, :target_type
|
|
254
|
+
|
|
255
|
+
# Create a new transformation error
|
|
256
|
+
#
|
|
257
|
+
# @param message [String] The error message
|
|
258
|
+
# @param source_type [String, Class, nil] The source type being transformed
|
|
259
|
+
# @param target_type [String, Class, nil] The target type
|
|
260
|
+
def initialize(message, source_type: nil, target_type: nil)
|
|
261
|
+
@source_type = source_type
|
|
262
|
+
@target_type = target_type
|
|
263
|
+
super(build_message(message))
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
private
|
|
267
|
+
|
|
268
|
+
def build_message(message)
|
|
269
|
+
parts = [message]
|
|
270
|
+
parts << "source: #{source_type}" if source_type
|
|
271
|
+
parts << "target: #{target_type}" if target_type
|
|
272
|
+
parts.join(' (') + (parts.length > 1 ? ')' : '')
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
# Error raised when a file is not found
|
|
277
|
+
class FileNotFoundError < Error
|
|
278
|
+
attr_reader :path
|
|
279
|
+
|
|
280
|
+
def initialize(path)
|
|
281
|
+
@path = path
|
|
282
|
+
super("File not found: #{path}")
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
# Error raised when an include directive's target cannot be located.
|
|
287
|
+
# Honors the +missing_include+ policy: +:error+ raises this; +:warn+,
|
|
288
|
+
# +:silent+, and +:passthrough+ swallow it.
|
|
289
|
+
class IncludeNotFoundError < Error
|
|
290
|
+
attr_reader :target
|
|
291
|
+
|
|
292
|
+
def initialize(target)
|
|
293
|
+
@target = target
|
|
294
|
+
super("Include target not found: #{target}")
|
|
295
|
+
end
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# Raised when a document still contains unresolved +include::+ edges
|
|
299
|
+
# at serialization time and the target format cannot represent them
|
|
300
|
+
# (it would silently drop the content). Resolve the graph first with
|
|
301
|
+
# +Coridoc.resolve_includes+, or opt out with
|
|
302
|
+
# +allow_unresolved_includes: true+.
|
|
303
|
+
class UnresolvedIncludesError < Error
|
|
304
|
+
attr_reader :targets
|
|
305
|
+
|
|
306
|
+
def initialize(targets)
|
|
307
|
+
@targets = targets
|
|
308
|
+
super(
|
|
309
|
+
"Document has #{targets.size} unresolved include directive(s): " \
|
|
310
|
+
"#{targets.join(', ')}. Resolve them before serializing with " \
|
|
311
|
+
'Coridoc.resolve_includes(doc, base_dir: ...), or pass ' \
|
|
312
|
+
'allow_unresolved_includes: true.'
|
|
313
|
+
)
|
|
314
|
+
end
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
# Error raised when an include chain exceeds the configured depth limit.
|
|
318
|
+
class IncludeDepthExceededError < Error
|
|
319
|
+
attr_reader :depth, :target
|
|
320
|
+
|
|
321
|
+
def initialize(target:, depth:, max:)
|
|
322
|
+
@target = target
|
|
323
|
+
@depth = depth
|
|
324
|
+
super("Include depth #{depth} exceeds max #{max} at #{target}")
|
|
325
|
+
end
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
# Error raised when a cycle is detected in the include graph.
|
|
329
|
+
# The chain is the list of files leading back to the repeated target.
|
|
330
|
+
class CircularIncludeError < Error
|
|
331
|
+
attr_reader :chain, :target
|
|
332
|
+
|
|
333
|
+
def initialize(target:, chain:)
|
|
334
|
+
@target = target
|
|
335
|
+
@chain = chain
|
|
336
|
+
super("Circular include detected: #{chain.join(' -> ')} -> #{target}")
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
# Error raised when an include target escapes the resolver's safe base
|
|
341
|
+
# directory and +allow_unsafe_includes+ is not set.
|
|
342
|
+
class UnsafeIncludeError < Error
|
|
343
|
+
attr_reader :target
|
|
344
|
+
|
|
345
|
+
def initialize(target)
|
|
346
|
+
@target = target
|
|
347
|
+
super("Unsafe include path blocked: #{target}")
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
# Error raised when an include target exceeds the resolver's size limit.
|
|
352
|
+
class IncludeTooLargeError < Error
|
|
353
|
+
attr_reader :target
|
|
354
|
+
|
|
355
|
+
def initialize(target)
|
|
356
|
+
@target = target
|
|
357
|
+
super("Include target too large to read: #{target}")
|
|
358
|
+
end
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
# Error raised when a requested format is not supported
|
|
362
|
+
#
|
|
363
|
+
# @example
|
|
364
|
+
# raise UnsupportedFormatError.new(:docx, available: [:html, :markdown])
|
|
365
|
+
#
|
|
366
|
+
class UnsupportedFormatError < Error
|
|
367
|
+
attr_reader :requested_format, :available_formats
|
|
368
|
+
|
|
369
|
+
# Create a new unsupported format error
|
|
370
|
+
#
|
|
371
|
+
# @param format [Symbol, String] The requested format
|
|
372
|
+
# @param available [Array<Symbol>] List of available formats
|
|
373
|
+
def initialize(format, available: [])
|
|
374
|
+
@requested_format = format
|
|
375
|
+
@available_formats = available
|
|
376
|
+
super(build_message)
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
private
|
|
380
|
+
|
|
381
|
+
def build_message
|
|
382
|
+
msg = "Format '#{requested_format}' is not registered"
|
|
383
|
+
msg += ". Available formats: #{available_formats.join(', ')}" if available_formats.any?
|
|
384
|
+
msg
|
|
385
|
+
end
|
|
386
|
+
end
|
|
387
|
+
end
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
# Format registry, detection, and capability introspection. Single
|
|
5
|
+
# source of truth for "what formats exist and what can they do?",
|
|
6
|
+
# extracted from the top-level Coridoc façade. Public API on
|
|
7
|
+
# +Coridoc+ delegates here.
|
|
8
|
+
module FormatCatalog
|
|
9
|
+
class << self
|
|
10
|
+
def registry
|
|
11
|
+
@registry ||= Registry.new
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def register_format(format_name, format_module, **options)
|
|
15
|
+
format_module.extend(FormatModule::Interface) unless format_module.is_a?(FormatModule::Interface)
|
|
16
|
+
registry.register(format_name, format_module, options)
|
|
17
|
+
FormatModule.validate!(format_module, format_name)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def get_format(format_name)
|
|
21
|
+
registry.get(format_name) || lazy_load_format(format_name)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# If a format gem (e.g. coridoc-adoc) is in the bundle but has not
|
|
25
|
+
# been required yet, attempt to require it on first lookup. This
|
|
26
|
+
# removes the friction where `Coridoc.parse(text, format: :asciidoc)`
|
|
27
|
+
# blows up just because the user only wrote `require "coridoc"` and
|
|
28
|
+
# never explicitly required `coridoc/asciidoc`. The require is for
|
|
29
|
+
# an external gem, not internal library code, so it stays within
|
|
30
|
+
# the autoload-over-require_relative rule.
|
|
31
|
+
def lazy_load_format(format_name)
|
|
32
|
+
return nil if format_name.nil? || format_name.to_s.empty?
|
|
33
|
+
|
|
34
|
+
begin
|
|
35
|
+
require "coridoc/#{format_name}"
|
|
36
|
+
rescue LoadError
|
|
37
|
+
return nil
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
registry.get(format_name)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def registered_formats
|
|
44
|
+
registry.list
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def detect_format(filename)
|
|
48
|
+
ext = File.extname(filename).downcase
|
|
49
|
+
registry.each_key do |name|
|
|
50
|
+
opts = registry.options_for(name)
|
|
51
|
+
return name if opts[:extensions]&.include?(ext)
|
|
52
|
+
end
|
|
53
|
+
nil
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def binary_format?(format)
|
|
57
|
+
opts = registry.options_for(format)
|
|
58
|
+
opts&.fetch(:binary, false) == true
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def normalize_format(name)
|
|
62
|
+
return nil unless name
|
|
63
|
+
|
|
64
|
+
key = name.to_s.downcase
|
|
65
|
+
registry.each_key do |fmt_name|
|
|
66
|
+
opts = registry.options_for(fmt_name)
|
|
67
|
+
return fmt_name if opts[:aliases]&.include?(key)
|
|
68
|
+
end
|
|
69
|
+
key.to_sym
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def serialize_format?(format)
|
|
73
|
+
mod = get_format(format)
|
|
74
|
+
return false unless mod
|
|
75
|
+
|
|
76
|
+
mod.serialize?
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def parse_format?(format)
|
|
80
|
+
mod = get_format(format)
|
|
81
|
+
return false unless mod
|
|
82
|
+
|
|
83
|
+
mod.public_methods.include?(:parse_to_core) || mod.public_methods.include?(:parse)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def capabilities
|
|
87
|
+
registered_formats.each_with_object({}) do |name, caps|
|
|
88
|
+
caps[name] = {
|
|
89
|
+
parse: parse_format?(name),
|
|
90
|
+
serialize: serialize_format?(name)
|
|
91
|
+
}
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def resolve_output_format(output_file, default: :html)
|
|
96
|
+
return default unless output_file
|
|
97
|
+
|
|
98
|
+
detect_format(output_file) || default
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
# Declared interface contract for format modules.
|
|
5
|
+
#
|
|
6
|
+
# Format modules registered via Coridoc.register_format must implement:
|
|
7
|
+
#
|
|
8
|
+
# Required:
|
|
9
|
+
# - parse_to_core(input, options={}) → CoreModel::Base
|
|
10
|
+
# - serialize(model, **options) → String
|
|
11
|
+
#
|
|
12
|
+
# Optional:
|
|
13
|
+
# - parse(input, options={}) → format-specific model
|
|
14
|
+
# - handles_model?(model) → Boolean
|
|
15
|
+
# - to_core(model) → CoreModel::Base
|
|
16
|
+
# - serialize? → Boolean
|
|
17
|
+
#
|
|
18
|
+
module FormatModule
|
|
19
|
+
MINIMUM_PARSE_METHODS = %i[parse_to_core parse].freeze
|
|
20
|
+
REQUIRED_METHODS = %i[serialize].freeze
|
|
21
|
+
|
|
22
|
+
# Default implementations for optional format module methods.
|
|
23
|
+
# Format modules are auto-extended with this during registration.
|
|
24
|
+
module Interface
|
|
25
|
+
def handles_model?(_model)
|
|
26
|
+
false
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def serialize?
|
|
30
|
+
true
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Canonical formatter (#110): parse the format model tree and
|
|
34
|
+
# serialize canonically. Formats without a formatter keep the
|
|
35
|
+
# default nil and callers report "no formatter" instead of
|
|
36
|
+
# silently passing text through.
|
|
37
|
+
def format(_text)
|
|
38
|
+
nil
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Whether the serializer can represent unresolved include edges
|
|
42
|
+
# (graph-mode CoreModel::Include nodes) without losing them.
|
|
43
|
+
# Formats that round-trip the directive natively (asciidoc,
|
|
44
|
+
# mirror) or preserve it explicitly (markdown comments) override
|
|
45
|
+
# this to true; formats that would silently drop the content keep
|
|
46
|
+
# the default false, and the pipeline raises instead of losing it.
|
|
47
|
+
def preserves_unresolved_includes?
|
|
48
|
+
false
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Parse a file to CoreModel, handling file-specific concerns like include resolution.
|
|
52
|
+
# Format modules that support include directives or file-relative references
|
|
53
|
+
# should override this method.
|
|
54
|
+
#
|
|
55
|
+
# @param path [String] path to the source file
|
|
56
|
+
# @param content [String, nil] pre-read file content (hooks may have modified it)
|
|
57
|
+
# @return [Coridoc::CoreModel::Base] CoreModel document
|
|
58
|
+
def parse_file_to_core(path, content = nil)
|
|
59
|
+
parse_to_core(content || File.read(path))
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Validate that a format module implements the minimum interface.
|
|
64
|
+
# Warns to $stderr if methods are missing. Returns true if valid.
|
|
65
|
+
def self.validate!(format_module, format_name)
|
|
66
|
+
has_parse = MINIMUM_PARSE_METHODS.any? { |m| format_module.public_methods.include?(m) }
|
|
67
|
+
has_serialize = REQUIRED_METHODS.all? { |m| format_module.public_methods.include?(m) }
|
|
68
|
+
|
|
69
|
+
return true if has_parse && has_serialize
|
|
70
|
+
|
|
71
|
+
missing = []
|
|
72
|
+
missing << 'parse_to_core or parse' unless has_parse
|
|
73
|
+
missing << 'serialize' unless has_serialize
|
|
74
|
+
warn "Coridoc: format :#{format_name} (#{format_module}) missing: #{missing.join(', ')}"
|
|
75
|
+
false
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|