Class: Coradoc::Mirror::MirrorToCoreModel

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/mirror/mirror_to_core_model.rb

Overview

Transforms ProseMirror-compatible Mirror nodes back into CoreModel.

Dispatch is delegated to ReverseBuilder (node-level) and MarkReverseBuilder (mark-level) — adding a new node or mark type is done by registering a new Builder class, with no edit to this file (OCP). ReverseBuilder and MarkReverseBuilder are autoloaded from coradoc/mirror.rb; referencing them here triggers load of the registry files, which is where the built-in builders self-register.

Instance Method Summary collapse

Instance Method Details

#apply_mark(inner, mark) ⇒ Object

Mark dispatch goes through MarkReverseBuilder so adding a new mark type is purely additive (OCP parity with node dispatch). Unknown marks pass ‘inner` through unchanged.



42
43
44
45
46
47
# File 'lib/coradoc/mirror/mirror_to_core_model.rb', line 42

def apply_mark(inner, mark)
  builder_class = MarkReverseBuilder.lookup(mark.type)
  return inner unless builder_class

  builder_class.new.build(inner, mark)
end

#build_content(node) ⇒ Object

── Shared helpers (single source of truth — used by every ReverseBuilder::Base subclass via delegation) ──



28
29
30
31
32
33
34
35
36
37
# File 'lib/coradoc/mirror/mirror_to_core_model.rb', line 28

def build_content(node)
  return [] unless node.content

  node.content.flat_map do |child|
    result = build_node(child)
    next [] if result.nil?

    result.is_a?(Array) ? result : [result]
  end
end

#build_inline_children(node) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/coradoc/mirror/mirror_to_core_model.rb', line 49

def build_inline_children(node)
  return [] unless node.content

  node.content.filter_map do |child|
    next unless child.is_a?(Node)

    build_node(child)
  end
end

#build_node(node) ⇒ Object

Raises:



18
19
20
21
22
23
# File 'lib/coradoc/mirror/mirror_to_core_model.rb', line 18

def build_node(node)
  builder_class = ReverseBuilder.lookup(node.type)
  raise Error, "Unknown mirror node type: #{node.type}" unless builder_class

  builder_class.new(self).build(node)
end

#call(mirror_node) ⇒ Object



14
15
16
# File 'lib/coradoc/mirror/mirror_to_core_model.rb', line 14

def call(mirror_node)
  build_node(mirror_node)
end

#extract_text(node) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/coradoc/mirror/mirror_to_core_model.rb', line 59

def extract_text(node)
  return node.text.to_s if node.is_a?(Node::Text)
  return '' unless node.content

  node.content.filter_map do |child|
    child.is_a?(Node) ? extract_text(child) : ''
  end.join
end

#inline_content(element) ⇒ Object



68
69
70
# File 'lib/coradoc/mirror/mirror_to_core_model.rb', line 68

def inline_content(element)
  element.is_a?(CoreModel::InlineElement) ? element.content : element.to_s
end