coridoc 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/Rakefile +8 -0
  4. data/exe/coridoc +24 -0
  5. data/lib/coridoc/binding_inflater.rb +118 -0
  6. data/lib/coridoc/cli.rb +311 -0
  7. data/lib/coridoc/configurable.rb +530 -0
  8. data/lib/coridoc/core_model/abstract_block.rb +11 -0
  9. data/lib/coridoc/core_model/annotation_block.rb +57 -0
  10. data/lib/coridoc/core_model/attribute_reference_resolver.rb +155 -0
  11. data/lib/coridoc/core_model/base.rb +281 -0
  12. data/lib/coridoc/core_model/bibliography.rb +41 -0
  13. data/lib/coridoc/core_model/bibliography_entry.rb +48 -0
  14. data/lib/coridoc/core_model/block.rb +69 -0
  15. data/lib/coridoc/core_model/callout.rb +32 -0
  16. data/lib/coridoc/core_model/callout_text.rb +35 -0
  17. data/lib/coridoc/core_model/children_content.rb +72 -0
  18. data/lib/coridoc/core_model/comment_block.rb +16 -0
  19. data/lib/coridoc/core_model/comment_line.rb +23 -0
  20. data/lib/coridoc/core_model/definition_item.rb +68 -0
  21. data/lib/coridoc/core_model/definition_list.rb +28 -0
  22. data/lib/coridoc/core_model/element_attribute.rb +14 -0
  23. data/lib/coridoc/core_model/example_block.rb +12 -0
  24. data/lib/coridoc/core_model/footnote.rb +92 -0
  25. data/lib/coridoc/core_model/frontmatter/codec.rb +151 -0
  26. data/lib/coridoc/core_model/frontmatter/field_transform.rb +75 -0
  27. data/lib/coridoc/core_model/frontmatter/frontmatter_entry.rb +22 -0
  28. data/lib/coridoc/core_model/frontmatter/frontmatter_value.rb +78 -0
  29. data/lib/coridoc/core_model/frontmatter/schema_resolver.rb +57 -0
  30. data/lib/coridoc/core_model/frontmatter/text_splitter.rb +75 -0
  31. data/lib/coridoc/core_model/frontmatter.rb +72 -0
  32. data/lib/coridoc/core_model/has_children.rb +23 -0
  33. data/lib/coridoc/core_model/horizontal_rule_block.rb +16 -0
  34. data/lib/coridoc/core_model/id_generator.rb +23 -0
  35. data/lib/coridoc/core_model/image.rb +69 -0
  36. data/lib/coridoc/core_model/include.rb +43 -0
  37. data/lib/coridoc/core_model/include_level_offset.rb +71 -0
  38. data/lib/coridoc/core_model/include_options.rb +101 -0
  39. data/lib/coridoc/core_model/inline_content.rb +78 -0
  40. data/lib/coridoc/core_model/inline_element.rb +198 -0
  41. data/lib/coridoc/core_model/list_block.rb +137 -0
  42. data/lib/coridoc/core_model/list_item.rb +129 -0
  43. data/lib/coridoc/core_model/listing_block.rb +19 -0
  44. data/lib/coridoc/core_model/literal_block.rb +16 -0
  45. data/lib/coridoc/core_model/metadata.rb +71 -0
  46. data/lib/coridoc/core_model/open_block.rb +12 -0
  47. data/lib/coridoc/core_model/output_artifact.rb +48 -0
  48. data/lib/coridoc/core_model/paragraph_block.rb +16 -0
  49. data/lib/coridoc/core_model/partintro_block.rb +11 -0
  50. data/lib/coridoc/core_model/pass_block.rb +16 -0
  51. data/lib/coridoc/core_model/quote_block.rb +14 -0
  52. data/lib/coridoc/core_model/raw_inline_element.rb +16 -0
  53. data/lib/coridoc/core_model/reviewer_block.rb +12 -0
  54. data/lib/coridoc/core_model/sidebar_block.rb +12 -0
  55. data/lib/coridoc/core_model/source_block.rb +16 -0
  56. data/lib/coridoc/core_model/stem_block.rb +25 -0
  57. data/lib/coridoc/core_model/structural_element.rb +157 -0
  58. data/lib/coridoc/core_model/table.rb +148 -0
  59. data/lib/coridoc/core_model/term.rb +53 -0
  60. data/lib/coridoc/core_model/text_content.rb +26 -0
  61. data/lib/coridoc/core_model/toc.rb +99 -0
  62. data/lib/coridoc/core_model/toc_generator.rb +151 -0
  63. data/lib/coridoc/core_model/verse_block.rb +14 -0
  64. data/lib/coridoc/core_model.rb +92 -0
  65. data/lib/coridoc/coridoc.rb +164 -0
  66. data/lib/coridoc/dispatch.rb +96 -0
  67. data/lib/coridoc/errors.rb +387 -0
  68. data/lib/coridoc/format_catalog.rb +102 -0
  69. data/lib/coridoc/format_module.rb +78 -0
  70. data/lib/coridoc/hooks.rb +176 -0
  71. data/lib/coridoc/include_resolver/filesystem.rb +84 -0
  72. data/lib/coridoc/include_resolver.rb +67 -0
  73. data/lib/coridoc/include_selectors/indent.rb +54 -0
  74. data/lib/coridoc/include_selectors/level_offset.rb +84 -0
  75. data/lib/coridoc/include_selectors/lines.rb +60 -0
  76. data/lib/coridoc/include_selectors/tags.rb +136 -0
  77. data/lib/coridoc/include_selectors.rb +26 -0
  78. data/lib/coridoc/introspection/element_counter.rb +48 -0
  79. data/lib/coridoc/introspection.rb +72 -0
  80. data/lib/coridoc/link_rewriter/identity.rb +13 -0
  81. data/lib/coridoc/link_rewriter/visitor.rb +148 -0
  82. data/lib/coridoc/link_rewriter.rb +37 -0
  83. data/lib/coridoc/lint/registry.rb +33 -0
  84. data/lib/coridoc/lint/rule.rb +41 -0
  85. data/lib/coridoc/lint/runner.rb +67 -0
  86. data/lib/coridoc/lint/template.rb +190 -0
  87. data/lib/coridoc/lint/violation.rb +16 -0
  88. data/lib/coridoc/lint.rb +44 -0
  89. data/lib/coridoc/logger.rb +54 -0
  90. data/lib/coridoc/performance_regression.rb +121 -0
  91. data/lib/coridoc/pipeline.rb +169 -0
  92. data/lib/coridoc/query.rb +486 -0
  93. data/lib/coridoc/reference/address/anchor.rb +48 -0
  94. data/lib/coridoc/reference/address/doi.rb +40 -0
  95. data/lib/coridoc/reference/address/isbn.rb +39 -0
  96. data/lib/coridoc/reference/address/path.rb +66 -0
  97. data/lib/coridoc/reference/address/scoped_path.rb +47 -0
  98. data/lib/coridoc/reference/address/url.rb +41 -0
  99. data/lib/coridoc/reference/address.rb +153 -0
  100. data/lib/coridoc/reference/catalog/composite.rb +59 -0
  101. data/lib/coridoc/reference/catalog/local.rb +97 -0
  102. data/lib/coridoc/reference/catalog/memory_index.rb +59 -0
  103. data/lib/coridoc/reference/catalog.rb +33 -0
  104. data/lib/coridoc/reference/edge/citation_options.rb +16 -0
  105. data/lib/coridoc/reference/edge/footnote_ref_options.rb +13 -0
  106. data/lib/coridoc/reference/edge/image_ref_options.rb +15 -0
  107. data/lib/coridoc/reference/edge/include_options.rb +15 -0
  108. data/lib/coridoc/reference/edge/kind.rb +72 -0
  109. data/lib/coridoc/reference/edge/link_options.rb +12 -0
  110. data/lib/coridoc/reference/edge/navigation_options.rb +12 -0
  111. data/lib/coridoc/reference/edge/options.rb +13 -0
  112. data/lib/coridoc/reference/edge.rb +80 -0
  113. data/lib/coridoc/reference/edge_search.rb +128 -0
  114. data/lib/coridoc/reference/materializer/base.rb +44 -0
  115. data/lib/coridoc/reference/materializer/passthrough.rb +31 -0
  116. data/lib/coridoc/reference/materializer/registry.rb +116 -0
  117. data/lib/coridoc/reference/materializer.rb +14 -0
  118. data/lib/coridoc/reference/presentation/base.rb +47 -0
  119. data/lib/coridoc/reference/presentation/custom_hierarchy.rb +104 -0
  120. data/lib/coridoc/reference/presentation/page.rb +20 -0
  121. data/lib/coridoc/reference/presentation/single_document.rb +35 -0
  122. data/lib/coridoc/reference/presentation/split_pages.rb +118 -0
  123. data/lib/coridoc/reference/presentation.rb +16 -0
  124. data/lib/coridoc/reference/resolution.rb +184 -0
  125. data/lib/coridoc/reference/resolver/base.rb +15 -0
  126. data/lib/coridoc/reference/resolver/caching.rb +37 -0
  127. data/lib/coridoc/reference/resolver/catalog.rb +69 -0
  128. data/lib/coridoc/reference/resolver/chain.rb +33 -0
  129. data/lib/coridoc/reference/resolver.rb +20 -0
  130. data/lib/coridoc/reference/result/ambiguous.rb +23 -0
  131. data/lib/coridoc/reference/result/base.rb +46 -0
  132. data/lib/coridoc/reference/result/missing.rb +14 -0
  133. data/lib/coridoc/reference/result/resolved.rb +22 -0
  134. data/lib/coridoc/reference/result.rb +20 -0
  135. data/lib/coridoc/reference.rb +65 -0
  136. data/lib/coridoc/registry.rb +156 -0
  137. data/lib/coridoc/relative_path.rb +32 -0
  138. data/lib/coridoc/resolve_includes.rb +202 -0
  139. data/lib/coridoc/source_graph/file_node.rb +147 -0
  140. data/lib/coridoc/source_graph/graph.rb +165 -0
  141. data/lib/coridoc/source_graph/include_edges.rb +33 -0
  142. data/lib/coridoc/source_graph/sidecar.rb +97 -0
  143. data/lib/coridoc/source_graph.rb +36 -0
  144. data/lib/coridoc/validation.rb +686 -0
  145. data/lib/coridoc/version.rb +5 -0
  146. data/lib/coridoc/visitor.rb +283 -0
  147. data/lib/coridoc.rb +41 -0
  148. metadata +221 -0
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module CoreModel
5
+ class FrontmatterBlock
6
+ # OCP registry mapping `$schema` URLs to validator classes.
7
+ #
8
+ # Core ships with NO built-in validators. Downstream gems (e.g.,
9
+ # a future `coridoc-jsonschema`) register resolvers without
10
+ # modifying core code.
11
+ module SchemaResolver
12
+ # Structured validation error. Typed — never a hash bag.
13
+ ValidationError = Struct.new(:field, :message, keyword_init: true)
14
+
15
+ # Base class for schema resolvers. Override #validate in subclasses.
16
+ class Base
17
+ def validate(_block)
18
+ []
19
+ end
20
+ end
21
+
22
+ # Registry of URL → resolver class.
23
+ class Registry
24
+ DEFAULT = new
25
+
26
+ def initialize
27
+ @resolvers = {}
28
+ end
29
+
30
+ def register(schema_url, resolver_class)
31
+ @resolvers[schema_url.to_s] = resolver_class
32
+ end
33
+
34
+ def lookup(schema_url)
35
+ @resolvers[schema_url.to_s]
36
+ end
37
+
38
+ def registered?(schema_url)
39
+ @resolvers.key?(schema_url.to_s)
40
+ end
41
+
42
+ # Returns array of ValidationError structs. Empty if no schema,
43
+ # no resolver, or validation passes.
44
+ def validate(block)
45
+ return [] unless block.is_a?(FrontmatterBlock)
46
+ return [] if block.schema.nil?
47
+
48
+ resolver_class = lookup(block.schema)
49
+ return [] unless resolver_class
50
+
51
+ resolver_class.new.validate(block)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module CoreModel
5
+ class FrontmatterBlock
6
+ # Format-agnostic text splitter for the YAML frontmatter block
7
+ # convention (`---\n...\n---\n` at the very start of a document).
8
+ #
9
+ # Lives under FrontmatterBlock alongside Codec, SchemaResolver,
10
+ # and FieldTransform — together they form the complete frontmatter
11
+ # machinery (MECE):
12
+ #
13
+ # TextSplitter — text → (frontmatter_text, body_text)
14
+ # Codec — frontmatter_text ↔ typed FrontmatterBlock
15
+ # SchemaResolver — typed FrontmatterBlock → validation errors
16
+ # FieldTransform — typed FrontmatterBlock → transformed block
17
+ #
18
+ # Format gems (Markdown, AsciiDoc, ...) call this splitter at the
19
+ # top of their parse pipeline so frontmatter never reaches the
20
+ # format's block parser. Single source of truth (DRY).
21
+ module TextSplitter
22
+ OPEN_DELIMITER = '---'
23
+ CLOSE_DELIMITERS = %w[--- ...].freeze
24
+
25
+ # Result of splitting source text. +frontmatter+ is the raw YAML
26
+ # body (without delimiters), nil if no frontmatter was present.
27
+ # +body+ is the remaining document text.
28
+ Result = Struct.new(:frontmatter, :body, keyword_init: true) do
29
+ def frontmatter?
30
+ !frontmatter.nil? && !frontmatter.empty?
31
+ end
32
+ end
33
+
34
+ class << self
35
+ # @param text [String, nil] Source document text.
36
+ # @return [Result]
37
+ def call(text)
38
+ return Result.new(frontmatter: nil, body: '') if text.nil? || text.empty?
39
+
40
+ lines = text.lines
41
+ return empty_with(text) unless opens_frontmatter?(lines.first)
42
+
43
+ close_index = find_close_line(lines, 1)
44
+ return empty_with(text) if close_index.nil?
45
+
46
+ frontmatter = lines[1...close_index].join
47
+ body = lines[(close_index + 1)..].join
48
+ body = body.sub(/\A\n+/, '') if body.start_with?("\n")
49
+
50
+ Result.new(frontmatter: frontmatter, body: body)
51
+ end
52
+
53
+ private
54
+
55
+ def opens_frontmatter?(first_line)
56
+ return false unless first_line
57
+
58
+ first_line.strip == OPEN_DELIMITER
59
+ end
60
+
61
+ def find_close_line(lines, start_at)
62
+ start_at.upto(lines.size - 1) do |i|
63
+ return i if CLOSE_DELIMITERS.include?(lines[i].strip)
64
+ end
65
+ nil
66
+ end
67
+
68
+ def empty_with(text)
69
+ Result.new(frontmatter: nil, body: text)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module CoreModel
5
+ # First-class block representing YAML frontmatter attached to a
6
+ # document.
7
+ #
8
+ # Frontmatter is modeled as a Block (not a side-attribute on
9
+ # DocumentElement) so it flows through the standard block pipeline:
10
+ # parsers produce it, transformers dispatch on its class, serializers
11
+ # emit it. No special-casing anywhere.
12
+ #
13
+ # Entries are stored as a typed tree (FrontmatterEntry /
14
+ # FrontmatterValue) covering every YAML.safe_load shape — scalars
15
+ # (string, integer, float, boolean, date, datetime, symbol, nil) and
16
+ # containers (array, map). Native-hash (de)serialization is
17
+ # exclusively Codec's job (DRY/MECE); nothing else may build or
18
+ # read a frontmatter hash. Order is preserved for round-trip
19
+ # fidelity.
20
+ #
21
+ # The +$schema+ key, if present in source YAML, is promoted to the
22
+ # +schema+ attribute (single source of truth — DRY); SchemaResolver
23
+ # reads it to find validators.
24
+ class FrontmatterBlock < Block
25
+ def self.semantic_type
26
+ :frontmatter
27
+ end
28
+
29
+ def self.element_type_name
30
+ 'frontmatter'
31
+ end
32
+
33
+ # `$schema` URL, nil-safe. Consumed by SchemaResolver registry.
34
+ attribute :schema, :string
35
+
36
+ # Typed entry tree autoloads — declared before the attribute so
37
+ # the constant resolves; Entry and Value reference each other and
38
+ # the lazy load breaks the cycle.
39
+ autoload :FrontmatterValue, "#{__dir__}/frontmatter/frontmatter_value"
40
+ autoload :FrontmatterEntry, "#{__dir__}/frontmatter/frontmatter_entry"
41
+
42
+ # Parsed YAML frontmatter (minus `$schema`) as a typed entry
43
+ # tree. Build via Codec.from_yaml / Codec.from_hash.
44
+ attribute :entries, FrontmatterEntry, collection: true, default: []
45
+
46
+ # Convenience accessor — native Ruby value for a single key.
47
+ def entry(key)
48
+ found = entries&.find { |e| e.key == key.to_s }
49
+ found&.value&.to_native
50
+ end
51
+
52
+ def has_entry?(key)
53
+ !entries.nil? && entries.any? { |e| e.key == key.to_s }
54
+ end
55
+
56
+ def empty?
57
+ schema.nil? && (entries.nil? || entries.empty?)
58
+ end
59
+
60
+ def body_content?
61
+ false
62
+ end
63
+
64
+ # Sub-namespaces (Codec, SchemaResolver, FieldTransform,
65
+ # TextSplitter) live under FrontmatterBlock and autoload lazily.
66
+ autoload :Codec, "#{__dir__}/frontmatter/codec"
67
+ autoload :SchemaResolver, "#{__dir__}/frontmatter/schema_resolver"
68
+ autoload :FieldTransform, "#{__dir__}/frontmatter/field_transform"
69
+ autoload :TextSplitter, "#{__dir__}/frontmatter/text_splitter"
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module CoreModel
5
+ # Marker module for "this model exposes a +children+ collection".
6
+ #
7
+ # Including this is the canonical way to opt a CoreModel class into
8
+ # children-based traversal. Downstream code (e.g. mirror's
9
+ # CoreModelToMirror#element_children) dispatches on +is_a?(HasChildren)+
10
+ # rather than enumerating subclasses, so adding a new children-bearing
11
+ # class is purely additive (OCP).
12
+ #
13
+ # ChildrenContent (the mixed-content auto-wrap behavior) includes
14
+ # HasChildren, so every class that mixes in ChildrenContent also
15
+ # satisfies HasChildren. Classes that carry typed block children
16
+ # only (StructuralElement, etc.) include HasChildren directly.
17
+ module HasChildren
18
+ def has_children?
19
+ true
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module CoreModel
5
+ # Horizontal rule — a thematic break between sections
6
+ class HorizontalRuleBlock < Block
7
+ def self.semantic_type
8
+ :horizontal_rule
9
+ end
10
+
11
+ def prose?
12
+ false
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module CoreModel
5
+ module IdGenerator
6
+ def self.generate_from_title(title, parent_id: nil)
7
+ return nil if title.nil? || title.to_s.strip.empty?
8
+
9
+ suffix = title.to_s.downcase
10
+ .gsub(/[^a-z0-9\s]/, '')
11
+ .gsub(/\s+/, '_')
12
+ # Trim edge underscore runs without end-anchored quantified
13
+ # patterns (CodeQL rb/polynomial-redos).
14
+ suffix = suffix[1..] while suffix.start_with?('_')
15
+ suffix = suffix[0...-1] while suffix.end_with?('_')
16
+
17
+ return "_#{suffix}" if parent_id.nil? || parent_id.to_s.strip.empty?
18
+
19
+ "#{parent_id}_#{suffix}"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module CoreModel
5
+ # Represents an image in a document
6
+ #
7
+ # Images can be block-level (standalone) or inline. They support
8
+ # various attributes like alt text, dimensions, and linking.
9
+ #
10
+ # @example Creating a block image
11
+ # image = CoreModel::Image.new(
12
+ # src: "images/diagram.png",
13
+ # alt: "System Architecture",
14
+ # caption: "Figure 1: System Overview",
15
+ # width: "800px"
16
+ # )
17
+ #
18
+ # @example Creating an inline image
19
+ # icon = CoreModel::Image.new(
20
+ # src: "icons/warning.png",
21
+ # alt: "Warning",
22
+ # inline: true
23
+ # )
24
+ class Image < Base
25
+ # @!attribute src
26
+ # @return [String, nil] source URL or path to the image
27
+ attribute :src, :string
28
+
29
+ # @!attribute alt
30
+ # @return [String, nil] alternative text for accessibility
31
+ attribute :alt, :string
32
+
33
+ # @!attribute caption
34
+ # @return [String, nil] caption text for the image
35
+ attribute :caption, :string
36
+
37
+ # @!attribute width
38
+ # @return [String, nil] image width (e.g., '100%', '500px')
39
+ attribute :width, :string
40
+
41
+ # @!attribute height
42
+ # @return [String, nil] image height (e.g., '300px', 'auto')
43
+ attribute :height, :string
44
+
45
+ # @!attribute link
46
+ # @return [String, nil] URL to link to when image is clicked
47
+ attribute :link, :string
48
+
49
+ # @!attribute role
50
+ # @return [String, nil] CSS role/class for the image (AsciiDoc role attr)
51
+ attribute :role, :string
52
+
53
+ # @!attribute inline
54
+ # @return [Boolean] whether this is an inline image
55
+ attribute :inline, :boolean, default: -> { false }
56
+
57
+ # @!attribute float
58
+ # @return [String, nil] float position ('left', 'right')
59
+ # @note Not yet wired by any transformer; reserved for future use
60
+ attribute :float, :string
61
+
62
+ private
63
+
64
+ def comparable_attributes
65
+ super + %i[src alt caption width height link role inline]
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module CoreModel
5
+ # First-class include directive node in the canonical document model.
6
+ #
7
+ # An include directive is a LINK from one document to another file.
8
+ # Parsing preserves these nodes verbatim — no file I/O happens during
9
+ # parse. The result is a text graph: a document referencing other
10
+ # documents via Include edges.
11
+ #
12
+ # Splicing the linked content inline is an explicit, separate step:
13
+ # +Coridoc.resolve_includes(doc, base_dir:)+ walks the tree and
14
+ # replaces each Include node with the parsed content of its target,
15
+ # recursing into the result.
16
+ #
17
+ # This separation lets callers:
18
+ # - inspect the graph before deciding to flatten
19
+ # - resolve with different base dirs / resolvers without re-parsing
20
+ # - treat includes as external links (e.g. when parsing a site)
21
+ #
22
+ # Attributes:
23
+ # target String path or URL as authored
24
+ # options IncludeOptions parsed selectors (tags/lines/leveloffset/indent/encoding)
25
+ # raw_options String original bracket body, preserved for verbatim round-trip
26
+ # line_break String trailing line break, default "\n"
27
+ #
28
+ # The node is block-level: it appears in the +content+ / +children+
29
+ # array of any block container (Document, Section, Paragraph, List
30
+ # item, Table cell, etc.) alongside other block-level nodes.
31
+ class Include < Base
32
+ attribute :target, :string
33
+ attribute :options, Coridoc::CoreModel::IncludeOptions,
34
+ default: -> { Coridoc::CoreModel::IncludeOptions.new }
35
+ attribute :raw_options, :string, default: -> { '' }
36
+ attribute :line_break, :string, default: -> { "\n" }
37
+
38
+ def self.semantic_type
39
+ :include
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module CoreModel
5
+ # A leveloffset value parsed from an include directive.
6
+ #
7
+ # asciidoctor supports two forms:
8
+ # leveloffset=+N → relative shift (heading.level += N)
9
+ # leveloffset=-N → relative shift (heading.level -= N)
10
+ # leveloffset=N → absolute set (heading.level = N)
11
+ #
12
+ # The parsed form keeps the mode and delta separate so that the
13
+ # selector that applies the offset does not need to re-parse the
14
+ # string each time it walks a section (DRY).
15
+ class IncludeLevelOffset < Base
16
+ # "relative" (+N/-N) or "absolute" (bare N).
17
+ attribute :mode, :string
18
+
19
+ # Signed integer for relative shifts; the absolute target level
20
+ # for absolute mode.
21
+ attribute :delta, :integer
22
+
23
+ # Construct from a raw asciidoctor-style string ("+2", "-1", "3").
24
+ # Returns nil if the input is nil or unparsable.
25
+ #
26
+ # @param raw [String, nil]
27
+ # @return [IncludeLevelOffset, nil]
28
+ def self.parse(raw)
29
+ return nil if raw.nil? || raw.strip.empty?
30
+
31
+ matched_offset(raw.strip)
32
+ end
33
+
34
+ # Apply this offset to a heading level (1-indexed asciidoctor level).
35
+ #
36
+ # @param level [Integer] original section level
37
+ # @return [Integer] new section level, clamped to >= 0
38
+ def apply(level)
39
+ case mode
40
+ when 'relative' then [level + delta, 0].max
41
+ when 'absolute' then [delta, 0].max
42
+ else level
43
+ end
44
+ end
45
+
46
+ # Render back to the asciidoctor wire form ("+2", "-1", "3").
47
+ #
48
+ # @return [String]
49
+ def to_s
50
+ case mode
51
+ when 'relative' then format('%+d', delta)
52
+ when 'absolute' then delta.to_s
53
+ else ''
54
+ end
55
+ end
56
+
57
+ class << self
58
+ private
59
+
60
+ def matched_offset(trimmed)
61
+ /\A(?<sign>[+-]?)(?<digits>\d+)\z/.match(trimmed) do |m|
62
+ digits = m[:digits].to_i
63
+ signed = m[:sign] == '-' ? -digits : digits
64
+ mode = m[:sign].empty? ? 'absolute' : 'relative'
65
+ new(mode: mode, delta: signed)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module CoreModel
5
+ # Typed options for an include directive, parsed once at construction.
6
+ #
7
+ # Each asciidoctor selector (tags / lines / leveloffset / indent /
8
+ # encoding) gets one typed attribute. Selectors downstream operate on
9
+ # the typed form, never re-parsing the raw string (DRY).
10
+ #
11
+ # tags Array<String> ["body"], [] when unspecified
12
+ # tags_wildcard Boolean true for tags=*
13
+ # tags_inverted Boolean true for tags=**
14
+ # lines_spec String? raw "1..2;5;7..8" — parsed by Lines selector
15
+ # leveloffset IncludeLevelOffset?
16
+ # indent Integer? 0 = strip, N = re-indent, nil = passthrough
17
+ # file_encoding String? passed through to resolver for File.read
18
+ class IncludeOptions < Base
19
+ attribute :tags, :string, collection: true, default: -> { [] }
20
+ attribute :tags_wildcard, :boolean, default: -> { false }
21
+ attribute :tags_inverted, :boolean, default: -> { false }
22
+ attribute :lines_spec, :string
23
+ attribute :leveloffset, Coridoc::CoreModel::IncludeLevelOffset
24
+ attribute :indent, :integer
25
+ attribute :file_encoding, :string
26
+
27
+ # Whether the lines selector is in effect. Tags are ignored when
28
+ # lines is set — matches asciidoctor precedence (SPEC 3.5).
29
+ def lines?
30
+ !lines_spec.nil? && !lines_spec.strip.empty?
31
+ end
32
+
33
+ # Whether any tag selector is in effect.
34
+ def tags?
35
+ tags_wildcard || tags_inverted || !tags.empty?
36
+ end
37
+
38
+ # True when both selectors were specified (lines wins).
39
+ def conflict_resolved_to_lines?
40
+ lines? && tags?
41
+ end
42
+
43
+ # Build from a flat hash of asciidoctor-style key/value strings.
44
+ # Whitespace around keys and values is trimmed (SPEC 6.3).
45
+ #
46
+ # @param attrs [Hash{String=>String}] e.g. {"tags"=>"a;b", "leveloffset"=>"+2"}
47
+ # @return [IncludeOptions]
48
+ def self.from_hash(attrs)
49
+ new(build_args(attrs))
50
+ end
51
+
52
+ class << self
53
+ private
54
+
55
+ def build_args(attrs)
56
+ cleaned = clean_keys(attrs)
57
+ {
58
+ tags: parse_tags(cleaned['tags']),
59
+ tags_wildcard: wildcard?(cleaned['tags']),
60
+ tags_inverted: inverted?(cleaned['tags']),
61
+ lines_spec: cleaned['lines'],
62
+ leveloffset: CoreModel::IncludeLevelOffset.parse(cleaned['leveloffset']),
63
+ indent: parse_integer(cleaned['indent']),
64
+ file_encoding: cleaned['encoding']
65
+ }
66
+ end
67
+
68
+ def clean_keys(attrs)
69
+ attrs.each_with_object({}) do |(k, v), h|
70
+ h[k.to_s.strip] = v.to_s.strip
71
+ end
72
+ end
73
+
74
+ def parse_tags(raw)
75
+ return [] if raw.nil?
76
+
77
+ trimmed = raw.strip
78
+ return [] if trimmed.empty? || trimmed == '*' || trimmed == '**'
79
+
80
+ trimmed.split(';').map(&:strip).reject(&:empty?)
81
+ end
82
+
83
+ def wildcard?(raw)
84
+ !raw.nil? && raw.strip == '*'
85
+ end
86
+
87
+ def inverted?(raw)
88
+ !raw.nil? && raw.strip == '**'
89
+ end
90
+
91
+ def parse_integer(raw)
92
+ return nil if raw.nil? || raw.strip.empty?
93
+
94
+ Integer(raw.strip)
95
+ rescue ArgumentError
96
+ nil
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module CoreModel
5
+ # Operations on mixed arrays of inline content (String /
6
+ # InlineElement / CoreModel::Base). Single source of truth for text
7
+ # extraction and edge cleanup, replacing parallel implementations
8
+ # that previously lived in the HTML converters.
9
+ #
10
+ # No method here mutates its inputs — InlineElements are duplicated
11
+ # via #with_content, Strings are replaced with new instances.
12
+ module InlineContent
13
+ class << self
14
+ # Extract plain text from a mixed content value.
15
+ #
16
+ # nil → '' / String → itself / Array → text_of mapped + joined /
17
+ # InlineElement → #content.to_s / StructuralElement → recurse on
18
+ # #children / other Base → #content if String else #title.to_s /
19
+ # anything else → #to_s.
20
+ def text_of(content)
21
+ return '' if content.nil?
22
+ return content if content.is_a?(String)
23
+ return text_of_one(content) unless content.is_a?(Array)
24
+
25
+ content.map { |item| text_of_one(item) }.join
26
+ end
27
+
28
+ # Return a new array with leading whitespace stripped from the
29
+ # first text-carrying item and trailing whitespace stripped from
30
+ # the last. Inputs are not mutated. Non-Array inputs return
31
+ # unchanged. If no item carries text, returns the input array
32
+ # unchanged.
33
+ def strip_edges(content)
34
+ return content unless content.is_a?(Array)
35
+ return content if content.empty?
36
+
37
+ first_idx = content.index { |i| text_carrier?(i) }
38
+ return content if first_idx.nil?
39
+
40
+ last_idx = content.rindex { |i| text_carrier?(i) }
41
+
42
+ content.map.with_index do |item, idx|
43
+ next item unless text_carrier?(item)
44
+
45
+ stripped = item_text(item)
46
+ stripped = stripped.lstrip if idx == first_idx
47
+ stripped = stripped.rstrip if idx == last_idx
48
+ item.is_a?(String) ? stripped : item.with_content(stripped)
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def text_of_one(item)
55
+ case item
56
+ when String then item
57
+ when CoreModel::InlineElement then item.content.to_s
58
+ when CoreModel::StructuralElement then text_of(Array(item.children))
59
+ when CoreModel::Block
60
+ item.children.is_a?(Array) && !item.children.empty? ? text_of(item.children) : item.content.to_s
61
+ when CoreModel::Base
62
+ item.content.is_a?(String) ? item.content : item.title.to_s
63
+ else
64
+ item.to_s
65
+ end
66
+ end
67
+
68
+ def text_carrier?(item)
69
+ item.is_a?(String) || item.is_a?(CoreModel::InlineElement)
70
+ end
71
+
72
+ def item_text(item)
73
+ item.is_a?(String) ? item : item.content.to_s
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end