coradoc-mirror 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/coradoc/mirror/core_model_to_mirror.rb +181 -0
- data/lib/coradoc/mirror/handler_registry.rb +105 -0
- data/lib/coradoc/mirror/handlers/admonition.rb +29 -0
- data/lib/coradoc/mirror/handlers/bibliography.rb +43 -0
- data/lib/coradoc/mirror/handlers/blockquote.rb +19 -0
- data/lib/coradoc/mirror/handlers/code_block.rb +69 -0
- data/lib/coradoc/mirror/handlers/comment.rb +14 -0
- data/lib/coradoc/mirror/handlers/definition_list.rb +69 -0
- data/lib/coradoc/mirror/handlers/example.rb +19 -0
- data/lib/coradoc/mirror/handlers/footnote.rb +18 -0
- data/lib/coradoc/mirror/handlers/frontmatter.rb +71 -0
- data/lib/coradoc/mirror/handlers/generic_block.rb +24 -0
- data/lib/coradoc/mirror/handlers/horizontal_rule.rb +14 -0
- data/lib/coradoc/mirror/handlers/image.rb +58 -0
- data/lib/coradoc/mirror/handlers/inline.rb +213 -0
- data/lib/coradoc/mirror/handlers/list.rb +80 -0
- data/lib/coradoc/mirror/handlers/open_block.rb +16 -0
- data/lib/coradoc/mirror/handlers/paragraph.rb +16 -0
- data/lib/coradoc/mirror/handlers/reviewer.rb +14 -0
- data/lib/coradoc/mirror/handlers/sidebar.rb +19 -0
- data/lib/coradoc/mirror/handlers/structural.rb +84 -0
- data/lib/coradoc/mirror/handlers/table.rb +82 -0
- data/lib/coradoc/mirror/handlers/toc.rb +48 -0
- data/lib/coradoc/mirror/handlers/verse.rb +22 -0
- data/lib/coradoc/mirror/handlers.rb +38 -0
- data/lib/coradoc/mirror/mark.rb +181 -0
- data/lib/coradoc/mirror/mark_reverse_builder.rb +142 -0
- data/lib/coradoc/mirror/mirror_json_format.rb +42 -0
- data/lib/coradoc/mirror/mirror_to_core_model.rb +73 -0
- data/lib/coradoc/mirror/mirror_yaml_format.rb +41 -0
- data/lib/coradoc/mirror/node.rb +856 -0
- data/lib/coradoc/mirror/output.rb +62 -0
- data/lib/coradoc/mirror/partitioner.rb +62 -0
- data/lib/coradoc/mirror/reverse_builder.rb +600 -0
- data/lib/coradoc/mirror/transformer.rb +41 -0
- data/lib/coradoc/mirror/version.rb +7 -0
- data/lib/coradoc/mirror.rb +161 -0
- data/lib/coradoc-mirror.rb +14 -0
- metadata +140 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'yaml'
|
|
5
|
+
|
|
6
|
+
module Coradoc
|
|
7
|
+
module Mirror
|
|
8
|
+
class Error < StandardError; end
|
|
9
|
+
|
|
10
|
+
autoload :VERSION, "#{__dir__}/mirror/version"
|
|
11
|
+
autoload :Node, "#{__dir__}/mirror/node"
|
|
12
|
+
autoload :Mark, "#{__dir__}/mirror/mark"
|
|
13
|
+
autoload :Transformer, "#{__dir__}/mirror/transformer"
|
|
14
|
+
autoload :CoreModelToMirror, "#{__dir__}/mirror/core_model_to_mirror"
|
|
15
|
+
autoload :MirrorToCoreModel, "#{__dir__}/mirror/mirror_to_core_model"
|
|
16
|
+
autoload :Partitioner, "#{__dir__}/mirror/partitioner"
|
|
17
|
+
# ReverseBuilder's REGISTRY is populated by the built-in builder
|
|
18
|
+
# subclasses (defined inside reverse_builder.rb) at load time. The
|
|
19
|
+
# file is the autoload target, so the registry is full by the time
|
|
20
|
+
# any caller references Coradoc::Mirror::ReverseBuilder.
|
|
21
|
+
autoload :ReverseBuilder, "#{__dir__}/mirror/reverse_builder"
|
|
22
|
+
# MarkReverseBuilder is the mark-level parallel to ReverseBuilder.
|
|
23
|
+
# Same OCP pattern: one Builder class per mark type, self-registering.
|
|
24
|
+
autoload :MarkReverseBuilder, "#{__dir__}/mirror/mark_reverse_builder"
|
|
25
|
+
autoload :HandlerRegistry, "#{__dir__}/mirror/handler_registry"
|
|
26
|
+
autoload :Handlers, "#{__dir__}/mirror/handlers"
|
|
27
|
+
# MirrorJsonFormat and MirrorYamlFormat are loaded via require in
|
|
28
|
+
# coradoc-mirror.rb (side-effect: register_format calls).
|
|
29
|
+
|
|
30
|
+
# Build the default handler registry with all built-in handlers.
|
|
31
|
+
#
|
|
32
|
+
# Third-party code can add handlers to extend the registry without
|
|
33
|
+
# modifying this method (OCP). Each handler maps a CoreModel class
|
|
34
|
+
# to a handler module/class that produces Mirror nodes.
|
|
35
|
+
#
|
|
36
|
+
# @return [HandlerRegistry]
|
|
37
|
+
def self.default_registry
|
|
38
|
+
registry = HandlerRegistry.new
|
|
39
|
+
|
|
40
|
+
# ── Structural ──
|
|
41
|
+
registry.register(CoreModel::DocumentElement, Handlers::Structural,
|
|
42
|
+
method_name: :document)
|
|
43
|
+
registry.register(CoreModel::SectionElement, Handlers::Structural,
|
|
44
|
+
method_name: :section)
|
|
45
|
+
registry.register(CoreModel::PreambleElement, Handlers::Structural,
|
|
46
|
+
method_name: :preamble)
|
|
47
|
+
registry.register(CoreModel::HeaderElement, Handlers::Structural,
|
|
48
|
+
method_name: :header)
|
|
49
|
+
|
|
50
|
+
# ── Paragraphs ──
|
|
51
|
+
registry.register(CoreModel::ParagraphBlock, Handlers::Paragraph)
|
|
52
|
+
|
|
53
|
+
# ── Code / Preformatted ──
|
|
54
|
+
registry.register(CoreModel::SourceBlock, Handlers::CodeBlock,
|
|
55
|
+
method_name: :source)
|
|
56
|
+
registry.register(CoreModel::ListingBlock, Handlers::CodeBlock,
|
|
57
|
+
method_name: :listing)
|
|
58
|
+
registry.register(CoreModel::LiteralBlock, Handlers::CodeBlock,
|
|
59
|
+
method_name: :literal)
|
|
60
|
+
registry.register(CoreModel::PassBlock, Handlers::CodeBlock,
|
|
61
|
+
method_name: :pass)
|
|
62
|
+
|
|
63
|
+
# ── Blocks ──
|
|
64
|
+
registry.register(CoreModel::QuoteBlock, Handlers::Blockquote)
|
|
65
|
+
registry.register(CoreModel::ExampleBlock, Handlers::Example)
|
|
66
|
+
registry.register(CoreModel::SidebarBlock, Handlers::Sidebar)
|
|
67
|
+
registry.register(CoreModel::OpenBlock, Handlers::OpenBlock)
|
|
68
|
+
registry.register(CoreModel::VerseBlock, Handlers::Verse)
|
|
69
|
+
registry.register(CoreModel::CommentBlock, Handlers::Comment)
|
|
70
|
+
registry.register(CoreModel::HorizontalRuleBlock, Handlers::HorizontalRule)
|
|
71
|
+
registry.register(CoreModel::ReviewerBlock, Handlers::Reviewer)
|
|
72
|
+
|
|
73
|
+
# ── Annotations / Admonitions ──
|
|
74
|
+
registry.register(CoreModel::AnnotationBlock, Handlers::Admonition)
|
|
75
|
+
|
|
76
|
+
# ── Lists ──
|
|
77
|
+
registry.register(CoreModel::ListBlock, Handlers::List)
|
|
78
|
+
registry.register(CoreModel::DefinitionList, Handlers::DefinitionList)
|
|
79
|
+
|
|
80
|
+
# ── Tables ──
|
|
81
|
+
registry.register(CoreModel::Table, Handlers::Table)
|
|
82
|
+
|
|
83
|
+
# ── Images ──
|
|
84
|
+
registry.register(CoreModel::Image, Handlers::Image)
|
|
85
|
+
|
|
86
|
+
# ── Inline ──
|
|
87
|
+
registry.register(CoreModel::InlineElement, Handlers::Inline)
|
|
88
|
+
registry.register(CoreModel::TextContent, Handlers::Inline,
|
|
89
|
+
method_name: :text_content)
|
|
90
|
+
|
|
91
|
+
# ── Bibliography ──
|
|
92
|
+
registry.register(CoreModel::Bibliography, Handlers::Bibliography)
|
|
93
|
+
|
|
94
|
+
# ── Footnotes ──
|
|
95
|
+
registry.register(CoreModel::Footnote, Handlers::Footnote)
|
|
96
|
+
registry.register(CoreModel::FootnoteReference, Handlers::Footnote,
|
|
97
|
+
method_name: :reference)
|
|
98
|
+
|
|
99
|
+
# ── TOC ──
|
|
100
|
+
registry.register(CoreModel::Toc, Handlers::Toc)
|
|
101
|
+
|
|
102
|
+
# ── Frontmatter ──
|
|
103
|
+
registry.register(CoreModel::FrontmatterBlock, Handlers::Frontmatter)
|
|
104
|
+
|
|
105
|
+
# ── Generic Block (catch-all for unrecognized block types) ──
|
|
106
|
+
registry.register(CoreModel::Block, Handlers::GenericBlock)
|
|
107
|
+
|
|
108
|
+
registry
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Convenience: transform a CoreModel document to Mirror JSON in one call.
|
|
112
|
+
#
|
|
113
|
+
# @param document [CoreModel::Base] CoreModel document
|
|
114
|
+
# @param registry [HandlerRegistry] handler registry (defaults to built-in)
|
|
115
|
+
# @param partition_structural [Boolean] wrap doc.content in
|
|
116
|
+
# preface/sections/bibliography containers per the @metanorma/mirror JS
|
|
117
|
+
# structural contract (default: false for backward compatibility).
|
|
118
|
+
# @return [Node::Document] mirror document root
|
|
119
|
+
def self.transform(document, registry: default_registry, partition_structural: false)
|
|
120
|
+
CoreModelToMirror.new(registry: registry).call(document, partition_structural: partition_structural)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Convenience: transform and serialize to JSON string.
|
|
124
|
+
#
|
|
125
|
+
# @param document [CoreModel::Base] CoreModel document
|
|
126
|
+
# @param pretty [Boolean] pretty-print JSON (default: false)
|
|
127
|
+
# @param registry [HandlerRegistry] handler registry
|
|
128
|
+
# @return [String] JSON string
|
|
129
|
+
def self.to_json(document, pretty: false, registry: default_registry)
|
|
130
|
+
node = transform(document, registry: registry)
|
|
131
|
+
pretty ? JSON.pretty_generate(node.to_hash) : JSON.generate(node.to_hash)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Convenience: transform and serialize to YAML string.
|
|
135
|
+
#
|
|
136
|
+
# @param document [CoreModel::Base] CoreModel document
|
|
137
|
+
# @param registry [HandlerRegistry] handler registry
|
|
138
|
+
# @return [String] YAML string
|
|
139
|
+
def self.to_yaml(document, registry: default_registry)
|
|
140
|
+
node = transform(document, registry: registry)
|
|
141
|
+
YAML.dump(node.to_hash)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Top-level hash dispatcher: reads `type` and delegates to the
|
|
145
|
+
# matching Node subclass's lutaml-generated `from_hash`. This is a
|
|
146
|
+
# module-level factory — the actual deserialization is done by
|
|
147
|
+
# lutaml on the resolved subclass. Unknown types raise.
|
|
148
|
+
#
|
|
149
|
+
# @param hash [Hash, nil] wire-shape hash
|
|
150
|
+
# @return [Node, nil]
|
|
151
|
+
def self.from_hash(hash)
|
|
152
|
+
return nil if hash.nil?
|
|
153
|
+
|
|
154
|
+
type = hash.is_a?(Hash) ? hash['type'] : nil
|
|
155
|
+
klass_name = Node::TYPE_TO_CLASS[type]
|
|
156
|
+
raise Error, "Unknown mirror node type: #{type.inspect}" unless klass_name
|
|
157
|
+
|
|
158
|
+
Object.const_get(klass_name).from_hash(hash)
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'coradoc'
|
|
4
|
+
require_relative 'coradoc/mirror'
|
|
5
|
+
|
|
6
|
+
# Side-effect: registers output processors with Coradoc::Output pipeline.
|
|
7
|
+
# Side-effect: registers format modules with Coradoc registry.
|
|
8
|
+
# These must use require (not autoload) because they have load-time side effects.
|
|
9
|
+
require_relative 'coradoc/mirror/output'
|
|
10
|
+
require_relative 'coradoc/mirror/mirror_json_format'
|
|
11
|
+
require_relative 'coradoc/mirror/mirror_yaml_format'
|
|
12
|
+
|
|
13
|
+
# Ensure Coradoc error classes are loaded (autoloaded via Coradoc::Error).
|
|
14
|
+
Coradoc::Error
|
metadata
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: coradoc-mirror
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Ribose Inc.
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: coradoc
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '2.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '2.0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: rspec
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: rspec-its
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: simplecov
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0'
|
|
68
|
+
description: Transforms Coradoc CoreModel documents into ProseMirror-compatible JSON/YAML
|
|
69
|
+
suitable for rich frontend rendering with Vue.js, React, or any ProseMirror-based
|
|
70
|
+
editor.
|
|
71
|
+
email:
|
|
72
|
+
- open.source@ribose.com
|
|
73
|
+
executables: []
|
|
74
|
+
extensions: []
|
|
75
|
+
extra_rdoc_files: []
|
|
76
|
+
files:
|
|
77
|
+
- lib/coradoc-mirror.rb
|
|
78
|
+
- lib/coradoc/mirror.rb
|
|
79
|
+
- lib/coradoc/mirror/core_model_to_mirror.rb
|
|
80
|
+
- lib/coradoc/mirror/handler_registry.rb
|
|
81
|
+
- lib/coradoc/mirror/handlers.rb
|
|
82
|
+
- lib/coradoc/mirror/handlers/admonition.rb
|
|
83
|
+
- lib/coradoc/mirror/handlers/bibliography.rb
|
|
84
|
+
- lib/coradoc/mirror/handlers/blockquote.rb
|
|
85
|
+
- lib/coradoc/mirror/handlers/code_block.rb
|
|
86
|
+
- lib/coradoc/mirror/handlers/comment.rb
|
|
87
|
+
- lib/coradoc/mirror/handlers/definition_list.rb
|
|
88
|
+
- lib/coradoc/mirror/handlers/example.rb
|
|
89
|
+
- lib/coradoc/mirror/handlers/footnote.rb
|
|
90
|
+
- lib/coradoc/mirror/handlers/frontmatter.rb
|
|
91
|
+
- lib/coradoc/mirror/handlers/generic_block.rb
|
|
92
|
+
- lib/coradoc/mirror/handlers/horizontal_rule.rb
|
|
93
|
+
- lib/coradoc/mirror/handlers/image.rb
|
|
94
|
+
- lib/coradoc/mirror/handlers/inline.rb
|
|
95
|
+
- lib/coradoc/mirror/handlers/list.rb
|
|
96
|
+
- lib/coradoc/mirror/handlers/open_block.rb
|
|
97
|
+
- lib/coradoc/mirror/handlers/paragraph.rb
|
|
98
|
+
- lib/coradoc/mirror/handlers/reviewer.rb
|
|
99
|
+
- lib/coradoc/mirror/handlers/sidebar.rb
|
|
100
|
+
- lib/coradoc/mirror/handlers/structural.rb
|
|
101
|
+
- lib/coradoc/mirror/handlers/table.rb
|
|
102
|
+
- lib/coradoc/mirror/handlers/toc.rb
|
|
103
|
+
- lib/coradoc/mirror/handlers/verse.rb
|
|
104
|
+
- lib/coradoc/mirror/mark.rb
|
|
105
|
+
- lib/coradoc/mirror/mark_reverse_builder.rb
|
|
106
|
+
- lib/coradoc/mirror/mirror_json_format.rb
|
|
107
|
+
- lib/coradoc/mirror/mirror_to_core_model.rb
|
|
108
|
+
- lib/coradoc/mirror/mirror_yaml_format.rb
|
|
109
|
+
- lib/coradoc/mirror/node.rb
|
|
110
|
+
- lib/coradoc/mirror/output.rb
|
|
111
|
+
- lib/coradoc/mirror/partitioner.rb
|
|
112
|
+
- lib/coradoc/mirror/reverse_builder.rb
|
|
113
|
+
- lib/coradoc/mirror/transformer.rb
|
|
114
|
+
- lib/coradoc/mirror/version.rb
|
|
115
|
+
homepage: https://github.com/metanorma/coradoc
|
|
116
|
+
licenses:
|
|
117
|
+
- MIT
|
|
118
|
+
metadata:
|
|
119
|
+
homepage_uri: https://github.com/metanorma/coradoc
|
|
120
|
+
source_code_uri: https://github.com/metanorma/coradoc
|
|
121
|
+
changelog_uri: https://github.com/metanorma/coradoc/releases
|
|
122
|
+
rubygems_mfa_required: 'true'
|
|
123
|
+
rdoc_options: []
|
|
124
|
+
require_paths:
|
|
125
|
+
- lib
|
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
127
|
+
requirements:
|
|
128
|
+
- - ">="
|
|
129
|
+
- !ruby/object:Gem::Version
|
|
130
|
+
version: 3.3.0
|
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
|
+
requirements:
|
|
133
|
+
- - ">="
|
|
134
|
+
- !ruby/object:Gem::Version
|
|
135
|
+
version: '0'
|
|
136
|
+
requirements: []
|
|
137
|
+
rubygems_version: 3.6.9
|
|
138
|
+
specification_version: 4
|
|
139
|
+
summary: ProseMirror-compatible JSON document model for Coradoc
|
|
140
|
+
test_files: []
|