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,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module CoreModel
|
|
5
|
+
# Shared helpers for models that carry mixed-content children
|
|
6
|
+
# (TextContent + InlineElement + Block instances) alongside a string
|
|
7
|
+
# content attribute.
|
|
8
|
+
#
|
|
9
|
+
# Included by Block, ListItem, TableCell, and InlineElement.
|
|
10
|
+
# The children attribute is declared as
|
|
11
|
+
# attribute :children, Base, collection: true
|
|
12
|
+
# on each including class. This module overrides the setter to
|
|
13
|
+
# auto-wrap raw strings as TextContent, keeping all callers simple.
|
|
14
|
+
#
|
|
15
|
+
# Includes HasChildren so all mixed-content classes also satisfy
|
|
16
|
+
# the structural predicate (OCP — no subclass enumeration needed
|
|
17
|
+
# for children-based dispatch).
|
|
18
|
+
module ChildrenContent
|
|
19
|
+
include HasChildren
|
|
20
|
+
# Override the children= setter to auto-wrap strings as TextContent.
|
|
21
|
+
# This is defined via define_method so it always overrides the
|
|
22
|
+
# lutaml-generated setter, regardless of include order.
|
|
23
|
+
def self.included(base)
|
|
24
|
+
super
|
|
25
|
+
|
|
26
|
+
base.define_method(:children=) do |value|
|
|
27
|
+
wrapped = Array(value).map do |item|
|
|
28
|
+
next nil if item.nil?
|
|
29
|
+
next item if item.is_a?(CoreModel::Base)
|
|
30
|
+
|
|
31
|
+
CoreModel::TextContent.new(text: item.to_s)
|
|
32
|
+
end.compact
|
|
33
|
+
# Lutaml defines the setter directly on the class, so we overwrite it.
|
|
34
|
+
# We cannot use `super` because the original setter is lost.
|
|
35
|
+
# `instance_variable_set` is required here to actually store the wrapped value.
|
|
36
|
+
instance_variable_set(:@children, wrapped)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Get content for rendering, preferring children over content.
|
|
41
|
+
# When children are all TextContent (plain text), use the content
|
|
42
|
+
# attribute instead since it already has proper spacing between lines.
|
|
43
|
+
def renderable_content
|
|
44
|
+
return content if children.nil? || children.none?
|
|
45
|
+
return content if content && children.all?(TextContent)
|
|
46
|
+
|
|
47
|
+
children
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Flatten renderable_content to a single plain-text string.
|
|
51
|
+
def flat_text
|
|
52
|
+
rc = renderable_content
|
|
53
|
+
case rc
|
|
54
|
+
when String then rc
|
|
55
|
+
when Array then rc.map { |c| extract_child_text(c) }.join
|
|
56
|
+
else rc.to_s
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def extract_child_text(child)
|
|
63
|
+
case child
|
|
64
|
+
when TextContent then child.text
|
|
65
|
+
when String then child
|
|
66
|
+
when Base then child.flat_text
|
|
67
|
+
else child.to_s
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module CoreModel
|
|
5
|
+
# Single-line comment — editorial or hidden notes that do not render.
|
|
6
|
+
#
|
|
7
|
+
# Distinct from {CommentBlock} (multi-line). Round-trip fidelity for the
|
|
8
|
+
# single-line vs. block distinction is preserved across formats that have
|
|
9
|
+
# a single-line comment syntax (AsciiDoc `//`); formats without one (e.g.
|
|
10
|
+
# Markdown) collapse both to `<!-- ... -->`.
|
|
11
|
+
class CommentLine < Base
|
|
12
|
+
def self.semantic_type
|
|
13
|
+
:comment_line
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def body_content?
|
|
17
|
+
false
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
attribute :text, :string
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module CoreModel
|
|
5
|
+
class DefinitionItem < Base
|
|
6
|
+
# Primary term (first term in source order). For multi-term `<dt>`'s
|
|
7
|
+
# (AsciiDoc `term1::\nterm2::`), use {#terms} which carries the
|
|
8
|
+
# full list. `term` is kept as the singular accessor for legacy
|
|
9
|
+
# consumers; new consumers should prefer {#terms}.
|
|
10
|
+
attribute :term, :string
|
|
11
|
+
|
|
12
|
+
# All terms on this `<dt>`. Single-element for the common case;
|
|
13
|
+
# multi-element for shared-`<dd>` AsciiDoc forms like
|
|
14
|
+
# term1::
|
|
15
|
+
# term2::
|
|
16
|
+
# shared definition
|
|
17
|
+
# Order matches source order. Renderers emit one `<dt>` per entry.
|
|
18
|
+
attribute :terms, :string, collection: true, default: []
|
|
19
|
+
|
|
20
|
+
attribute :definitions, :string, collection: true
|
|
21
|
+
attribute :nested, DefinitionList
|
|
22
|
+
# `+`-continuation blocks attached to this dd in AsciiDoc source.
|
|
23
|
+
# Each entry is a CoreModel block (ParagraphBlock, AdmonitionBlock,
|
|
24
|
+
# etc.) that followed the dd after a `+` line — rendered as
|
|
25
|
+
# additional children of the dd.
|
|
26
|
+
attribute :attached_children, Base, collection: true, initialize_empty: true
|
|
27
|
+
|
|
28
|
+
def initialize(args = {})
|
|
29
|
+
@term_children = args.delete(:term_children) || []
|
|
30
|
+
@definition_children = args.delete(:definition_children) || []
|
|
31
|
+
super
|
|
32
|
+
# Backward-compat: if caller passed only `term:` (no `terms:`),
|
|
33
|
+
# seed the collection so #terms is always consistent with #term.
|
|
34
|
+
self.terms = [term].compact if terms.empty? && !term.to_s.empty?
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
attr_reader :term_children, :definition_children
|
|
38
|
+
|
|
39
|
+
def term_children=(value)
|
|
40
|
+
@term_children = value || []
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def definition_children=(value)
|
|
44
|
+
@definition_children = value || []
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def term_renderable
|
|
48
|
+
return term if term_children.nil? || term_children.none?
|
|
49
|
+
return term if term && term_children.all?(String)
|
|
50
|
+
|
|
51
|
+
term_children
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def definition_renderable
|
|
55
|
+
return definitions if definition_children.nil? || definition_children.none?
|
|
56
|
+
return definitions if definition_children.all?(String)
|
|
57
|
+
|
|
58
|
+
definition_children
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private
|
|
62
|
+
|
|
63
|
+
def comparable_attributes
|
|
64
|
+
super + %i[term terms definitions nested term_children definition_children attached_children]
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module CoreModel
|
|
5
|
+
# Represents a definition list
|
|
6
|
+
#
|
|
7
|
+
# DefinitionList contains terms and their definitions.
|
|
8
|
+
#
|
|
9
|
+
# @example Creating a definition list
|
|
10
|
+
# list = CoreModel::DefinitionList.new(
|
|
11
|
+
# items: [
|
|
12
|
+
# DefinitionItem.new(term: "API", definitions: ["Application Programming Interface"]),
|
|
13
|
+
# DefinitionItem.new(term: "REST", definitions: ["Representational State Transfer"])
|
|
14
|
+
# ]
|
|
15
|
+
# )
|
|
16
|
+
class DefinitionList < Base
|
|
17
|
+
# @!attribute items
|
|
18
|
+
# @return [Array<DefinitionItem>] the definition items
|
|
19
|
+
attribute :items, DefinitionItem, collection: true
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def comparable_attributes
|
|
24
|
+
super + %i[items]
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module CoreModel
|
|
5
|
+
# Represents a single attribute (key-value pair) on an element
|
|
6
|
+
#
|
|
7
|
+
# @example
|
|
8
|
+
# attr = ElementAttribute.new(name: "role", value: "note")
|
|
9
|
+
class ElementAttribute < Base
|
|
10
|
+
attribute :name, :string
|
|
11
|
+
attribute :value, :string
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module CoreModel
|
|
5
|
+
# Represents a footnote definition in a document
|
|
6
|
+
#
|
|
7
|
+
# Footnotes are used for auxiliary information, citations, or
|
|
8
|
+
# explanatory notes that appear at the bottom of a page or
|
|
9
|
+
# end of a document.
|
|
10
|
+
#
|
|
11
|
+
# @example Creating a simple footnote
|
|
12
|
+
# footnote = CoreModel::Footnote.new(
|
|
13
|
+
# id: "fn1",
|
|
14
|
+
# content: "This is a footnote explanation."
|
|
15
|
+
# )
|
|
16
|
+
#
|
|
17
|
+
# @example Creating a footnote with backlink
|
|
18
|
+
# footnote = CoreModel::Footnote.new(
|
|
19
|
+
# id: "cite1",
|
|
20
|
+
# content: "Smith, J. (2024). The Reference.",
|
|
21
|
+
# backlink: true
|
|
22
|
+
# )
|
|
23
|
+
class Footnote < Base
|
|
24
|
+
# @!attribute id
|
|
25
|
+
# @return [String, nil] the footnote identifier (e.g., "1", "fn1")
|
|
26
|
+
attribute :id, :string
|
|
27
|
+
|
|
28
|
+
# @!attribute content
|
|
29
|
+
# @return [String, nil] the footnote content
|
|
30
|
+
attribute :content, :string
|
|
31
|
+
|
|
32
|
+
# @!attribute inline_content
|
|
33
|
+
# @return [Array<String>, nil] inline content elements
|
|
34
|
+
attribute :inline_content, :string, collection: true
|
|
35
|
+
|
|
36
|
+
# @!attribute backlink
|
|
37
|
+
# @return [Boolean] whether to include backlink to reference
|
|
38
|
+
attribute :backlink, :boolean, default: -> { true }
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def comparable_attributes
|
|
43
|
+
super + %i[id content backlink]
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Represents an inline footnote reference
|
|
48
|
+
#
|
|
49
|
+
# FootnoteReference links to a Footnote definition within document text.
|
|
50
|
+
#
|
|
51
|
+
# @example Creating a footnote reference
|
|
52
|
+
# ref = CoreModel::FootnoteReference.new(id: "fn1")
|
|
53
|
+
# # Renders as: [^fn1] in Markdown, <sup>1</sup> in HTML
|
|
54
|
+
class FootnoteReference < Base
|
|
55
|
+
# @!attribute id
|
|
56
|
+
# @return [String, nil] the footnote identifier being referenced
|
|
57
|
+
attribute :id, :string
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def comparable_attributes
|
|
62
|
+
super + %i[id]
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Represents an abbreviation definition in a document
|
|
67
|
+
#
|
|
68
|
+
# Abbreviations define the expansion of shortened terms.
|
|
69
|
+
# They are typically rendered with the full definition on first use.
|
|
70
|
+
#
|
|
71
|
+
# @example Creating an abbreviation
|
|
72
|
+
# abbr = CoreModel::Abbreviation.new(
|
|
73
|
+
# term: "API",
|
|
74
|
+
# definition: "Application Programming Interface"
|
|
75
|
+
# )
|
|
76
|
+
class Abbreviation < Base
|
|
77
|
+
# @!attribute term
|
|
78
|
+
# @return [String, nil] the abbreviated term
|
|
79
|
+
attribute :term, :string
|
|
80
|
+
|
|
81
|
+
# @!attribute definition
|
|
82
|
+
# @return [String, nil] the full definition/expansion
|
|
83
|
+
attribute :definition, :string
|
|
84
|
+
|
|
85
|
+
private
|
|
86
|
+
|
|
87
|
+
def comparable_attributes
|
|
88
|
+
super + %i[term definition]
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'yaml'
|
|
4
|
+
|
|
5
|
+
module Coridoc
|
|
6
|
+
module CoreModel
|
|
7
|
+
class FrontmatterBlock
|
|
8
|
+
# Single source of truth for YAML ↔ FrontmatterBlock translation.
|
|
9
|
+
#
|
|
10
|
+
# No other code in any gem may call YAML directly for frontmatter.
|
|
11
|
+
# This isolates permitted-classes configuration and error handling
|
|
12
|
+
# in one MECE location (DRY).
|
|
13
|
+
#
|
|
14
|
+
# The Codec emits flat YAML — values rendered with their natural
|
|
15
|
+
# YAML type. This is what Jekyll, Hugo, VitePress, VuePress, 11ty
|
|
16
|
+
# and every SSG expects: +title: Foo+ / +date: 2024-01-01+.
|
|
17
|
+
# Round-trip fidelity for typed values (Date, Time, Symbol) is
|
|
18
|
+
# preserved by Psych's permitted-classes mechanism, not by a
|
|
19
|
+
# custom discriminator scheme.
|
|
20
|
+
#
|
|
21
|
+
# Values inside the block are stored as the typed
|
|
22
|
+
# FrontmatterEntry / FrontmatterValue tree; ValueBridge below is
|
|
23
|
+
# the only native ↔ typed translator. For the typed-tree
|
|
24
|
+
# representation used by the coridoc-mirror JSON pipeline, see
|
|
25
|
+
# +Coridoc::Mirror::Node::FrontmatterValue+ and
|
|
26
|
+
# +Coridoc::Mirror::Handlers::Frontmatter+.
|
|
27
|
+
module Codec
|
|
28
|
+
PERMITTED_CLASSES = [Date, Time, DateTime, Symbol].freeze
|
|
29
|
+
|
|
30
|
+
# Native Ruby values (what YAML.safe_load returns) ↔ the typed
|
|
31
|
+
# FrontmatterValue tree. Adding a new scalar type is purely
|
|
32
|
+
# additive: declare a typed slot on FrontmatterValue and extend
|
|
33
|
+
# the case below (OCP).
|
|
34
|
+
module ValueBridge
|
|
35
|
+
module_function
|
|
36
|
+
|
|
37
|
+
def native_to_value(native)
|
|
38
|
+
attrs =
|
|
39
|
+
case native
|
|
40
|
+
when nil then { value_type: 'nil' }
|
|
41
|
+
when String then { value_type: 'string', string_value: native }
|
|
42
|
+
when Integer then { value_type: 'integer', integer_value: native }
|
|
43
|
+
when Float then { value_type: 'float', float_value: native }
|
|
44
|
+
when TrueClass, FalseClass
|
|
45
|
+
{ value_type: 'boolean', boolean_value: native }
|
|
46
|
+
when DateTime then { value_type: 'datetime', datetime_value: native }
|
|
47
|
+
when Time then { value_type: 'time', time_value: native }
|
|
48
|
+
when Date then { value_type: 'date', date_value: native }
|
|
49
|
+
when Symbol then { value_type: 'symbol', symbol_value: native }
|
|
50
|
+
when Array
|
|
51
|
+
{ value_type: 'array', items: native.map { |v| native_to_value(v) } }
|
|
52
|
+
when Hash
|
|
53
|
+
{
|
|
54
|
+
value_type: 'map',
|
|
55
|
+
entries: native.map do |k, v|
|
|
56
|
+
FrontmatterEntry.new(key: k.to_s, value: native_to_value(v))
|
|
57
|
+
end
|
|
58
|
+
}
|
|
59
|
+
else
|
|
60
|
+
{ value_type: 'string', string_value: native.to_s }
|
|
61
|
+
end
|
|
62
|
+
FrontmatterValue.new(attrs)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
private_constant :ValueBridge
|
|
66
|
+
|
|
67
|
+
class << self
|
|
68
|
+
# Parse YAML text into a FrontmatterBlock. Returns an empty
|
|
69
|
+
# FrontmatterBlock on malformed YAML or non-Hash payload.
|
|
70
|
+
# Logs a warning so the conversion pipeline can surface the
|
|
71
|
+
# skip rather than silently dropping user-authored content.
|
|
72
|
+
def from_yaml(yaml_text)
|
|
73
|
+
return FrontmatterBlock.new if yaml_text.nil? || yaml_text.strip.empty?
|
|
74
|
+
|
|
75
|
+
build_from_loaded(load_yaml(yaml_text))
|
|
76
|
+
rescue YAML::SyntaxError, Psych::DisallowedClass => e
|
|
77
|
+
Coridoc::Logger.warn("frontmatter parse failed: #{e.message}")
|
|
78
|
+
FrontmatterBlock.new
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Build a FrontmatterBlock from a Ruby hash with native-typed
|
|
82
|
+
# values (String, Integer, Date, …). Returns an empty block
|
|
83
|
+
# for non-Hash input.
|
|
84
|
+
def from_hash(hash)
|
|
85
|
+
return FrontmatterBlock.new unless hash.is_a?(Hash)
|
|
86
|
+
|
|
87
|
+
build_from_loaded(hash)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Typed entries for a native hash — the bridge used by
|
|
91
|
+
# from_hash/from_yaml and by callers constructing blocks
|
|
92
|
+
# entry-by-entry (e.g., the mirror reverse builder).
|
|
93
|
+
def entries_from_hash(hash)
|
|
94
|
+
return [] unless hash.is_a?(Hash)
|
|
95
|
+
|
|
96
|
+
hash.map do |key, value|
|
|
97
|
+
FrontmatterEntry.new(key: key.to_s, value: ValueBridge.native_to_value(value))
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Serialize a FrontmatterBlock to canonical YAML text.
|
|
102
|
+
# Does NOT include leading/trailing +---+ delimiters; the
|
|
103
|
+
# caller wraps the output. Returns +''+ for empty blocks.
|
|
104
|
+
def to_yaml(block)
|
|
105
|
+
return '' unless block.is_a?(FrontmatterBlock)
|
|
106
|
+
|
|
107
|
+
payload = flat_tree(block)
|
|
108
|
+
return '' if payload.empty?
|
|
109
|
+
|
|
110
|
+
YAML.dump(payload).delete_prefix("---\n").delete_suffix("\n...")
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Return the frontmatter entries as a native-typed Ruby hash
|
|
114
|
+
# (the block's data, WITHOUT +$schema+ — the schema attribute
|
|
115
|
+
# is read separately).
|
|
116
|
+
def to_hash(block)
|
|
117
|
+
return {} unless block.is_a?(FrontmatterBlock)
|
|
118
|
+
|
|
119
|
+
block.entries.to_a.each_with_object({}) { |e, h| h[e.key] = e.to_native }
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
private
|
|
123
|
+
|
|
124
|
+
def load_yaml(yaml_text)
|
|
125
|
+
YAML.safe_load(
|
|
126
|
+
yaml_text,
|
|
127
|
+
permitted_classes: PERMITTED_CLASSES,
|
|
128
|
+
aliases: true
|
|
129
|
+
)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def build_from_loaded(loaded)
|
|
133
|
+
return FrontmatterBlock.new unless loaded.is_a?(Hash)
|
|
134
|
+
|
|
135
|
+
FrontmatterBlock.new(
|
|
136
|
+
schema: loaded['$schema']&.to_s,
|
|
137
|
+
entries: entries_from_hash(loaded.except('$schema'))
|
|
138
|
+
)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def flat_tree(block)
|
|
142
|
+
tree = {}
|
|
143
|
+
tree['$schema'] = block.schema if block.schema
|
|
144
|
+
block.entries.to_a.each { |e| tree[e.key] = e.to_native }
|
|
145
|
+
tree
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module CoreModel
|
|
5
|
+
class FrontmatterBlock
|
|
6
|
+
# OCP registry for semantic field transforms applied during
|
|
7
|
+
# format conversion (e.g., `authors` array → `author` string when
|
|
8
|
+
# emitting Markdown for Jekyll).
|
|
9
|
+
#
|
|
10
|
+
# Transforms are directional + format-specific. Each transform
|
|
11
|
+
# declares when it applies and how it rewrites the block's data
|
|
12
|
+
# hash. Never mutates the input — always returns a new block.
|
|
13
|
+
module FieldTransform
|
|
14
|
+
# Base class. Override #applies? and #apply in subclasses.
|
|
15
|
+
class Base
|
|
16
|
+
# Override: return true if this transform should fire for the
|
|
17
|
+
# given direction (:to_format or :from_format) and format
|
|
18
|
+
# (:markdown, :asciidoc, etc.).
|
|
19
|
+
def applies?(direction:, format:) # rubocop:disable Lint/UnusedMethodArgument
|
|
20
|
+
false
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Override: receive a FrontmatterBlock, return a (possibly new)
|
|
24
|
+
# FrontmatterBlock. Never mutate the input.
|
|
25
|
+
def apply(block)
|
|
26
|
+
block
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
protected
|
|
30
|
+
|
|
31
|
+
# Helper: produce a new FrontmatterBlock with transformed
|
|
32
|
+
# data. +data+ is a native Ruby hash (transforms author
|
|
33
|
+
# native shapes); Codec bridges it into the typed tree.
|
|
34
|
+
def rebuild(block, data:)
|
|
35
|
+
FrontmatterBlock.new(
|
|
36
|
+
schema: block.schema,
|
|
37
|
+
entries: Codec.entries_from_hash(data)
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
class Registry
|
|
43
|
+
DEFAULT = new
|
|
44
|
+
|
|
45
|
+
def initialize
|
|
46
|
+
@transforms = []
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def register(transform_class)
|
|
50
|
+
@transforms << transform_class unless @transforms.include?(transform_class)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def count
|
|
54
|
+
@transforms.size
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Apply all registered transforms whose #applies? returns true.
|
|
58
|
+
# Returns a FrontmatterBlock (possibly the same one).
|
|
59
|
+
def apply_all(block, direction:, format:)
|
|
60
|
+
return block unless block.is_a?(FrontmatterBlock)
|
|
61
|
+
|
|
62
|
+
@transforms.reduce(block) do |current, klass|
|
|
63
|
+
transform = klass.new
|
|
64
|
+
if transform.applies?(direction: direction, format: format)
|
|
65
|
+
transform.apply(current)
|
|
66
|
+
else
|
|
67
|
+
current
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module CoreModel
|
|
5
|
+
class FrontmatterBlock
|
|
6
|
+
# A single frontmatter key with its typed value tree. Map
|
|
7
|
+
# containers inside a value hold nested FrontmatterEntry
|
|
8
|
+
# children. Key order across a block's entries is significant
|
|
9
|
+
# (round-trip fidelity) and is preserved by the entries
|
|
10
|
+
# collection.
|
|
11
|
+
class FrontmatterEntry < Base
|
|
12
|
+
attribute :key, :string
|
|
13
|
+
attribute :value, FrontmatterValue
|
|
14
|
+
|
|
15
|
+
# Convenience: the native Ruby value behind this entry.
|
|
16
|
+
def to_native
|
|
17
|
+
value&.to_native
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module CoreModel
|
|
5
|
+
class FrontmatterBlock
|
|
6
|
+
# Single typed value node in a FrontmatterBlock entry tree.
|
|
7
|
+
#
|
|
8
|
+
# Replaces the previous `attribute :data, :hash` representation.
|
|
9
|
+
# Each value carries a `value_type` discriminator plus one populated
|
|
10
|
+
# slot matching that type. Container types (`array`, `map`) hold
|
|
11
|
+
# nested FrontmatterValue / FrontmatterEntry children.
|
|
12
|
+
#
|
|
13
|
+
# Supported value types (mirror what YAML.safe_load returns given
|
|
14
|
+
# the Codec's PERMITTED_CLASSES):
|
|
15
|
+
#
|
|
16
|
+
# scalar -> string, integer, float, boolean, date, datetime, symbol, nil
|
|
17
|
+
# container -> array, map
|
|
18
|
+
#
|
|
19
|
+
# Adding a new scalar type is purely additive: declare a new typed
|
|
20
|
+
# slot and extend the case in Codec::ValueBridge (OCP).
|
|
21
|
+
class FrontmatterValue < Base
|
|
22
|
+
SCALAR_TYPES = %w[
|
|
23
|
+
string integer float boolean date datetime time symbol nil
|
|
24
|
+
].freeze
|
|
25
|
+
CONTAINER_TYPES = %w[array map].freeze
|
|
26
|
+
ALL_TYPES = (SCALAR_TYPES + CONTAINER_TYPES).freeze
|
|
27
|
+
|
|
28
|
+
attribute :value_type, :string
|
|
29
|
+
|
|
30
|
+
# Scalar slots — exactly one populated, selected by value_type.
|
|
31
|
+
attribute :string_value, :string
|
|
32
|
+
attribute :integer_value, :integer
|
|
33
|
+
attribute :float_value, :float
|
|
34
|
+
attribute :boolean_value, :boolean
|
|
35
|
+
attribute :date_value, :date
|
|
36
|
+
attribute :datetime_value, :date_time
|
|
37
|
+
attribute :time_value, :time
|
|
38
|
+
attribute :symbol_value, :symbol
|
|
39
|
+
|
|
40
|
+
# Container slots — populated when value_type is array/map.
|
|
41
|
+
attribute :items, FrontmatterValue, collection: true
|
|
42
|
+
attribute :entries, FrontmatterEntry, collection: true
|
|
43
|
+
|
|
44
|
+
# Convenience: return the Ruby-native scalar value for this node,
|
|
45
|
+
# or nil for containers / nil-typed values. Used by callers that
|
|
46
|
+
# don't care about the type discriminator.
|
|
47
|
+
def ruby_value
|
|
48
|
+
case value_type
|
|
49
|
+
when 'string' then string_value
|
|
50
|
+
when 'integer' then integer_value
|
|
51
|
+
when 'float' then float_value
|
|
52
|
+
when 'boolean' then boolean_value
|
|
53
|
+
when 'date' then date_value
|
|
54
|
+
when 'datetime' then datetime_value
|
|
55
|
+
when 'time' then time_value
|
|
56
|
+
when 'symbol' then symbol_value
|
|
57
|
+
when 'nil' then nil
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Recursive projection back to the native Ruby value this node
|
|
62
|
+
# was built from (the inverse of Codec::ValueBridge's native →
|
|
63
|
+
# typed conversion). Scalars return themselves; arrays and maps
|
|
64
|
+
# rebuild their native shapes.
|
|
65
|
+
def to_native
|
|
66
|
+
case value_type
|
|
67
|
+
when 'array'
|
|
68
|
+
items.to_a.map(&:to_native)
|
|
69
|
+
when 'map'
|
|
70
|
+
entries.to_a.each_with_object({}) { |e, h| h[e.key] = e.to_native }
|
|
71
|
+
else
|
|
72
|
+
ruby_value
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|