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/frontmatter_query.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/frontmatter_tree_to_hash.rb,
lib/coradoc/mirror/handlers/definition_list.rb,
lib/coradoc/mirror/handlers/horizontal_rule.rb
Defined Under Namespace
Modules: FrontmatterQuery, FrontmatterTreeToHash, Handlers, MarkReverseBuilder, MirrorJsonFormat, MirrorYamlFormat, Output, Partitioner, ReverseBuilder Classes: CoreModelToMirror, Error, HandlerRegistry, Mark, MirrorToCoreModel, Node, Transformer
Constant Summary collapse
- VERSION =
'0.1.2'
Class Method Summary collapse
-
.default_registry ⇒ HandlerRegistry
Build the default handler registry with all built-in handlers.
-
.from_hash(hash) ⇒ Node?
Top-level hash dispatcher: reads ‘type` and delegates to the matching Node subclass’s lutaml-generated ‘from_hash`.
-
.to_json(document, pretty: false, registry: default_registry) ⇒ String
Convenience: transform and serialize to JSON string.
-
.to_yaml(document, registry: default_registry) ⇒ String
Convenience: transform and serialize to YAML string.
-
.transform(document, registry: default_registry, partition_structural: false) ⇒ Node::Document
Convenience: transform a CoreModel document to Mirror JSON in one call.
Class Method Details
.default_registry ⇒ HandlerRegistry
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.
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 110 111 112 113 114 115 116 117 118 |
# File 'lib/coradoc/mirror.rb', line 46 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.
160 161 162 163 164 165 166 167 168 |
# File 'lib/coradoc/mirror.rb', line 160 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.
138 139 140 141 |
# File 'lib/coradoc/mirror.rb', line 138 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.
148 149 150 151 |
# File 'lib/coradoc/mirror.rb', line 148 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.
128 129 130 |
# File 'lib/coradoc/mirror.rb', line 128 def self.transform(document, registry: default_registry, partition_structural: false) CoreModelToMirror.new(registry: registry).call(document, partition_structural: partition_structural) end |