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 recognition is delegated to CoreModel::Base#prose? — each block class answers "is my text live or literal?". Adding a new verbatim block type means overriding prose? on it, not editing a list here. The visitor is closed for that change.

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

Constant Summary collapse

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.



46
47
48
# File 'lib/coradoc/link_rewriter/visitor.rb', line 46

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.



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

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

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