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,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Introspection
|
|
5
|
+
# Visitor that walks a document and counts each CoreModel node by
|
|
6
|
+
# its type key. Used by Introspection.count_element_types to back
|
|
7
|
+
# the +Coridoc.document_stats+ API.
|
|
8
|
+
#
|
|
9
|
+
# Typed StructuralElement / Block nodes are counted under their
|
|
10
|
+
# +element_type+ (semantic identity). Other nodes fall back to a
|
|
11
|
+
# snake_case rendering of their class name.
|
|
12
|
+
class ElementCounter < Visitor::Base
|
|
13
|
+
def initialize
|
|
14
|
+
@counts = Hash.new(0)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
attr_reader :counts
|
|
18
|
+
|
|
19
|
+
def visit(element)
|
|
20
|
+
return super unless element.is_a?(CoreModel::Base)
|
|
21
|
+
|
|
22
|
+
@counts[type_key_for(element)] += 1
|
|
23
|
+
super
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def type_key_for(element)
|
|
29
|
+
if typed_node?(element) && element.element_type
|
|
30
|
+
element.element_type
|
|
31
|
+
else
|
|
32
|
+
snake_case(element.class.name)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def typed_node?(element)
|
|
37
|
+
element.is_a?(CoreModel::StructuralElement) || element.is_a?(CoreModel::Block)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def snake_case(class_name)
|
|
41
|
+
class_name.split('::').last
|
|
42
|
+
.gsub(/([A-Z])/, '_\1')
|
|
43
|
+
.downcase
|
|
44
|
+
.sub(/^_/, '')
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
# Document introspection: file metadata, validation, stats. Extracted
|
|
5
|
+
# from the top-level Coridoc façade so the document-counting visitor
|
|
6
|
+
# and file-metadata helpers have their own home. Public API on
|
|
7
|
+
# +Coridoc+ delegates here.
|
|
8
|
+
module Introspection
|
|
9
|
+
autoload :ElementCounter, "#{__dir__}/introspection/element_counter"
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
def file_info(path)
|
|
13
|
+
fmt = FormatCatalog.detect_format(path)
|
|
14
|
+
info = { size: File.size(path), format: fmt }
|
|
15
|
+
info[:lines] = File.foreach(path).count unless FormatCatalog.binary_format?(fmt)
|
|
16
|
+
info
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def validate_file(path, format: nil)
|
|
20
|
+
doc = Pipeline.parse_file(path, format: format)
|
|
21
|
+
|
|
22
|
+
schema = Validation::SchemaGenerator.generate(doc.class)
|
|
23
|
+
return schema.validate(doc) if schema
|
|
24
|
+
|
|
25
|
+
Validation::Result.new
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def document_stats(doc)
|
|
29
|
+
stats = {}
|
|
30
|
+
stats[:title] = doc.title if doc.title
|
|
31
|
+
|
|
32
|
+
if doc.is_a?(CoreModel::StructuralElement)
|
|
33
|
+
stats[:child_count] = count_elements(doc)
|
|
34
|
+
stats[:element_counts] = count_element_types(doc)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
stats
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def describe_element(elem)
|
|
41
|
+
return elem.to_s unless elem.is_a?(CoreModel::Base)
|
|
42
|
+
|
|
43
|
+
type = elem.class.name.split('::').last
|
|
44
|
+
if elem.title
|
|
45
|
+
"#{type}: #{elem.title}"
|
|
46
|
+
elsif elem.is_a?(CoreModel::Block) && elem.content
|
|
47
|
+
preview = elem.content.to_s[0..50]
|
|
48
|
+
preview += '...' if elem.content.to_s.length > 50
|
|
49
|
+
"#{type}: #{preview}"
|
|
50
|
+
else
|
|
51
|
+
type
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def count_elements(doc)
|
|
58
|
+
return 0 unless doc.is_a?(CoreModel::StructuralElement)
|
|
59
|
+
|
|
60
|
+
doc.children.sum do |child|
|
|
61
|
+
1 + (child.is_a?(CoreModel::StructuralElement) ? count_elements(child) : 0)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def count_element_types(doc)
|
|
66
|
+
counter = ElementCounter.new
|
|
67
|
+
counter.visit(doc)
|
|
68
|
+
counter.counts.reject { |_, v| v.zero? }
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module LinkRewriter
|
|
5
|
+
# Default no-op rewriter. Returns every target unchanged. Used when
|
|
6
|
+
# the caller only wants the visitor's immutable-copy guarantee.
|
|
7
|
+
class Identity
|
|
8
|
+
def call(target:, **)
|
|
9
|
+
target
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module LinkRewriter
|
|
5
|
+
# Focused rewriting visitor for the CoreModel tree.
|
|
6
|
+
#
|
|
7
|
+
# The set of node types that carry link/xref targets is closed and
|
|
8
|
+
# small: +LinkElement+, +CrossReferenceElement+, plus generic
|
|
9
|
+
# +InlineElement+ instances whose +format_type+ is +'link'+ or
|
|
10
|
+
# +'xref'+. That classification is owned polymorphically by
|
|
11
|
+
# +InlineElement#link_kind+ — the visitor just calls it. Adding a
|
|
12
|
+
# new link-bearing subclass means overriding +link_kind+ on it,
|
|
13
|
+
# not editing a case/when here (OCP).
|
|
14
|
+
#
|
|
15
|
+
# Verbatim block recognition is delegated to +CoreModel::Base#prose?+
|
|
16
|
+
# — each block class answers "is my text live or literal?". Adding a
|
|
17
|
+
# new verbatim block type means overriding +prose?+ on it, not editing
|
|
18
|
+
# a list here. The visitor is closed for that change.
|
|
19
|
+
#
|
|
20
|
+
# Dispatch is class-based (no +respond_to?+ duck-typing). Unrecognized
|
|
21
|
+
# classes are returned unchanged — the visitor is closed by design.
|
|
22
|
+
class Visitor
|
|
23
|
+
# Structural/container classes that own a child collection. Each
|
|
24
|
+
# entry maps the class to the reader method that exposes its
|
|
25
|
+
# children. MECE — every "recurse into the children" case lands
|
|
26
|
+
# in this table. Exposed as a public constant so spec helpers
|
|
27
|
+
# and other visitors can mirror the same paths without restating
|
|
28
|
+
# the dispatch (DRY).
|
|
29
|
+
CONTAINER_TYPES = {
|
|
30
|
+
Coridoc::CoreModel::DocumentElement => :children,
|
|
31
|
+
Coridoc::CoreModel::SectionElement => :children,
|
|
32
|
+
Coridoc::CoreModel::PreambleElement => :children,
|
|
33
|
+
Coridoc::CoreModel::HeaderElement => :children,
|
|
34
|
+
Coridoc::CoreModel::Block => :children,
|
|
35
|
+
Coridoc::CoreModel::ListBlock => :items,
|
|
36
|
+
Coridoc::CoreModel::ListItem => :children,
|
|
37
|
+
Coridoc::CoreModel::Table => :rows,
|
|
38
|
+
Coridoc::CoreModel::TableRow => :cells,
|
|
39
|
+
Coridoc::CoreModel::TableCell => :children,
|
|
40
|
+
Coridoc::CoreModel::DefinitionList => :items,
|
|
41
|
+
Coridoc::CoreModel::Toc => :entries,
|
|
42
|
+
Coridoc::CoreModel::Bibliography => :entries,
|
|
43
|
+
Coridoc::CoreModel::AnnotationBlock => :children
|
|
44
|
+
}.freeze
|
|
45
|
+
|
|
46
|
+
def initialize(rewriter)
|
|
47
|
+
@rewriter = rewriter
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Entry point. Always returns a NEW root node — even Identity
|
|
51
|
+
# callers can rely on object identity to confirm the rewrite ran.
|
|
52
|
+
def visit_document(document)
|
|
53
|
+
return document unless document.is_a?(Coridoc::CoreModel::Base)
|
|
54
|
+
|
|
55
|
+
result = visit_subtree(document)
|
|
56
|
+
result.equal?(document) ? document.dup : result
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def visit_subtree(node)
|
|
62
|
+
return node unless node.prose?
|
|
63
|
+
return rewrite_inline(node) if node.is_a?(Coridoc::CoreModel::InlineElement)
|
|
64
|
+
|
|
65
|
+
reader = reader_for(node)
|
|
66
|
+
return node unless reader
|
|
67
|
+
|
|
68
|
+
rewrite_collection(node, reader)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Look up the children-reader method for +node+. Returns nil for
|
|
72
|
+
# unrecognized classes (no duck-typing — the CONTAINER_TYPES
|
|
73
|
+
# table is the single source of truth).
|
|
74
|
+
def reader_for(node)
|
|
75
|
+
CONTAINER_TYPES.each do |klass, reader|
|
|
76
|
+
return reader if node.is_a?(klass)
|
|
77
|
+
end
|
|
78
|
+
nil
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def rewrite_collection(node, attr_name)
|
|
82
|
+
original = node.public_send(attr_name)
|
|
83
|
+
return node if original.nil? || original.empty?
|
|
84
|
+
|
|
85
|
+
rewritten = original.map { |child| visit_subtree(child) }
|
|
86
|
+
return node if unchanged?(rewritten, original)
|
|
87
|
+
|
|
88
|
+
rebuild_with(node, attr_name => rewritten)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Inline dispatch. Typed LinkElement / CrossReferenceElement are
|
|
92
|
+
# always candidates; generic InlineElement instances must declare a
|
|
93
|
+
# matching format_type. Other typed subclasses (Bold, Italic, …)
|
|
94
|
+
# are walked for nested inlines instead of being rewritten.
|
|
95
|
+
def rewrite_inline(inline)
|
|
96
|
+
kind = inline.link_kind
|
|
97
|
+
|
|
98
|
+
rewritten_target = rewrite_target(inline, kind)
|
|
99
|
+
rewritten_nested = rewrite_nested(inline)
|
|
100
|
+
|
|
101
|
+
return inline if rewritten_target.nil? && rewritten_nested.nil?
|
|
102
|
+
|
|
103
|
+
overrides = {}
|
|
104
|
+
overrides[:target] = rewritten_target unless rewritten_target.nil?
|
|
105
|
+
overrides[:nested_elements] = rewritten_nested unless rewritten_nested.nil?
|
|
106
|
+
rebuild_with(inline, overrides)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def rewrite_target(inline, kind)
|
|
110
|
+
return nil unless kind
|
|
111
|
+
|
|
112
|
+
target = inline.target
|
|
113
|
+
return nil if target.nil? || target.empty?
|
|
114
|
+
|
|
115
|
+
new_target = @rewriter.call(
|
|
116
|
+
target: target,
|
|
117
|
+
kind: kind,
|
|
118
|
+
context: { in_verbatim: false }
|
|
119
|
+
)
|
|
120
|
+
return nil if new_target == target
|
|
121
|
+
|
|
122
|
+
new_target
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def rewrite_nested(inline)
|
|
126
|
+
nested = inline.nested_elements
|
|
127
|
+
return nil if nested.nil? || nested.empty?
|
|
128
|
+
|
|
129
|
+
rewritten = nested.map { |child| visit_subtree(child) }
|
|
130
|
+
return nil if unchanged?(rewritten, nested)
|
|
131
|
+
|
|
132
|
+
rewritten
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def rebuild_with(node, overrides)
|
|
136
|
+
duplicate = node.dup
|
|
137
|
+
overrides.each { |key, value| duplicate.public_send("#{key}=", value) }
|
|
138
|
+
duplicate
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def unchanged?(rewritten, original)
|
|
142
|
+
return false unless rewritten.length == original.length
|
|
143
|
+
|
|
144
|
+
rewritten.each_with_index.all? { |node, i| node.equal?(original[i]) }
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
# Post-parse link/xref rewriting.
|
|
5
|
+
#
|
|
6
|
+
# Consumers that need to canonicalize link and xref targets (snake→kebab,
|
|
7
|
+
# strip +.adoc+, redirect maps, dialect translation) get a single
|
|
8
|
+
# immutable entry point: +Coridoc.rewrite_links(doc, rewriter:, &)+.
|
|
9
|
+
# The visitor walks the parsed CoreModel, invokes the supplied rewriter
|
|
10
|
+
# for every link/xref target, and returns a NEW document. Verbatim
|
|
11
|
+
# blocks (source, listing, literal, pass, stem) are skipped entirely —
|
|
12
|
+
# coridoc owns the parse and guarantees those bodies never reach the
|
|
13
|
+
# rewriter, removing the "track parser state to avoid verbatim bodies"
|
|
14
|
+
# footgun that plagues regex-based rewriting.
|
|
15
|
+
#
|
|
16
|
+
# Two-step API mirrors +Coridoc.resolve_includes+: parse produces the
|
|
17
|
+
# document, rewrite is a separate explicit step the caller controls.
|
|
18
|
+
module LinkRewriter
|
|
19
|
+
autoload :Identity, "#{__dir__}/link_rewriter/identity"
|
|
20
|
+
autoload :Visitor, "#{__dir__}/link_rewriter/visitor"
|
|
21
|
+
|
|
22
|
+
class << self
|
|
23
|
+
# Rewrite every link/xref target in +doc+.
|
|
24
|
+
#
|
|
25
|
+
# +rewriter+ responds to +#call(target:, kind:, context:)+ and returns
|
|
26
|
+
# the new target String. If a block is given it is used as the
|
|
27
|
+
# rewriter. Omitting both falls back to {Identity} (no-op) — useful
|
|
28
|
+
# for "give me a structurally identical copy" cases.
|
|
29
|
+
#
|
|
30
|
+
# Returns a NEW document; the input is never mutated.
|
|
31
|
+
def rewrite(doc, rewriter: nil, &block)
|
|
32
|
+
callable = rewriter || block || Identity.new
|
|
33
|
+
Visitor.new(callable).visit_document(doc)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Lint
|
|
5
|
+
# Rule selection by format and id. Format gems register their
|
|
6
|
+
# rules at require time.
|
|
7
|
+
class Registry
|
|
8
|
+
def initialize
|
|
9
|
+
@rules = []
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def register(rule)
|
|
13
|
+
raise ArgumentError, "rule #{rule} must declare an id" if rule.id.nil?
|
|
14
|
+
raise ArgumentError, "rule #{rule} must declare a format" if rule.format.nil?
|
|
15
|
+
|
|
16
|
+
@rules << rule
|
|
17
|
+
self
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def for_format(format, only: nil, except: nil)
|
|
21
|
+
@rules.select do |rule|
|
|
22
|
+
rule.format == format &&
|
|
23
|
+
(only.nil? || only.include?(rule.id)) &&
|
|
24
|
+
(except.nil? || !except.include?(rule.id))
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def rule_ids(format)
|
|
29
|
+
for_format(format).map(&:id)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Lint
|
|
5
|
+
# Base class for lint rules. Subclasses (in the format gems)
|
|
6
|
+
# declare:
|
|
7
|
+
#
|
|
8
|
+
# id [String] stable rule code, e.g. 'ADOC001'
|
|
9
|
+
# format [Symbol] registered format the rule applies to
|
|
10
|
+
# check(document, path) [Array<Violation>] inspect the
|
|
11
|
+
# format-specific model tree
|
|
12
|
+
#
|
|
13
|
+
# Rules encapsulate their own traversal of the format model —
|
|
14
|
+
# the framework never switches on node types (OCP: a new rule is
|
|
15
|
+
# a new file, nothing else changes).
|
|
16
|
+
class Rule
|
|
17
|
+
class << self
|
|
18
|
+
attr_reader :id, :format
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def rule_id(id)
|
|
23
|
+
@id = id
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def applies_to(format)
|
|
27
|
+
@format = format
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def id
|
|
32
|
+
self.class.id
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def build_violation(path:, message:, line: nil, severity: :warning)
|
|
36
|
+
Violation.new(rule_id: id, path: path, line: line,
|
|
37
|
+
message: message, severity: severity)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'tmpdir'
|
|
4
|
+
|
|
5
|
+
module Coridoc
|
|
6
|
+
module Lint
|
|
7
|
+
# Runs rules over the format model trees a SourceGraph exposes.
|
|
8
|
+
# Files parse lazily — only when a rule set exists for their
|
|
9
|
+
# format — so linting a mixed corpus costs nothing for formats
|
|
10
|
+
# without rules.
|
|
11
|
+
class Runner
|
|
12
|
+
def initialize(format: nil, only: nil, except: nil,
|
|
13
|
+
registry: Coridoc::Lint.registry)
|
|
14
|
+
@format = format
|
|
15
|
+
@only = only
|
|
16
|
+
@except = except
|
|
17
|
+
@registry = registry
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
attr_reader :registry
|
|
21
|
+
|
|
22
|
+
# @param sources [String, Array<String>, SourceGraph::Graph]
|
|
23
|
+
# @return [Array<Violation>]
|
|
24
|
+
def run(sources)
|
|
25
|
+
graph = build_graph(sources)
|
|
26
|
+
graph.nodes.flat_map { |node| lint_node(node) }
|
|
27
|
+
.sort_by { |v| [v.path, v.rule_id] }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def build_graph(sources)
|
|
33
|
+
return sources if sources.is_a?(SourceGraph::Graph)
|
|
34
|
+
|
|
35
|
+
SourceGraph.build(Array(sources), sidecar: null_sidecar)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Lint runs should never silently create cache entries in the
|
|
39
|
+
# user's XDG dir unless persist is explicit.
|
|
40
|
+
def null_sidecar
|
|
41
|
+
SourceGraph::Sidecar.new(File.join(Dir.tmpdir, "coridoc-lint-#{Process.pid}"))
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def lint_node(node)
|
|
45
|
+
format = @format || node.format
|
|
46
|
+
rules = rules_for(format)
|
|
47
|
+
return [] if rules.empty?
|
|
48
|
+
|
|
49
|
+
rules.flat_map { |rule| rule.new.check(node.parsed, node.path) }
|
|
50
|
+
rescue Coridoc::UnsupportedFormatError
|
|
51
|
+
[]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Rules for a format, lazily requiring the format gem's lint
|
|
55
|
+
# ruleset on first sight — mirrors FormatCatalog.lazy_load_format.
|
|
56
|
+
def rules_for(format)
|
|
57
|
+
rules = @registry.for_format(format, only: @only, except: @except)
|
|
58
|
+
return rules unless rules.empty? && !format.nil?
|
|
59
|
+
|
|
60
|
+
require "coridoc/#{format}/lint"
|
|
61
|
+
@registry.for_format(format, only: @only, except: @except)
|
|
62
|
+
rescue LoadError
|
|
63
|
+
rules
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Lint
|
|
5
|
+
# Parameterized invariant templates (#309).
|
|
6
|
+
#
|
|
7
|
+
# An invariant is declared ONCE as an operator over references
|
|
8
|
+
# (lambdas); each format BINDS its own model paths into the
|
|
9
|
+
# operator. Semantics are shared, bindings are per-format — the
|
|
10
|
+
# formats keep their own model sets (hub-and-spoke).
|
|
11
|
+
#
|
|
12
|
+
# Coridoc::Lint::Template.register(
|
|
13
|
+
# :monotonic, id: 'MD001', format: :markdown,
|
|
14
|
+
# roots: ->(doc) { headings(doc) },
|
|
15
|
+
# level: :level.to_proc, label: :text.to_proc, order: :sequence
|
|
16
|
+
# )
|
|
17
|
+
#
|
|
18
|
+
# Operators (smallest expressive set first):
|
|
19
|
+
#
|
|
20
|
+
# :monotonic levels may not skip downward (refs: roots, level,
|
|
21
|
+
# label; optional children, order, message)
|
|
22
|
+
# :unique keys may not repeat (refs: roots, key, message;
|
|
23
|
+
# optional children)
|
|
24
|
+
# :present subjects must satisfy a presence test (refs:
|
|
25
|
+
# subjects, present, message)
|
|
26
|
+
module Template
|
|
27
|
+
# Shared plumbing: ref validation and the check entry point.
|
|
28
|
+
class Operator
|
|
29
|
+
REQUIRED = [].freeze
|
|
30
|
+
OPTIONAL = [].freeze
|
|
31
|
+
# Optional refs that are plain values, not lambdas.
|
|
32
|
+
CONFIG = [].freeze
|
|
33
|
+
|
|
34
|
+
def initialize(**refs)
|
|
35
|
+
unknown = refs.keys - self.class::REQUIRED - self.class::OPTIONAL
|
|
36
|
+
raise ArgumentError, "unknown refs #{unknown.inspect} for #{self.class.name}" unless unknown.empty?
|
|
37
|
+
|
|
38
|
+
missing = self.class::REQUIRED - refs.keys
|
|
39
|
+
raise ArgumentError, "missing refs #{missing.inspect} for #{self.class.name}" unless missing.empty?
|
|
40
|
+
|
|
41
|
+
bad = refs.reject { |name, value| self.class::CONFIG.include?(name) || value.respond_to?(:call) }
|
|
42
|
+
raise ArgumentError, "refs #{bad.keys.inspect} must be callable" unless bad.empty?
|
|
43
|
+
|
|
44
|
+
@refs = refs
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# @return [Array<String>] one message per violation
|
|
48
|
+
def messages(_document)
|
|
49
|
+
raise NotImplementedError
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
attr_reader :refs
|
|
55
|
+
|
|
56
|
+
def ref(name, default = nil)
|
|
57
|
+
refs.fetch(name, default)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Depth-first node enumeration through the optional
|
|
61
|
+
# children ref (none by default).
|
|
62
|
+
def each_node(nodes, children, &block)
|
|
63
|
+
Array(nodes).each do |node|
|
|
64
|
+
yield(node)
|
|
65
|
+
each_node(children.call(node), children, &block)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Levels must not skip: a node may be at most one level deeper
|
|
71
|
+
# than its predecessor — its enclosing node (order: :tree,
|
|
72
|
+
# via children) or the previous node (order: :sequence).
|
|
73
|
+
class Monotonic < Operator
|
|
74
|
+
REQUIRED = %i[roots level label].freeze
|
|
75
|
+
OPTIONAL = %i[children order message].freeze
|
|
76
|
+
CONFIG = %i[order].freeze
|
|
77
|
+
NO_CHILDREN = ->(_node) { [] }
|
|
78
|
+
DEFAULT_MESSAGE = lambda do |from, to, label|
|
|
79
|
+
"heading level skips from #{from} to #{to}: #{label}"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def initialize(**refs)
|
|
83
|
+
super
|
|
84
|
+
@order = refs.fetch(:order, :tree)
|
|
85
|
+
return if %i[tree sequence].include?(@order)
|
|
86
|
+
|
|
87
|
+
raise ArgumentError, "order must be :tree or :sequence, got #{@order.inspect}"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def messages(document)
|
|
91
|
+
out = []
|
|
92
|
+
nodes = ref(:roots).call(document)
|
|
93
|
+
if @order == :sequence
|
|
94
|
+
sequence(nodes, out)
|
|
95
|
+
else
|
|
96
|
+
walk(nodes, 0, out)
|
|
97
|
+
end
|
|
98
|
+
out
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
private
|
|
102
|
+
|
|
103
|
+
def sequence(nodes, out)
|
|
104
|
+
Array(nodes).each_cons(2) do |prev, node|
|
|
105
|
+
check(ref(:level).call(prev), ref(:level).call(node), node, out)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def walk(nodes, parent_level, out)
|
|
110
|
+
Array(nodes).each do |node|
|
|
111
|
+
level = ref(:level).call(node)
|
|
112
|
+
check(parent_level, level, node, out)
|
|
113
|
+
walk(ref(:children, NO_CHILDREN).call(node), level || parent_level, out)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def check(from, to, node, out)
|
|
118
|
+
return unless to && to > from + 1
|
|
119
|
+
|
|
120
|
+
out << ref(:message, DEFAULT_MESSAGE).call(from, to, ref(:label).call(node))
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Keys must not repeat. Nodes whose key is nil are ignored.
|
|
125
|
+
class Unique < Operator
|
|
126
|
+
REQUIRED = %i[roots key message].freeze
|
|
127
|
+
OPTIONAL = %i[children].freeze
|
|
128
|
+
|
|
129
|
+
def messages(document)
|
|
130
|
+
counts = Hash.new(0)
|
|
131
|
+
each_node(ref(:roots).call(document),
|
|
132
|
+
ref(:children, Monotonic::NO_CHILDREN)) do |node|
|
|
133
|
+
key = ref(:key).call(node)
|
|
134
|
+
counts[key] += 1 unless key.nil?
|
|
135
|
+
end
|
|
136
|
+
counts.select { |_key, count| count > 1 }
|
|
137
|
+
.map { |key, count| ref(:message).call(key, count) }
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# Every subject must satisfy the presence test.
|
|
142
|
+
class Present < Operator
|
|
143
|
+
REQUIRED = %i[subjects present message].freeze
|
|
144
|
+
|
|
145
|
+
def messages(document)
|
|
146
|
+
Array(ref(:subjects).call(document))
|
|
147
|
+
.reject { |subject| ref(:present).call(subject) }
|
|
148
|
+
.map { |subject| ref(:message).call(subject) }
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
OPERATORS = {
|
|
153
|
+
monotonic: Monotonic,
|
|
154
|
+
unique: Unique,
|
|
155
|
+
present: Present
|
|
156
|
+
}.freeze
|
|
157
|
+
|
|
158
|
+
class << self
|
|
159
|
+
# Bind a format's refs into an operator, yielding a Rule
|
|
160
|
+
# subclass the existing Registry / Runner can use unchanged.
|
|
161
|
+
#
|
|
162
|
+
# @param operator [Symbol] one of OPERATORS
|
|
163
|
+
# @return [Class<Rule>]
|
|
164
|
+
def bind(operator, id:, format:, **refs)
|
|
165
|
+
klass = OPERATORS.fetch(operator) do
|
|
166
|
+
raise ArgumentError, "unknown template operator #{operator.inspect}"
|
|
167
|
+
end
|
|
168
|
+
evaluator = klass.new(**refs)
|
|
169
|
+
|
|
170
|
+
Class.new(Rule) do
|
|
171
|
+
rule_id id
|
|
172
|
+
applies_to format
|
|
173
|
+
define_method(:check) do |document, path|
|
|
174
|
+
evaluator.messages(document).map do |message|
|
|
175
|
+
build_violation(path: path, message: message)
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# Bind and register in the lint registry.
|
|
182
|
+
def register(operator, registry: Coridoc::Lint.registry, **)
|
|
183
|
+
rule = bind(operator, **)
|
|
184
|
+
registry.register(rule)
|
|
185
|
+
rule
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Lint
|
|
5
|
+
# One reported rule violation. Plain data — path and rule always
|
|
6
|
+
# present, line nil when the model does not carry source
|
|
7
|
+
# positions.
|
|
8
|
+
Violation = Struct.new(:rule_id, :path, :line, :message, :severity,
|
|
9
|
+
keyword_init: true) do
|
|
10
|
+
def to_s
|
|
11
|
+
line_part = line ? ":#{line}" : ''
|
|
12
|
+
"#{path}#{line_part} [#{rule_id}] #{message}"
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|