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,486 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ # Document querying and introspection API.
5
+ #
6
+ # Provides CSS-like selectors for navigating and querying document trees.
7
+ module Query
8
+ # Selector parsing and matching
9
+ class Selector
10
+ attr_reader :element_type, :id, :classes, :attributes, :pseudo_classes
11
+
12
+ def self.parse(selector)
13
+ new.parse(selector)
14
+ end
15
+
16
+ def initialize
17
+ @element_type = nil
18
+ @id = nil
19
+ @classes = []
20
+ @attributes = {}
21
+ @pseudo_classes = []
22
+ end
23
+
24
+ def parse(selector)
25
+ @original = selector.to_s.strip
26
+ return self if @original.empty?
27
+
28
+ @original.sub!(/\A([a-z_][a-z0-9_-]*)/i) do |match|
29
+ @element_type = match.downcase
30
+ ''
31
+ end
32
+
33
+ @original.sub!(/#([a-z_])([a-z0-9_-]*)/i) do
34
+ @id = ::Regexp.last_match(1) + ::Regexp.last_match(2)
35
+ ''
36
+ end
37
+
38
+ # Two separate captures: a single class can't disambiguate
39
+ # adjacent overlapping classes ([a-z_] vs [a-z0-9_-] share
40
+ # members), which CodeQL models as polynomial-redos.
41
+ @original.gsub!(/\.([a-z_])([a-z0-9_-]*)/i) do
42
+ @classes << (::Regexp.last_match(1) + ::Regexp.last_match(2))
43
+ ''
44
+ end
45
+
46
+ parse_attribute_pairs
47
+
48
+ parse_pseudo_classes
49
+
50
+ self
51
+ end
52
+
53
+ # Attribute pass without a bracket regex — CodeQL
54
+ # rb/polynomial-redos models /\[([^\]]+)\]/ as polynomial.
55
+ def parse_attribute_pairs
56
+ pos = 0
57
+ while (open_idx = @original.index('[', pos))
58
+ close_idx = @original.index(']', open_idx + 1)
59
+ break if close_idx.nil?
60
+
61
+ if close_idx == open_idx + 1
62
+ pos = close_idx + 1
63
+ next
64
+ end
65
+
66
+ parse_attribute(@original[(open_idx + 1)...close_idx])
67
+ @original.slice!(open_idx, close_idx - open_idx + 1)
68
+ pos = open_idx
69
+ end
70
+ end
71
+
72
+ # Pseudo-class pass without quantified-group nesting — CodeQL
73
+ # rb/polynomial-redos models /:([a-z-]+)(?:\(([^)]+)\))?/ as
74
+ # polynomial. ':' + name chars + optional parens, one scan.
75
+ def parse_pseudo_classes
76
+ pos = 0
77
+ while (colon = @original.index(':', pos))
78
+ j = colon + 1
79
+ j += 1 while j < @original.length && @original[j].match?(/[a-z-]/i)
80
+ name = @original[(colon + 1)...j].downcase
81
+ if name.empty?
82
+ pos = colon + 1
83
+ next
84
+ end
85
+
86
+ arg = nil
87
+ if @original[j] == '('
88
+ close = @original.index(')', j + 1)
89
+ arg = @original[(j + 1)...close] if close
90
+ j = close + 1 if close
91
+ end
92
+ @pseudo_classes << { name: name, argument: arg }
93
+ @original.slice!(colon, j - colon)
94
+ pos = colon
95
+ end
96
+ end
97
+
98
+ def matches?(element)
99
+ return false unless element
100
+ return false if @element_type && !type_matches?(element)
101
+ return false if @id && element.id != @id
102
+ return false if @classes.any? && !classes_match?(element)
103
+ return false if @attributes.any? && !attributes_match?(element)
104
+
105
+ true
106
+ end
107
+
108
+ def matches_pseudo_classes?(element, siblings:, index:)
109
+ @pseudo_classes.all? do |pseudo|
110
+ case pseudo[:name]
111
+ when 'first-child'
112
+ index.zero?
113
+ when 'last-child'
114
+ index == siblings.length - 1
115
+ when 'nth-child'
116
+ n = pseudo[:argument].to_i
117
+ index == n - 1
118
+ when 'only-child'
119
+ siblings.length == 1
120
+ when 'empty'
121
+ empty_element?(element)
122
+ else
123
+ true
124
+ end
125
+ end
126
+ end
127
+
128
+ def universal?
129
+ @element_type == '*' || @original == '*'
130
+ end
131
+
132
+ OPERATORS = { '=' => :equals, '~=' => :includes, '^=' => :starts_with,
133
+ '$=' => :ends_with, '*=' => :contains }.freeze
134
+
135
+ private
136
+
137
+ # Attribute selector parse without overlapping adjacent
138
+ # quantifiers — CodeQL rb/polynomial-redos models the
139
+ # /(\w+)\s*op\s*["']?([^"]']+)['"]?/ family as polynomial.
140
+ # Scan: name word, optional spaces, operator, optional quotes.
141
+ def parse_attribute(expr)
142
+ name = expr[/\A\w+/]
143
+ return if name.nil?
144
+
145
+ rest = expr[name.length..].to_s.lstrip
146
+ op = OPERATORS.keys.find { |candidate| rest.start_with?(candidate) }
147
+ if op.nil?
148
+ @attributes[name.to_sym] = { operator: :present }
149
+ return
150
+ end
151
+
152
+ value = rest[op.length..].to_s.lstrip
153
+ value = value[1..] if value.start_with?('"', "'")
154
+ value = value[0...-1] if value.end_with?('"', "'")
155
+ @attributes[name.to_sym] = { operator: OPERATORS[op], value: value }
156
+ end
157
+
158
+ def type_matches?(element)
159
+ return true if @element_type == '*'
160
+
161
+ return element.element_type.to_s.downcase == @element_type.downcase if (element.is_a?(CoreModel::StructuralElement) || element.is_a?(CoreModel::Block)) && element.element_type
162
+
163
+ class_name = class_to_query_name(element.class)
164
+ class_name == @element_type
165
+ end
166
+
167
+ def class_to_query_name(klass)
168
+ klass.name
169
+ .to_s
170
+ .split('::')
171
+ .last
172
+ .gsub(/([A-Z])/) { "_#{::Regexp.last_match(1).downcase}" }
173
+ .sub(/^_/, '')
174
+ .downcase
175
+ end
176
+
177
+ def classes_match?(element)
178
+ element_classes = if element.is_a?(CoreModel::StructuralElement) && element.element_type
179
+ [element.element_type]
180
+ elsif element.is_a?(CoreModel::Base)
181
+ []
182
+ else
183
+ extract_role(element)
184
+ end
185
+
186
+ @classes.all? { |c| element_classes.include?(c.downcase) }
187
+ end
188
+
189
+ def extract_role(element)
190
+ role = element.public_send(:role)
191
+ role ? role.to_s.split.map(&:downcase) : []
192
+ rescue NoMethodError
193
+ []
194
+ end
195
+
196
+ def attributes_match?(element)
197
+ @attributes.all? do |attr_name, condition|
198
+ value = get_attribute_value(element, attr_name)
199
+ match_attribute_condition(value, condition)
200
+ end
201
+ end
202
+
203
+ def get_attribute_value(element, attr_name)
204
+ case attr_name
205
+ when :id, :title
206
+ element.public_send(attr_name)
207
+ when :level
208
+ if element.is_a?(CoreModel::StructuralElement)
209
+ element.level
210
+ else
211
+ element.public_send(:level)
212
+ end
213
+ when :element_type
214
+ element.element_type if element.is_a?(CoreModel::StructuralElement) || element.is_a?(CoreModel::Block)
215
+ when :type
216
+ element.type if element.is_a?(CoreModel::AnnotationBlock) || element.is_a?(CoreModel::InlineElement)
217
+ else
218
+ element.public_send(attr_name) if element.is_a?(CoreModel::Base) && element.class.attributes.key?(attr_name)
219
+ end
220
+ rescue NoMethodError
221
+ nil
222
+ end
223
+
224
+ def match_attribute_condition(value, condition)
225
+ case condition[:operator]
226
+ when :present
227
+ !value.nil?
228
+ when :equals
229
+ value.to_s == condition[:value]
230
+ when :includes
231
+ value.to_s.split.map(&:downcase).include?(condition[:value].downcase)
232
+ when :starts_with
233
+ value.to_s.start_with?(condition[:value])
234
+ when :ends_with
235
+ value.to_s.end_with?(condition[:value])
236
+ when :contains
237
+ value.to_s.include?(condition[:value])
238
+ else
239
+ false
240
+ end
241
+ end
242
+
243
+ def empty_element?(element)
244
+ return true unless element.is_a?(CoreModel::Block) || element.is_a?(CoreModel::StructuralElement)
245
+
246
+ content = element.content
247
+ case content
248
+ when String
249
+ content.strip.empty?
250
+ when Array
251
+ content.empty?
252
+ else
253
+ content.nil?
254
+ end
255
+ end
256
+ end
257
+
258
+ # Query result set - collection of matched elements
259
+ class ResultSet
260
+ include Enumerable
261
+
262
+ attr_reader :elements
263
+
264
+ def initialize(elements = [])
265
+ @elements = Array(elements).compact
266
+ end
267
+
268
+ def each(&)
269
+ @elements.each(&)
270
+ end
271
+
272
+ def [](index)
273
+ @elements[index]
274
+ end
275
+
276
+ def length
277
+ @elements.length
278
+ end
279
+ alias size length
280
+
281
+ def empty?
282
+ @elements.empty?
283
+ end
284
+
285
+ def first
286
+ @elements.first
287
+ end
288
+
289
+ def last
290
+ @elements.last
291
+ end
292
+
293
+ def filter(selector)
294
+ parsed = Selector.parse(selector)
295
+ filtered = @elements.select { |e| parsed.matches?(e) }
296
+ ResultSet.new(filtered)
297
+ end
298
+
299
+ def query(selector)
300
+ results = @elements.flat_map do |element|
301
+ Query.query_within(element, selector).to_a
302
+ end
303
+ ResultSet.new(results.uniq)
304
+ end
305
+
306
+ def map(&)
307
+ ResultSet.new(@elements.map(&))
308
+ end
309
+
310
+ def select(&)
311
+ ResultSet.new(@elements.select(&))
312
+ end
313
+
314
+ def reject(&)
315
+ ResultSet.new(@elements.reject(&))
316
+ end
317
+
318
+ def to_a
319
+ @elements.dup
320
+ end
321
+
322
+ def inspect
323
+ "#<Coridoc::Query::ResultSet count=#{length}>"
324
+ end
325
+ end
326
+
327
+ # Query engine for executing selectors
328
+ class Engine
329
+ def self.query(document, selector)
330
+ new.query(document, selector)
331
+ end
332
+
333
+ def query(document, selector)
334
+ return ResultSet.new if document.nil? || selector.to_s.strip.empty?
335
+
336
+ return query_multiple(document, selector.split(',').map(&:strip)) if selector.include?(',')
337
+
338
+ return query_with_combinators(document, selector) if selector.include?('>') || selector.include?(' ')
339
+
340
+ parsed = Selector.parse(selector)
341
+ results = []
342
+
343
+ traverse(document) do |element, siblings, index|
344
+ if parsed.matches?(element)
345
+ next if parsed.pseudo_classes.any? && !parsed.matches_pseudo_classes?(element, siblings: siblings,
346
+ index: index)
347
+
348
+ results << element
349
+ end
350
+ end
351
+
352
+ ResultSet.new(results)
353
+ end
354
+
355
+ private
356
+
357
+ def query_multiple(document, selectors)
358
+ results = selectors.flat_map do |sel|
359
+ query(document, sel).to_a
360
+ end
361
+ ResultSet.new(results.uniq)
362
+ end
363
+
364
+ def query_with_combinators(document, selector)
365
+ parts = parse_combinator_selector(selector)
366
+ results = []
367
+
368
+ first_results = query(document, parts[:first])
369
+ return ResultSet.new if first_results.empty?
370
+
371
+ first_results.each do |parent|
372
+ find_matching_descendants(parent, parts[:rest]).each do |match|
373
+ results << match
374
+ end
375
+ end
376
+
377
+ ResultSet.new(results.uniq)
378
+ end
379
+
380
+ def parse_combinator_selector(selector)
381
+ if selector.include?(' > ')
382
+ parts = selector.split(' > ', 2)
383
+ { first: parts[0], rest: [{ combinator: :child, selector: parts[1] }] }
384
+ elsif selector.include?(' ')
385
+ parts = selector.split(' ', 2)
386
+ { first: parts[0], rest: [{ combinator: :descendant, selector: parts[1] }] }
387
+ else
388
+ { first: selector, rest: [] }
389
+ end
390
+ end
391
+
392
+ def find_matching_descendants(parent, parts)
393
+ return [parent] if parts.empty?
394
+
395
+ part = parts.first
396
+ remaining = parts[1..]
397
+
398
+ parsed = Selector.parse(part[:selector])
399
+ results = []
400
+
401
+ siblings = get_children(parent)
402
+ siblings.each_with_index do |child, index|
403
+ case part[:combinator]
404
+ when :child
405
+ results.concat(find_matching_descendants(child, remaining)) if parsed.matches?(child) && pseudo_matches?(
406
+ parsed, child, siblings, index
407
+ )
408
+ when :descendant
409
+ results.concat(find_matching_descendants(child, remaining)) if parsed.matches?(child) && pseudo_matches?(
410
+ parsed, child, siblings, index
411
+ )
412
+ results.concat(find_matching_descendants(child, parts))
413
+ end
414
+ end
415
+
416
+ results
417
+ end
418
+
419
+ def pseudo_matches?(parsed, element, siblings, index)
420
+ return true if parsed.pseudo_classes.empty?
421
+
422
+ parsed.matches_pseudo_classes?(element, siblings: siblings, index: index)
423
+ end
424
+
425
+ def traverse(element, siblings: [], index: 0, &block)
426
+ return unless element
427
+
428
+ yield(element, siblings, index)
429
+
430
+ children = get_children(element)
431
+ children.each_with_index do |child, i|
432
+ traverse(child, siblings: children, index: i, &block)
433
+ end
434
+ end
435
+
436
+ def get_children(element)
437
+ Query.get_children(element)
438
+ end
439
+ end
440
+
441
+ # Module-level query methods
442
+ class << self
443
+ def query(document, selector)
444
+ Engine.query(document, selector)
445
+ end
446
+
447
+ def query_within(element, selector)
448
+ parsed = Selector.parse(selector)
449
+ results = []
450
+
451
+ traverse_children(element) do |child, siblings, index|
452
+ if parsed.matches?(child)
453
+ next if parsed.pseudo_classes.any? && !parsed.matches_pseudo_classes?(child, siblings: siblings,
454
+ index: index)
455
+
456
+ results << child
457
+ end
458
+ end
459
+
460
+ ResultSet.new(results)
461
+ end
462
+
463
+ def get_children(element)
464
+ return [] unless element
465
+
466
+ if element.is_a?(CoreModel::StructuralElement) && element.children&.any?
467
+ element.children
468
+ elsif element.is_a?(CoreModel::Block) && element.content
469
+ Array(element.content).select { |c| c.is_a?(CoreModel::Base) }
470
+ else
471
+ []
472
+ end
473
+ end
474
+
475
+ private
476
+
477
+ def traverse_children(element, siblings: [], index: 0, &block)
478
+ children = get_children(element)
479
+ children.each_with_index do |child, i|
480
+ yield(child, children, i)
481
+ traverse_children(child, &block)
482
+ end
483
+ end
484
+ end
485
+ end
486
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Reference
5
+ class Address < Lutaml::Model::Serializable
6
+ # In-document anchor — the most common navigation target.
7
+ # Matches raw strings starting with "#" plus bareword names that
8
+ # don't look like document IDs or external URLs.
9
+ #
10
+ # "#intro" => anchor target "intro"
11
+ # "intro" => anchor target "intro"
12
+ # "footnote-1" => anchor target "footnote-1"
13
+ module Anchor
14
+ module_function
15
+
16
+ def scheme_name
17
+ :anchor
18
+ end
19
+
20
+ def matches?(raw)
21
+ return false if raw.nil? || raw.empty?
22
+
23
+ stripped = raw.to_s
24
+ return false if stripped == '#'
25
+ return true if stripped.start_with?('#')
26
+
27
+ stripped.match?(/\A[\w.\-]+\z/)
28
+ end
29
+
30
+ def parse(raw)
31
+ target = raw.to_s.delete_prefix('#')
32
+ new_address(target: target)
33
+ end
34
+
35
+ def serialize(address)
36
+ target = address.target
37
+ return '' if target.nil? || target.empty?
38
+
39
+ "##{target}"
40
+ end
41
+
42
+ def new_address(target:)
43
+ Address.new(scheme: 'anchor', target: target)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Reference
5
+ class Address < Lutaml::Model::Serializable
6
+ # DOI — Digital Object Identifier. Regex per the DOI handbook.
7
+ module Doi
8
+ module_function
9
+
10
+ DOI_PATTERN = %r{\A(10\.\d{4,}/[^\s#]+)(?:#(.*))?\z}
11
+
12
+ def scheme_name
13
+ :doi
14
+ end
15
+
16
+ def matches?(raw)
17
+ return false if raw.nil? || raw.empty?
18
+
19
+ DOI_PATTERN.match?(raw.to_s)
20
+ end
21
+
22
+ def parse(raw)
23
+ match = DOI_PATTERN.match(raw.to_s)
24
+ raise Address::ParseError, "Invalid DOI: #{raw.inspect}" unless match
25
+
26
+ target = match[1]
27
+ fragment = match[2]
28
+ Address.new(scheme: 'doi', target: target, fragment: fragment)
29
+ end
30
+
31
+ def serialize(address)
32
+ base = address.target.to_s
33
+ return base unless address.fragment && !address.fragment.empty?
34
+
35
+ "#{base}##{address.fragment}"
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Reference
5
+ class Address < Lutaml::Model::Serializable
6
+ # ISBN — book identifier. Recognizes an optional "ISBN" prefix
7
+ # (case insensitive) and either ISBN-10 or ISBN-13 digits. Bare
8
+ # numbers must be long enough to be ISBN-shaped so short numeric
9
+ # anchors are not claimed.
10
+ module Isbn
11
+ module_function
12
+
13
+ ISBN_PATTERN = /\A(?:ISBN\s+)?(\d[\d\-Xx]{8,16})\z/i
14
+
15
+ def scheme_name
16
+ :isbn
17
+ end
18
+
19
+ def matches?(raw)
20
+ return false if raw.nil? || raw.empty?
21
+
22
+ ISBN_PATTERN.match?(raw.to_s)
23
+ end
24
+
25
+ def parse(raw)
26
+ match = ISBN_PATTERN.match(raw.to_s)
27
+ raise Address::ParseError, "Invalid ISBN: #{raw.inspect}" unless match
28
+
29
+ target = match[1]
30
+ Address.new(scheme: 'isbn', target: target)
31
+ end
32
+
33
+ def serialize(address)
34
+ "ISBN #{address.target}"
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Reference
5
+ class Address < Lutaml::Model::Serializable
6
+ # Inter-document path — a document identifier plus optional fragment.
7
+ # Matches document-ID-shaped barewords (uppercase + dash + digit,
8
+ # e.g. "ELF-5005-1") and relative/absolute file paths
9
+ # ("images/foo.png", "../shared/common.adoc"). Locality is carried
10
+ # in fragment.
11
+ #
12
+ # "ELF-5005-1" => path target "ELF-5005-1"
13
+ # "ELF-5005-1#sec-3" => path target "ELF-5005-1", fragment "sec-3"
14
+ # "images/foo.png" => path target "images/foo.png"
15
+ module Path
16
+ module_function
17
+
18
+ # Doc-ID pattern: uppercase-led bareword ("ELF-5005-1"). Used
19
+ # together with a separate digit scan — two linear passes, no
20
+ # ambiguous adjacent quantifiers (polynomial-ReDoS-safe).
21
+ DOC_ID_PATTERN = /\A[A-Z][A-Z0-9_\-]*\z/
22
+ # Loose pattern used by +parse+ when the scheme is already chosen
23
+ # (via hint). Accepts any non-empty target plus optional fragment.
24
+ LOOSE_PATTERN = /\A([^#]+)(?:#(.*))?\z/
25
+
26
+ def scheme_name
27
+ :path
28
+ end
29
+
30
+ def matches?(raw)
31
+ return false if raw.nil? || raw.empty?
32
+
33
+ value = raw.to_s
34
+ return true if value.include?('/')
35
+
36
+ # Document IDs are uppercase-led and contain at least one digit
37
+ # (what distinguishes them from anchors like "SECTION").
38
+ id_part = value.split('#', 2).first
39
+ DOC_ID_PATTERN.match?(id_part) && id_part.match?(/\d/)
40
+ end
41
+
42
+ def parse(raw)
43
+ match = LOOSE_PATTERN.match(raw.to_s)
44
+ raise Address::ParseError, "Invalid path: #{raw.inspect}" unless match
45
+
46
+ Address.new(
47
+ scheme: 'path',
48
+ target: match[1],
49
+ fragment: normalize_fragment(match[2])
50
+ )
51
+ end
52
+
53
+ def serialize(address)
54
+ base = address.target.to_s
55
+ return base unless address.fragment && !address.fragment.empty?
56
+
57
+ "#{base}##{address.fragment}"
58
+ end
59
+
60
+ def normalize_fragment(fragment)
61
+ fragment&.empty? ? nil : fragment
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end