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,155 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module CoreModel
|
|
5
|
+
# Walks a CoreModel tree and resolves InlineElement nodes whose
|
|
6
|
+
# format_type is 'attribute_reference' against a Metadata store.
|
|
7
|
+
#
|
|
8
|
+
# AsciiDoc `{foo}` references round-trip as InlineElement with
|
|
9
|
+
# format_type: 'attribute_reference' and target: 'foo'. After the
|
|
10
|
+
# document is parsed and its attributes are known, this visitor
|
|
11
|
+
# rewrites those nodes in place to TextContent carrying the value.
|
|
12
|
+
# Unresolved references are left untouched so they survive
|
|
13
|
+
# serialisation back to the source format.
|
|
14
|
+
#
|
|
15
|
+
# The visitor never mutates its input tree; it returns a new tree
|
|
16
|
+
# sharing unchanged subtrees. InlineElement#with_content already
|
|
17
|
+
# produces non-mutating copies for inline content; the same pattern
|
|
18
|
+
# is used here at the block level via `replace_children`.
|
|
19
|
+
class AttributeReferenceResolver
|
|
20
|
+
attr_reader :attributes
|
|
21
|
+
|
|
22
|
+
def self.call(root, attributes)
|
|
23
|
+
new(attributes).visit(root)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def initialize(attributes)
|
|
27
|
+
@attributes = attributes || Metadata.new
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def visit(node)
|
|
31
|
+
return node.map { |child| visit(child) } if node.is_a?(Array)
|
|
32
|
+
|
|
33
|
+
return node unless node.is_a?(Base)
|
|
34
|
+
|
|
35
|
+
visit_typed(node)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def visit_typed(node)
|
|
41
|
+
case node
|
|
42
|
+
when DocumentElement, SectionElement
|
|
43
|
+
rebuild_children(node)
|
|
44
|
+
when Table
|
|
45
|
+
rebuild_collection(node, :rows)
|
|
46
|
+
when TableRow
|
|
47
|
+
rebuild_collection(node, :cells)
|
|
48
|
+
when Block, ParagraphBlock, ListItem, TableCell, Term,
|
|
49
|
+
QuoteBlock, ExampleBlock, SidebarBlock, OpenBlock,
|
|
50
|
+
AnnotationBlock, ReviewerBlock, VerseBlock, LiteralBlock,
|
|
51
|
+
PassBlock, StemBlock, ListingBlock, SourceBlock,
|
|
52
|
+
CommentBlock, FrontmatterBlock
|
|
53
|
+
rebuild_block(node)
|
|
54
|
+
when DefinitionList, DefinitionItem
|
|
55
|
+
rebuild_children(node)
|
|
56
|
+
when InlineElement, RawInlineElement
|
|
57
|
+
resolve_inline(node)
|
|
58
|
+
else
|
|
59
|
+
node
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def rebuild_children(node)
|
|
64
|
+
return node unless node.is_a?(CoreModel::HasChildren)
|
|
65
|
+
|
|
66
|
+
original = node.children
|
|
67
|
+
return node if original.nil?
|
|
68
|
+
|
|
69
|
+
updated = original.is_a?(Array) ? original.map { |c| visit(c) } : visit(original)
|
|
70
|
+
return node if updated == original
|
|
71
|
+
|
|
72
|
+
node.dup.tap { |copy| copy.children = updated }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# For nodes whose child collection lives on a non-`children`
|
|
76
|
+
# attribute (Table#rows, TableRow#cells). Walks the collection,
|
|
77
|
+
# rebuilds the parent only when at least one child changed.
|
|
78
|
+
def rebuild_collection(node, attr_name)
|
|
79
|
+
original = node.public_send(attr_name)
|
|
80
|
+
return node if original.nil?
|
|
81
|
+
|
|
82
|
+
updated = if original.is_a?(Array)
|
|
83
|
+
original.map { |c| visit(c) }
|
|
84
|
+
else
|
|
85
|
+
visit(original)
|
|
86
|
+
end
|
|
87
|
+
return node if updated == original
|
|
88
|
+
|
|
89
|
+
node.dup.tap { |copy| copy.public_send("#{attr_name}=", updated) }
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def rebuild_block(node)
|
|
93
|
+
return node unless node.is_a?(CoreModel::HasChildren)
|
|
94
|
+
|
|
95
|
+
original_children = node.children
|
|
96
|
+
return node if original_children.nil?
|
|
97
|
+
|
|
98
|
+
updated_children = if original_children.is_a?(Array)
|
|
99
|
+
original_children.map { |c| visit(c) }
|
|
100
|
+
else
|
|
101
|
+
visit(original_children)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
if inline_resolvable?(node)
|
|
105
|
+
original_content = node.content
|
|
106
|
+
updated_content = resolve_content(original_content)
|
|
107
|
+
else
|
|
108
|
+
updated_content = node.content
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
return node if updated_children == original_children && updated_content == node.content
|
|
112
|
+
|
|
113
|
+
node.dup.tap do |copy|
|
|
114
|
+
copy.children = updated_children
|
|
115
|
+
copy.content = updated_content if inline_resolvable?(node)
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def inline_resolvable?(node)
|
|
120
|
+
# Capability check: nodes that include ChildrenContent expose a
|
|
121
|
+
# `content` attribute alongside `children`. We resolve attribute
|
|
122
|
+
# references only when content is a String (plain-text inline
|
|
123
|
+
# content). Using is_a?(ChildrenContent) instead of
|
|
124
|
+
# respond_to?(:content) catches type drift early: if a node
|
|
125
|
+
# acquires string content without including ChildrenContent,
|
|
126
|
+
# this method returns false (no resolution) rather than
|
|
127
|
+
# silently resolving on a mis-typed node.
|
|
128
|
+
node.is_a?(CoreModel::ChildrenContent) && node.content.is_a?(String)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Resolve attribute references inside a flat content string.
|
|
132
|
+
# Only replaces `{name}` when +name+ exists in +attributes+;
|
|
133
|
+
# unknown references are preserved verbatim for round-trip.
|
|
134
|
+
def resolve_content(content)
|
|
135
|
+
return content unless content.is_a?(String)
|
|
136
|
+
|
|
137
|
+
content.gsub(/\{([a-zA-Z0-9_-]+)\}/) do |match|
|
|
138
|
+
name = Regexp.last_match(1)
|
|
139
|
+
attributes.key?(name) ? attributes[name].to_s : match
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def resolve_inline(node)
|
|
144
|
+
return node unless node.is_a?(InlineElement)
|
|
145
|
+
return node unless node.resolve_format_type == 'attribute_reference'
|
|
146
|
+
|
|
147
|
+
target = node.target
|
|
148
|
+
return node if target.nil? || target.empty?
|
|
149
|
+
return node unless attributes.key?(target)
|
|
150
|
+
|
|
151
|
+
TextContent.new(text: attributes[target].to_s)
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'lutaml/model'
|
|
4
|
+
|
|
5
|
+
module Coridoc
|
|
6
|
+
module CoreModel
|
|
7
|
+
# Base class for all core models
|
|
8
|
+
#
|
|
9
|
+
# Provides common functionality for schema-agnostic document models.
|
|
10
|
+
# This class establishes the foundational structure for all CoreModel
|
|
11
|
+
# classes, including semantic equivalence comparison and common
|
|
12
|
+
# attributes.
|
|
13
|
+
#
|
|
14
|
+
# @example Creating a base model
|
|
15
|
+
# model = CoreModel::Base.new(
|
|
16
|
+
# id: "example-1",
|
|
17
|
+
# title: "Example Title",
|
|
18
|
+
# element_attributes: [
|
|
19
|
+
# CoreModel::ElementAttribute.new(name: "role", value: "note")
|
|
20
|
+
# ]
|
|
21
|
+
# )
|
|
22
|
+
#
|
|
23
|
+
# @example Semantic comparison
|
|
24
|
+
# model1 = CoreModel::Base.new(id: "test", title: "Test")
|
|
25
|
+
# model2 = CoreModel::Base.new(id: "test", title: "Test")
|
|
26
|
+
# model1.semantically_equivalent?(model2) # => true
|
|
27
|
+
class Base < Lutaml::Model::Serializable
|
|
28
|
+
# @!attribute id
|
|
29
|
+
# @return [String, nil] unique identifier for the element
|
|
30
|
+
attribute :id, :string
|
|
31
|
+
|
|
32
|
+
# @!attribute title
|
|
33
|
+
# @return [String, nil] title of the element
|
|
34
|
+
attribute :title, :string
|
|
35
|
+
|
|
36
|
+
# @!attribute element_attributes
|
|
37
|
+
# @return [Array<ElementAttribute>] collection of element attributes
|
|
38
|
+
# Lazy string type names: ElementAttribute/MetadataEntry inherit
|
|
39
|
+
# Base, so eager constants deadlock the autoload cycle when either
|
|
40
|
+
# is referenced first (see list_block.rb for the same pattern).
|
|
41
|
+
attribute :element_attributes, 'Coridoc::CoreModel::ElementAttribute', collection: true
|
|
42
|
+
|
|
43
|
+
# @!attribute metadata_entries
|
|
44
|
+
# @return [Array<MetadataEntry>] additional metadata entries
|
|
45
|
+
attribute :metadata_entries, 'Coridoc::CoreModel::MetadataEntry', collection: true
|
|
46
|
+
|
|
47
|
+
# 1-indexed source line where this element begins, when known.
|
|
48
|
+
# Populated by format transformers (e.g., AsciiDoc's SourceLineExtractor)
|
|
49
|
+
# so consumers can map AST nodes back to the source text. nil for
|
|
50
|
+
# programmatically constructed models. Single source of truth across
|
|
51
|
+
# all CoreModel types (Issue 1, STATUS-2026-06-28).
|
|
52
|
+
attribute :source_line, :integer
|
|
53
|
+
|
|
54
|
+
# Construct an instance and yield it for in-place mutation.
|
|
55
|
+
#
|
|
56
|
+
# This is the programmatic-construction entry point for CoreModel
|
|
57
|
+
# nodes. It calls +new+ exactly as a caller would, then yields
|
|
58
|
+
# the resulting instance for append-style construction. No new
|
|
59
|
+
# class hierarchy, no +method_missing+ — the block operates on
|
|
60
|
+
# the real model object.
|
|
61
|
+
#
|
|
62
|
+
# Per-class fluent helpers (e.g., +ListBlock#add_item+,
|
|
63
|
+
# +ListItem#add_text+) compose naturally with +build+:
|
|
64
|
+
#
|
|
65
|
+
# list = ListBlock.build do |ul|
|
|
66
|
+
# children.each { |c| ul.add_item { |li| li.add_link(c[:slug], text: c[:title]) } }
|
|
67
|
+
# end
|
|
68
|
+
#
|
|
69
|
+
# Without a block, +build(**attrs)+ is identical to +new(**attrs)+.
|
|
70
|
+
def self.build(**attrs)
|
|
71
|
+
instance = new(**attrs)
|
|
72
|
+
yield instance if block_given?
|
|
73
|
+
instance
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Get all metadata as a hash, or a specific metadata value by key
|
|
77
|
+
# @overload metadata
|
|
78
|
+
# @return [Hash] All metadata as key-value pairs
|
|
79
|
+
# @overload metadata(key)
|
|
80
|
+
# @param key [String] The metadata key
|
|
81
|
+
# @return [String, nil] The value or nil
|
|
82
|
+
def metadata(key = nil)
|
|
83
|
+
entries = metadata_entries || []
|
|
84
|
+
if key.nil?
|
|
85
|
+
# Return all metadata as hash
|
|
86
|
+
entries.each_with_object({}) { |e, h| h[e.key] = e.value }
|
|
87
|
+
else
|
|
88
|
+
# Return specific value
|
|
89
|
+
entries.find { |e| e.key == key }&.value
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Convenience method to set metadata
|
|
94
|
+
# @param key [String] The metadata key
|
|
95
|
+
# @param value [String] The value to set
|
|
96
|
+
def set_metadata(key, value)
|
|
97
|
+
self.metadata_entries ||= []
|
|
98
|
+
existing = metadata_entries.find { |e| e.key == key }
|
|
99
|
+
if existing
|
|
100
|
+
existing.value = value
|
|
101
|
+
else
|
|
102
|
+
metadata_entries << MetadataEntry.new(key: key, value: value)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Get all element attributes as a hash, or a specific attribute value by name
|
|
107
|
+
# @overload attr
|
|
108
|
+
# @return [Hash] All attributes as key-value pairs
|
|
109
|
+
# @overload attr(name)
|
|
110
|
+
# @param name [String] The attribute name
|
|
111
|
+
# @return [String, nil] The value or nil
|
|
112
|
+
def attr(name = nil)
|
|
113
|
+
attrs = element_attributes || []
|
|
114
|
+
if name.nil?
|
|
115
|
+
# Return all attributes as hash
|
|
116
|
+
attrs.each_with_object({}) { |a, h| h[a.name] = a.value }
|
|
117
|
+
else
|
|
118
|
+
# Return specific value
|
|
119
|
+
attrs.find { |a| a.name == name }&.value
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Set attribute value
|
|
124
|
+
# @param name [String] The attribute name
|
|
125
|
+
# @param value [String] The value to set
|
|
126
|
+
def set_attr(name, value)
|
|
127
|
+
self.element_attributes ||= []
|
|
128
|
+
existing = element_attributes.find { |a| a.name == name }
|
|
129
|
+
if existing
|
|
130
|
+
existing.value = value
|
|
131
|
+
else
|
|
132
|
+
element_attributes << ElementAttribute.new(name: name, value: value)
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Compare this model with another for semantic equivalence
|
|
137
|
+
#
|
|
138
|
+
# Semantic equivalence means the models represent the same semantic
|
|
139
|
+
# content, even if their exact structure differs. This is different
|
|
140
|
+
# from equality, which requires exact matching.
|
|
141
|
+
#
|
|
142
|
+
# @param other [Object] the object to compare with
|
|
143
|
+
# @return [Boolean] true if semantically equivalent, false otherwise
|
|
144
|
+
def semantically_equivalent?(other)
|
|
145
|
+
return false unless other.is_a?(self.class)
|
|
146
|
+
|
|
147
|
+
comparable_attributes.all? do |attr|
|
|
148
|
+
compare_attribute(attr, other)
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def flat_text
|
|
153
|
+
''
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# Flatten this element to a plain-text string.
|
|
157
|
+
#
|
|
158
|
+
# Subclasses that include ChildrenContent override this to
|
|
159
|
+
# concatenate their children's text. Block-level elements
|
|
160
|
+
# without textual content (ListBlock, Table, etc.) fall back
|
|
161
|
+
# to the empty string — they are serialized structurally,
|
|
162
|
+
# not flattened into inline text.
|
|
163
|
+
#
|
|
164
|
+
# @return [String]
|
|
165
|
+
def flat_text
|
|
166
|
+
''
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# True when this node counts as real body content for empty-body
|
|
170
|
+
# detection and similar structural queries. Default is true;
|
|
171
|
+
# ephemeral and metadata nodes override to false.
|
|
172
|
+
#
|
|
173
|
+
# Override sites: CommentBlock, CommentLine, FrontmatterBlock.
|
|
174
|
+
# Adding a new "skip me" type = overriding this method locally —
|
|
175
|
+
# no central walker to edit (OCP).
|
|
176
|
+
#
|
|
177
|
+
# @return [Boolean]
|
|
178
|
+
def body_content?
|
|
179
|
+
true
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# True when this node's text content should be processed by text
|
|
183
|
+
# consumers (link rewriters, spell checkers, search indexers, etc.).
|
|
184
|
+
# Default delegates to +body_content?+ — ephemeral/metadata nodes
|
|
185
|
+
# (CommentBlock, FrontmatterBlock, …) are not prose because they
|
|
186
|
+
# are not body content. Verbatim block subclasses (SourceBlock,
|
|
187
|
+
# ListingBlock, LiteralBlock, PassBlock, StemBlock) override this
|
|
188
|
+
# to false because their text is literal, not subject to
|
|
189
|
+
# substitution or spell-checking.
|
|
190
|
+
#
|
|
191
|
+
# Override sites for verbatim: SourceBlock, ListingBlock,
|
|
192
|
+
# LiteralBlock, PassBlock, StemBlock, HorizontalRuleBlock.
|
|
193
|
+
#
|
|
194
|
+
# Distinct from +body_content?+: a SourceBlock IS body content
|
|
195
|
+
# (the doc has real substance) but its text is not prose (a link
|
|
196
|
+
# shaped string inside source is literal text, not a link).
|
|
197
|
+
#
|
|
198
|
+
# @return [Boolean]
|
|
199
|
+
def prose?
|
|
200
|
+
body_content?
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# True when this node is structurally present but carries no
|
|
204
|
+
# visible characters (e.g., a paragraph whose text strips to
|
|
205
|
+
# empty). Default is false; inline content and paragraphs override.
|
|
206
|
+
#
|
|
207
|
+
# @return [Boolean]
|
|
208
|
+
def whitespace_only?
|
|
209
|
+
false
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# Accept a visitor to traverse this element
|
|
213
|
+
#
|
|
214
|
+
# Implements the visitor pattern for document traversal.
|
|
215
|
+
# The visitor's visit method will be called with this element.
|
|
216
|
+
#
|
|
217
|
+
# @param visitor [Coridoc::Visitor::Base] Visitor to accept
|
|
218
|
+
# @return [void]
|
|
219
|
+
def accept(visitor)
|
|
220
|
+
visitor.visit(self)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# True when this node counts as "real body content" for the
|
|
224
|
+
# purposes of empty-document detection and similar structural
|
|
225
|
+
# queries. Default is true; metadata and ephemeral nodes
|
|
226
|
+
# (FrontmatterBlock, CommentBlock, CommentLine) override to
|
|
227
|
+
# false. Polymorphic dispatch keeps the predicate open for
|
|
228
|
+
# future "skip-me" types — no central walker to edit (OCP).
|
|
229
|
+
def body_content?
|
|
230
|
+
true
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# True when this node is structurally present but carries no
|
|
234
|
+
# visible characters. Default is false; inline text and
|
|
235
|
+
# paragraph blocks override to inspect their text content.
|
|
236
|
+
def whitespace_only?
|
|
237
|
+
false
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
private
|
|
241
|
+
|
|
242
|
+
# List of attributes to compare for semantic equivalence
|
|
243
|
+
#
|
|
244
|
+
# Override in subclasses to define which attributes matter for
|
|
245
|
+
# equivalence. By default, only id and title are compared.
|
|
246
|
+
#
|
|
247
|
+
# @return [Array<Symbol>] list of attribute names to compare
|
|
248
|
+
def comparable_attributes
|
|
249
|
+
%i[id title]
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
# Compare a single attribute between this model and another
|
|
253
|
+
def compare_attribute(attr, other)
|
|
254
|
+
self_value = public_send(attr)
|
|
255
|
+
other_value = other.public_send(attr)
|
|
256
|
+
|
|
257
|
+
case self_value
|
|
258
|
+
when Array
|
|
259
|
+
compare_arrays(self_value, other_value)
|
|
260
|
+
when Base
|
|
261
|
+
self_value.semantically_equivalent?(other_value)
|
|
262
|
+
else
|
|
263
|
+
self_value == other_value
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
# Compare two arrays for semantic equivalence
|
|
268
|
+
def compare_arrays(arr1, arr2)
|
|
269
|
+
return false unless arr1.size == arr2.size
|
|
270
|
+
|
|
271
|
+
arr1.zip(arr2).all? do |item1, item2|
|
|
272
|
+
if item1.is_a?(Base)
|
|
273
|
+
item1.semantically_equivalent?(item2)
|
|
274
|
+
else
|
|
275
|
+
item1 == item2
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module CoreModel
|
|
5
|
+
# Bibliography section in a document.
|
|
6
|
+
#
|
|
7
|
+
# Bibliography sections contain lists of references cited in the document.
|
|
8
|
+
#
|
|
9
|
+
# @!attribute [r] id
|
|
10
|
+
# @return [String, nil] Optional identifier for the bibliography
|
|
11
|
+
#
|
|
12
|
+
# @!attribute [r] title
|
|
13
|
+
# @return [String, nil] Bibliography section title
|
|
14
|
+
#
|
|
15
|
+
# @!attribute [r] level
|
|
16
|
+
# @return [Integer, nil] Section level
|
|
17
|
+
#
|
|
18
|
+
# @!attribute [r] entries
|
|
19
|
+
# @return [Array<Coridoc::CoreModel::BibliographyEntry>] Bibliography entries
|
|
20
|
+
#
|
|
21
|
+
# @example Create a bibliography section
|
|
22
|
+
# bib = Coridoc::CoreModel::Bibliography.new(
|
|
23
|
+
# id: "norm-refs",
|
|
24
|
+
# title: "Normative references",
|
|
25
|
+
# entries: [
|
|
26
|
+
# Coridoc::CoreModel::BibliographyEntry.new(
|
|
27
|
+
# anchor_name: "ISO712",
|
|
28
|
+
# document_id: "ISO 712",
|
|
29
|
+
# ref_text: "Cereals and cereal products..."
|
|
30
|
+
# )
|
|
31
|
+
# ]
|
|
32
|
+
# )
|
|
33
|
+
#
|
|
34
|
+
class Bibliography < Base
|
|
35
|
+
attribute :id, :string
|
|
36
|
+
attribute :title, :string
|
|
37
|
+
attribute :level, :integer
|
|
38
|
+
attribute :entries, Coridoc::CoreModel::BibliographyEntry, collection: true
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module CoreModel
|
|
5
|
+
# Individual bibliography entry (reference).
|
|
6
|
+
#
|
|
7
|
+
# Bibliography entries represent single references within a bibliography,
|
|
8
|
+
# with anchor names for citation linking.
|
|
9
|
+
#
|
|
10
|
+
# @!attribute [r] anchor_name
|
|
11
|
+
# @return [String, nil] The anchor name for citing this entry (e.g., "ISO712")
|
|
12
|
+
#
|
|
13
|
+
# @!attribute [r] document_id
|
|
14
|
+
# @return [String, nil] The document identifier (e.g., "ISO 712")
|
|
15
|
+
#
|
|
16
|
+
# @!attribute [r] ref_text
|
|
17
|
+
# @return [String, nil] The reference text/citation description
|
|
18
|
+
#
|
|
19
|
+
# @!attribute [r] url
|
|
20
|
+
# @return [String, nil] Optional URL for the reference
|
|
21
|
+
#
|
|
22
|
+
# @example Create a bibliography entry
|
|
23
|
+
# entry = Coridoc::CoreModel::BibliographyEntry.new(
|
|
24
|
+
# anchor_name: "ISO712",
|
|
25
|
+
# document_id: "ISO 712",
|
|
26
|
+
# ref_text: "Cereals and cereal products. Determination of moisture content."
|
|
27
|
+
# )
|
|
28
|
+
#
|
|
29
|
+
class BibliographyEntry < Base
|
|
30
|
+
attribute :anchor_name, :string
|
|
31
|
+
attribute :document_id, :string
|
|
32
|
+
attribute :ref_text, :string
|
|
33
|
+
attribute :url, :string
|
|
34
|
+
|
|
35
|
+
# Returns a formatted display string combining label and reference text.
|
|
36
|
+
#
|
|
37
|
+
# Uses document_id or anchor_name as the label, falling back to ref_text
|
|
38
|
+
# alone when no label is available.
|
|
39
|
+
#
|
|
40
|
+
# @return [String] formatted citation text
|
|
41
|
+
def display_text
|
|
42
|
+
label = document_id || anchor_name || ''
|
|
43
|
+
ref = ref_text || ''
|
|
44
|
+
label.empty? ? ref : "#{label}: #{ref}"
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module CoreModel
|
|
5
|
+
# Generic block model
|
|
6
|
+
#
|
|
7
|
+
# Represents all block-level elements in a format-neutral way.
|
|
8
|
+
# Typed subclasses (SourceBlock, QuoteBlock, etc.) express their
|
|
9
|
+
# semantic identity via the class hierarchy — the class IS the type.
|
|
10
|
+
# Generic Block instances use block_semantic_type for typing.
|
|
11
|
+
class Block < Base
|
|
12
|
+
attribute :children, Base, collection: true
|
|
13
|
+
|
|
14
|
+
include ChildrenContent
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
def semantic_type
|
|
18
|
+
nil
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def resolve_semantic_type
|
|
23
|
+
self.class.semantic_type || block_semantic_type&.to_sym
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Derived element_type string for backward compatibility.
|
|
27
|
+
# Returns the semantic type as a string, derived from the class
|
|
28
|
+
# or block_semantic_type.
|
|
29
|
+
def element_type
|
|
30
|
+
resolve_semantic_type&.to_s
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# @!attribute block_semantic_type
|
|
34
|
+
# @return [String, nil] semantic type for generic Block instances.
|
|
35
|
+
# Typed subclasses should not override this — use the class instead.
|
|
36
|
+
attribute :block_semantic_type, :string
|
|
37
|
+
|
|
38
|
+
# @!attribute delimiter_type
|
|
39
|
+
# @return [String, nil] raw delimiter for round-trip fidelity.
|
|
40
|
+
# Format-specific; CoreModel does NOT derive semantics from this.
|
|
41
|
+
attribute :delimiter_type, :string
|
|
42
|
+
|
|
43
|
+
# @!attribute content
|
|
44
|
+
# @return [String, nil] the block's text content (simple string)
|
|
45
|
+
# For mixed content with inline elements, use children instead.
|
|
46
|
+
attribute :content, :string
|
|
47
|
+
|
|
48
|
+
# @!attribute lines
|
|
49
|
+
# @return [Array<String>, nil] individual lines of content
|
|
50
|
+
attribute :lines, :string, collection: true
|
|
51
|
+
|
|
52
|
+
# @!attribute language
|
|
53
|
+
# @return [String, nil] language identifier for source code blocks
|
|
54
|
+
attribute :language, :string
|
|
55
|
+
|
|
56
|
+
# @!attribute callouts
|
|
57
|
+
# @return [Array<Callout>] callout annotations attached to this
|
|
58
|
+
# block. Empty for most block types; populated by format gems
|
|
59
|
+
# when AsciiDoc-style `<N>` annotations follow a verbatim block.
|
|
60
|
+
attribute :callouts, Callout, collection: true, default: -> { [] }
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def comparable_attributes
|
|
65
|
+
super + %i[block_semantic_type content callouts]
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module CoreModel
|
|
5
|
+
# A single callout annotation attached to a verbatim block.
|
|
6
|
+
#
|
|
7
|
+
# Callouts are the AsciiDoc convention for annotating individual lines
|
|
8
|
+
# of a source/listing block: `<1>` markers appear inside the code and
|
|
9
|
+
# matching `<1> explanation` lines follow the block. Markdown has no
|
|
10
|
+
# native equivalent, so each format gem decides how to render them.
|
|
11
|
+
#
|
|
12
|
+
# The CoreModel stores each annotation as a typed Callout on its parent
|
|
13
|
+
# block, with the in-code marker `<index>` preserved in the block's
|
|
14
|
+
# `content` for verbatim round-trip.
|
|
15
|
+
class Callout < Base
|
|
16
|
+
# @!attribute index
|
|
17
|
+
# @return [Integer, nil] 1-based callout number matching the
|
|
18
|
+
# `<N>` marker embedded in the parent block's content.
|
|
19
|
+
attribute :index, :integer
|
|
20
|
+
|
|
21
|
+
# @!attribute content
|
|
22
|
+
# @return [String, nil] human-readable annotation text.
|
|
23
|
+
attribute :content, :string
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def comparable_attributes
|
|
28
|
+
super + %i[index content]
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module CoreModel
|
|
5
|
+
# Shared helpers for rendering Callout-annotated verbatim blocks.
|
|
6
|
+
#
|
|
7
|
+
# Both the Markdown and HTML spokes need to (a) order callouts by their
|
|
8
|
+
# numeric index and (b) strip AsciiDoc-style `<N>` markers from the
|
|
9
|
+
# raw code so they don't leak as literal text in the output format.
|
|
10
|
+
# Centralizing these operations here keeps the behavior consistent
|
|
11
|
+
# across spokes and avoids copy-paste drift.
|
|
12
|
+
module CalloutText
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
def ordered(callouts)
|
|
16
|
+
Array(callouts).sort_by { |c| c.index || Float::INFINITY }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Removes callout markers (`<N>`) from `code` for the indices
|
|
20
|
+
# referenced by `callouts`. Returns `code` unchanged when no
|
|
21
|
+
# callouts are provided or none carry a usable index, so literal
|
|
22
|
+
# `<N>` sequences in code without callouts are preserved.
|
|
23
|
+
def strip_markers(code, callouts)
|
|
24
|
+
list = Array(callouts)
|
|
25
|
+
return code if list.empty?
|
|
26
|
+
|
|
27
|
+
indices = list.filter_map(&:index).uniq
|
|
28
|
+
return code if indices.empty?
|
|
29
|
+
|
|
30
|
+
pattern = /<\s*(?:#{indices.join('|')})\s*>/
|
|
31
|
+
code.to_s.lines(chomp: true).map { |line| line.gsub(pattern, '').rstrip }.join("\n")
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|