Class: Coradoc::LinkRewriter::Visitor

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/link_rewriter/visitor.rb

Overview

Focused rewriting visitor for the CoreModel tree.

The set of node types that carry link/xref targets is closed and small: LinkElement, CrossReferenceElement, plus generic InlineElement instances whose format_type is ‘link’ or ‘xref’. That classification is owned polymorphically by InlineElement#link_kind — the visitor just calls it. Adding a new link-bearing subclass means overriding link_kind on it, not editing a case/when here (OCP).

Verbatim block types are also closed: SourceBlock, ListingBlock, LiteralBlock, PassBlock, StemBlock. The visitor returns them unchanged so the rewriter never sees link-shaped text that is, in fact, raw code/math.

Dispatch is class-based (no respond_to? duck-typing). Unrecognized classes are returned unchanged — the visitor is closed by design.

Constant Summary collapse

VERBATIM_TYPES =

Verbatim block classes — content is raw, no link semantics.

[
  Coradoc::CoreModel::SourceBlock,
  Coradoc::CoreModel::ListingBlock,
  Coradoc::CoreModel::LiteralBlock,
  Coradoc::CoreModel::PassBlock,
  Coradoc::CoreModel::StemBlock
].freeze
CONTAINER_TYPES =

Structural/container classes that own a child collection. Each entry maps the class to the reader method that exposes its children. MECE — every “recurse into the children” case lands in this table. Exposed as a public constant so spec helpers and other visitors can mirror the same paths without restating the dispatch (DRY).

{
  Coradoc::CoreModel::DocumentElement => :children,
  Coradoc::CoreModel::SectionElement => :children,
  Coradoc::CoreModel::PreambleElement => :children,
  Coradoc::CoreModel::HeaderElement => :children,
  Coradoc::CoreModel::Block => :children,
  Coradoc::CoreModel::ListBlock => :items,
  Coradoc::CoreModel::ListItem => :children,
  Coradoc::CoreModel::Table => :rows,
  Coradoc::CoreModel::TableRow => :cells,
  Coradoc::CoreModel::TableCell => :children,
  Coradoc::CoreModel::DefinitionList => :items,
  Coradoc::CoreModel::Toc => :entries,
  Coradoc::CoreModel::Bibliography => :entries,
  Coradoc::CoreModel::AnnotationBlock => :children
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(rewriter) ⇒ Visitor

Returns a new instance of Visitor.



55
56
57
# File 'lib/coradoc/link_rewriter/visitor.rb', line 55

def initialize(rewriter)
  @rewriter = rewriter
end

Instance Method Details

#visit_document(document) ⇒ Object

Entry point. Always returns a NEW root node — even Identity callers can rely on object identity to confirm the rewrite ran.



61
62
63
64
65
66
# File 'lib/coradoc/link_rewriter/visitor.rb', line 61

def visit_document(document)
  return document unless document.is_a?(Coradoc::CoreModel::Base)

  result = visit_subtree(document)
  result.equal?(document) ? document.dup : result
end