Module: Coradoc::Mirror

Defined in:
lib/coradoc/mirror.rb,
lib/coradoc/mirror/mark.rb,
lib/coradoc/mirror/mark.rb,
lib/coradoc/mirror/mark.rb,
lib/coradoc/mirror/node.rb,
lib/coradoc/mirror/node.rb,
lib/coradoc/mirror/node.rb,
lib/coradoc/mirror/output.rb,
lib/coradoc/mirror/version.rb,
lib/coradoc/mirror/handlers.rb,
lib/coradoc/mirror/partitioner.rb,
lib/coradoc/mirror/transformer.rb,
lib/coradoc/mirror/handlers/toc.rb,
lib/coradoc/mirror/handlers/list.rb,
lib/coradoc/mirror/handlers/image.rb,
lib/coradoc/mirror/handlers/table.rb,
lib/coradoc/mirror/handlers/verse.rb,
lib/coradoc/mirror/handlers/inline.rb,
lib/coradoc/mirror/reverse_builder.rb,
lib/coradoc/mirror/handler_registry.rb,
lib/coradoc/mirror/handlers/comment.rb,
lib/coradoc/mirror/handlers/example.rb,
lib/coradoc/mirror/handlers/sidebar.rb,
lib/coradoc/mirror/handlers/footnote.rb,
lib/coradoc/mirror/handlers/reviewer.rb,
lib/coradoc/mirror/handlers/paragraph.rb,
lib/coradoc/mirror/mirror_json_format.rb,
lib/coradoc/mirror/mirror_yaml_format.rb,
lib/coradoc/mirror/handlers/admonition.rb,
lib/coradoc/mirror/handlers/blockquote.rb,
lib/coradoc/mirror/handlers/code_block.rb,
lib/coradoc/mirror/handlers/open_block.rb,
lib/coradoc/mirror/handlers/structural.rb,
lib/coradoc/mirror/core_model_to_mirror.rb,
lib/coradoc/mirror/handlers/frontmatter.rb,
lib/coradoc/mirror/mark_reverse_builder.rb,
lib/coradoc/mirror/mirror_to_core_model.rb,
lib/coradoc/mirror/handlers/bibliography.rb,
lib/coradoc/mirror/handlers/generic_block.rb,
lib/coradoc/mirror/handlers/definition_list.rb,
lib/coradoc/mirror/handlers/horizontal_rule.rb

Defined Under Namespace

Modules: Handlers, MarkReverseBuilder, MirrorJsonFormat, MirrorYamlFormat, Output, Partitioner, ReverseBuilder Classes: CoreModelToMirror, Error, HandlerRegistry, Mark, MirrorToCoreModel, Node, Transformer

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.default_registryHandlerRegistry

Build the default handler registry with all built-in handlers.

Third-party code can add handlers to extend the registry without modifying this method (OCP). Each handler maps a CoreModel class to a handler module/class that produces Mirror nodes.

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/coradoc/mirror.rb', line 37

def self.default_registry
  registry = HandlerRegistry.new

  # ── Structural ──
  registry.register(CoreModel::DocumentElement, Handlers::Structural,
                    method_name: :document)
  registry.register(CoreModel::SectionElement, Handlers::Structural,
                    method_name: :section)
  registry.register(CoreModel::PreambleElement, Handlers::Structural,
                    method_name: :preamble)
  registry.register(CoreModel::HeaderElement, Handlers::Structural,
                    method_name: :header)

  # ── Paragraphs ──
  registry.register(CoreModel::ParagraphBlock, Handlers::Paragraph)

  # ── Code / Preformatted ──
  registry.register(CoreModel::SourceBlock, Handlers::CodeBlock,
                    method_name: :source)
  registry.register(CoreModel::ListingBlock, Handlers::CodeBlock,
                    method_name: :listing)
  registry.register(CoreModel::LiteralBlock, Handlers::CodeBlock,
                    method_name: :literal)
  registry.register(CoreModel::PassBlock, Handlers::CodeBlock,
                    method_name: :pass)

  # ── Blocks ──
  registry.register(CoreModel::QuoteBlock, Handlers::Blockquote)
  registry.register(CoreModel::ExampleBlock, Handlers::Example)
  registry.register(CoreModel::SidebarBlock, Handlers::Sidebar)
  registry.register(CoreModel::OpenBlock, Handlers::OpenBlock)
  registry.register(CoreModel::VerseBlock, Handlers::Verse)
  registry.register(CoreModel::CommentBlock, Handlers::Comment)
  registry.register(CoreModel::HorizontalRuleBlock, Handlers::HorizontalRule)
  registry.register(CoreModel::ReviewerBlock, Handlers::Reviewer)

  # ── Annotations / Admonitions ──
  registry.register(CoreModel::AnnotationBlock, Handlers::Admonition)

  # ── Lists ──
  registry.register(CoreModel::ListBlock, Handlers::List)
  registry.register(CoreModel::DefinitionList, Handlers::DefinitionList)

  # ── Tables ──
  registry.register(CoreModel::Table, Handlers::Table)

  # ── Images ──
  registry.register(CoreModel::Image, Handlers::Image)

  # ── Inline ──
  registry.register(CoreModel::InlineElement, Handlers::Inline)
  registry.register(CoreModel::TextContent, Handlers::Inline,
                    method_name: :text_content)

  # ── Bibliography ──
  registry.register(CoreModel::Bibliography, Handlers::Bibliography)

  # ── Footnotes ──
  registry.register(CoreModel::Footnote, Handlers::Footnote)
  registry.register(CoreModel::FootnoteReference, Handlers::Footnote,
                    method_name: :reference)

  # ── TOC ──
  registry.register(CoreModel::Toc, Handlers::Toc)

  # ── Frontmatter ──
  registry.register(CoreModel::FrontmatterBlock, Handlers::Frontmatter)

  # ── Generic Block (catch-all for unrecognized block types) ──
  registry.register(CoreModel::Block, Handlers::GenericBlock)

  registry
end

.from_hash(hash) ⇒ Node?

Top-level hash dispatcher: reads ‘type` and delegates to the matching Node subclass’s lutaml-generated ‘from_hash`. This is a module-level factory — the actual deserialization is done by lutaml on the resolved subclass. Unknown types raise.

Parameters:

  • hash (Hash, nil)

    wire-shape hash

Returns:

Raises:



151
152
153
154
155
156
157
158
159
# File 'lib/coradoc/mirror.rb', line 151

def self.from_hash(hash)
  return nil if hash.nil?

  type = hash.is_a?(Hash) ? hash['type'] : nil
  klass_name = Node::TYPE_TO_CLASS[type]
  raise Error, "Unknown mirror node type: #{type.inspect}" unless klass_name

  Object.const_get(klass_name).from_hash(hash)
end

.to_json(document, pretty: false, registry: default_registry) ⇒ String

Convenience: transform and serialize to JSON string.

Parameters:

  • document (CoreModel::Base)

    CoreModel document

  • pretty (Boolean) (defaults to: false)

    pretty-print JSON (default: false)

  • registry (HandlerRegistry) (defaults to: default_registry)

    handler registry

Returns:

  • (String)

    JSON string



129
130
131
132
# File 'lib/coradoc/mirror.rb', line 129

def self.to_json(document, pretty: false, registry: default_registry)
  node = transform(document, registry: registry)
  pretty ? JSON.pretty_generate(node.to_hash) : JSON.generate(node.to_hash)
end

.to_yaml(document, registry: default_registry) ⇒ String

Convenience: transform and serialize to YAML string.

Parameters:

  • document (CoreModel::Base)

    CoreModel document

  • registry (HandlerRegistry) (defaults to: default_registry)

    handler registry

Returns:

  • (String)

    YAML string



139
140
141
142
# File 'lib/coradoc/mirror.rb', line 139

def self.to_yaml(document, registry: default_registry)
  node = transform(document, registry: registry)
  YAML.dump(node.to_hash)
end

.transform(document, registry: default_registry, partition_structural: false) ⇒ Node::Document

Convenience: transform a CoreModel document to Mirror JSON in one call.

Parameters:

  • document (CoreModel::Base)

    CoreModel document

  • registry (HandlerRegistry) (defaults to: default_registry)

    handler registry (defaults to built-in)

  • partition_structural (Boolean) (defaults to: false)

    wrap doc.content in preface/sections/bibliography containers per the @metanorma/mirror JS structural contract (default: false for backward compatibility).

Returns:



119
120
121
# File 'lib/coradoc/mirror.rb', line 119

def self.transform(document, registry: default_registry, partition_structural: false)
  CoreModelToMirror.new(registry: registry).call(document, partition_structural: partition_structural)
end