coradoc-markdown 1.0.14 → 1.0.15

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97e2e902cb727d13b70afa875d8dc11d43c993ed9bf776e1a531ade9d4da91eb
4
- data.tar.gz: c2a5e5ea362bb459ca1fad91355542dc5b2038d6b3493595280c415470b5f822
3
+ metadata.gz: d0a7fd1a87349725905a318a44d33ec43156872195415b01183974ea70862e04
4
+ data.tar.gz: bb2f9298ab83f661e321676cb44d628d708bd4ef75d5a0219921a1020af93260
5
5
  SHA512:
6
- metadata.gz: 953cdfa792f3ef195ed0349b771f6f4b75235e5da771ad94120dd4e2cad127fe691b98b4a7732e93e9c509670d2a9d7dadf0102960032f1e82459deec8030007
7
- data.tar.gz: f89b2852646869758f31aaa3ce794e05955ed466e7210544f9325fa138a06c8f5fe6a9fa23df05a1a8da4e38283b5fc9f113900663ea6d690036bfedc34f7684
6
+ metadata.gz: b26bb83310278e00937a39ad8a1c7bcf6926a186a378e2bf1e379ff5f411fe3f5aaf10c0fc032a1e81502d5bd134b7e2995bd358241887dfb808507519b41ebb
7
+ data.tar.gz: 8f2b5fbfb9bef49eda15e7f3ac8dd7c09f4f992c556c0bed9a08f4902303fdfbd69261d41ea97df6cb760c55100d4c8c58ebee0526160e1d949e47d69bd29de5
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coradoc
4
+ module Markdown
5
+ module Behaviors
6
+ # Hand-written behavior for Admonition. Prepended (not included) so
7
+ # that the setter can call `super` into the attribute setter that
8
+ # lutaml-model defines on the class itself.
9
+ module Admonition
10
+ ALLOWED_TYPES = %w[note tip warning important caution].freeze
11
+
12
+ # Normalize type on assignment. Callers can pass "NOTE", "Note",
13
+ # or "note" — storage is always lowercase so downstream comparison
14
+ # and CSS class generation don't need to repeat the normalization.
15
+ def admonition_type=(value)
16
+ super(value.to_s.downcase)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coradoc
4
+ module Markdown
5
+ module Behaviors
6
+ # Hand-written behavior for AttributeList (IAL/ALD parsing, merging).
7
+ module AttributeList
8
+ def self.prepended(base)
9
+ base.extend(ClassMethods)
10
+ end
11
+
12
+ # Parsing entry points.
13
+ module ClassMethods
14
+ # Parse an IAL string into an AttributeList
15
+ # @param str [String] The IAL string (e.g., '{:.class #id key="val"}')
16
+ # @return [AttributeList] Parsed attribute list
17
+ def parse(str)
18
+ return nil if str.nil? || str.empty?
19
+
20
+ # Remove surrounding braces
21
+ content = str.strip.gsub(/\A\{?:?|\}?\z/, '')
22
+
23
+ attr_list = new
24
+
25
+ # Check for ALD (has a name before colon)
26
+ if content =~ /\A(\w+):\s*/
27
+ attr_list.name = ::Regexp.last_match(1)
28
+ content = ::Regexp.last_match.post_match
29
+ end
30
+
31
+ # Parse the content
32
+ parse_attributes(content, attr_list)
33
+
34
+ attr_list
35
+ end
36
+
37
+ def parse_attributes(content, attr_list)
38
+ # Use shared IalParser for consistent parsing
39
+ ParserUtil::IalParser.tokenize(content).each do |token|
40
+ case token[:type]
41
+ when :class
42
+ attr_list.classes << token[:value]
43
+ when :id
44
+ attr_list.id = token[:value]
45
+ when :attribute
46
+ attr_list.attributes << NamedValue.new(name: token[:key], value: token[:value])
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ # Merge another AttributeList into this one
53
+ # @param other [AttributeList] The other attribute list to merge
54
+ # @return [AttributeList] self for chaining
55
+ def merge!(other)
56
+ return self unless other
57
+
58
+ self.id = other.id if other.id
59
+ self.classes = (classes + other.classes).uniq
60
+ merged = {}
61
+ (attributes + other.attributes).each { |nv| merged[nv.name] = nv }
62
+ self.attributes = merged.values
63
+ self
64
+ end
65
+
66
+ # Create a merged copy
67
+ # @param other [AttributeList] The other attribute list to merge
68
+ # @return [AttributeList] New merged attribute list
69
+ def merge(other)
70
+ dup.merge!(other)
71
+ end
72
+
73
+ # Check if this has any attributes
74
+ # @return [Boolean]
75
+ def empty?
76
+ id.nil? && classes.empty? && attributes.empty?
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coradoc
4
+ module Markdown
5
+ module Behaviors
6
+ # Hand-written behavior for Document: index access and construction.
7
+ module Document
8
+ def self.prepended(base)
9
+ base.extend(ClassMethods)
10
+ end
11
+
12
+ # Constructors for Document.
13
+ module ClassMethods
14
+ # Create a document from an array of blocks
15
+ def from_ast(elements)
16
+ new(blocks: elements)
17
+ end
18
+ end
19
+
20
+ # @param [Integer] index The index of the block to retrieve
21
+ # @return [Coradoc::Markdown::Base] The block at the specified index
22
+ def [](index)
23
+ blocks[index]
24
+ end
25
+
26
+ # @param [Integer] index The index of the block to set
27
+ # @param [Coradoc::Markdown::Base] value The block to set
28
+ def []=(index, value)
29
+ blocks[index] = value
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coradoc
4
+ module Markdown
5
+ module Behaviors
6
+ # Hand-written behavior for the kramdown Extension model.
7
+ module Extension
8
+ # Known extension types
9
+ TYPES = {
10
+ toc: :toc,
11
+ options: :options,
12
+ comment: :comment,
13
+ nomarkdown: :nomarkdown,
14
+ ignore: :ignore,
15
+ if: :conditional,
16
+ endif: :conditional
17
+ }.freeze
18
+
19
+ def self.prepended(base)
20
+ base.extend(ClassMethods)
21
+ end
22
+
23
+ # Factory helpers for common extensions.
24
+ module ClassMethods
25
+ # @param options [Hash] TOC options (levels, etc.)
26
+ def toc(options = {})
27
+ new(name: :toc, options: hash_to_named(options))
28
+ end
29
+
30
+ # @param options [Hash] Parser options
31
+ def options(options = {})
32
+ new(name: :options, options: hash_to_named(options))
33
+ end
34
+
35
+ # @param content [String] Comment content
36
+ def comment(content = '')
37
+ new(name: :comment, content: content)
38
+ end
39
+
40
+ # @param content [String] Raw content to pass through
41
+ def nomarkdown(content)
42
+ new(name: :nomarkdown, content: content)
43
+ end
44
+
45
+ private
46
+
47
+ def hash_to_named(hash)
48
+ return [] if hash.nil? || hash.empty?
49
+
50
+ hash.map { |k, v| NamedValue.new(name: k.to_s, value: v.to_s) }
51
+ end
52
+ end
53
+
54
+ # Check if this is a specific extension type
55
+ # @param type [Symbol] The extension type
56
+ # @return [Boolean]
57
+ def type?(type)
58
+ name.to_sym == type
59
+ end
60
+
61
+ # Check if this is a self-closing extension
62
+ # @return [Boolean]
63
+ def self_closing?
64
+ content.nil? || content.empty?
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coradoc
4
+ module Markdown
5
+ module Behaviors
6
+ # Hand-written behavior for Heading, shared by the hand-written and
7
+ # the schema-generated class.
8
+ module Heading
9
+ # Generate an auto ID from the heading text
10
+ #
11
+ # @return [String] A slugified version of the text suitable for use as an ID
12
+ # @example
13
+ # Heading.new(text: "Hello World!").auto_id #=> "hello-world"
14
+ def auto_id
15
+ return '' if text.nil? || text.empty?
16
+
17
+ slug = text.to_s
18
+ .downcase
19
+ .gsub(/[^a-z0-9]+/, '-')
20
+ .gsub(/^-+|-+$/, '')
21
+ slug.empty? ? 'section' : slug
22
+ end
23
+
24
+ # Get the ID for this heading (uses explicit id if set, otherwise auto_id)
25
+ #
26
+ # @return [String] The ID to use for this heading
27
+ def heading_id
28
+ id || auto_id
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coradoc
4
+ module Markdown
5
+ module Behaviors
6
+ # Hand-written behavior for Math (block or inline).
7
+ module Math
8
+ def self.prepended(base)
9
+ base.extend(ClassMethods)
10
+ end
11
+
12
+ # Constructors for the two math flavors.
13
+ module ClassMethods
14
+ # @param content [String] The math content
15
+ # @return [Coradoc::Markdown::Math]
16
+ def inline(content)
17
+ new(content: content, inline: true)
18
+ end
19
+
20
+ def block(content)
21
+ new(content: content, inline: false)
22
+ end
23
+ end
24
+
25
+ # @return [Boolean]
26
+ def inline?
27
+ inline == true
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coradoc
4
+ module Markdown
5
+ module Behaviors
6
+ # Hand-written behavior for Text.
7
+ module Text
8
+ def to_s
9
+ content
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coradoc
4
+ module Markdown
5
+ # Hand-written behavior modules, one per model that needs methods beyond
6
+ # its attributes. Both the hand-written models and the schema-generated
7
+ # classes prepend the same module, so behavior has a single source.
8
+ module Behaviors
9
+ autoload :AttributeList, 'coradoc/markdown/behaviors/attribute_list'
10
+ autoload :Admonition, 'coradoc/markdown/behaviors/admonition'
11
+ autoload :Document, 'coradoc/markdown/behaviors/document'
12
+ autoload :Extension, 'coradoc/markdown/behaviors/extension'
13
+ autoload :Heading, 'coradoc/markdown/behaviors/heading'
14
+ autoload :Math, 'coradoc/markdown/behaviors/math'
15
+ autoload :Text, 'coradoc/markdown/behaviors/text'
16
+ end
17
+ end
18
+ end
@@ -18,8 +18,6 @@ module Coradoc
18
18
  # Type is stored lowercase. Content is raw Markdown text (already
19
19
  # serialized). Title is optional.
20
20
  class Admonition < Base
21
- ALLOWED_TYPES = %w[note tip warning important caution].freeze
22
-
23
21
  attribute :admonition_type, :string
24
22
  attribute :content, :string
25
23
  attribute :title, :string
@@ -29,12 +27,7 @@ module Coradoc
29
27
  # references, code spans, etc. When empty, fall back to `content`.
30
28
  attribute :children, Coradoc::Markdown::Base, collection: true, default: []
31
29
 
32
- # Normalize type on assignment. Callers can pass "NOTE", "Note",
33
- # or "note" — storage is always lowercase so downstream comparison
34
- # and CSS class generation don't need to repeat the normalization.
35
- def admonition_type=(value)
36
- super(value.to_s.downcase)
37
- end
30
+ prepend Behaviors::Admonition
38
31
  end
39
32
  end
40
33
  end
@@ -19,69 +19,7 @@ module Coradoc
19
19
  attribute :attributes, NamedValue, collection: true, default: []
20
20
  attribute :name, :string # For ALD - the reference name
21
21
 
22
- # Parse an IAL string into an AttributeList
23
- # @param str [String] The IAL string (e.g., '{:.class #id key="val"}')
24
- # @return [AttributeList] Parsed attribute list
25
- def self.parse(str)
26
- return nil if str.nil? || str.empty?
27
-
28
- # Remove surrounding braces
29
- content = str.strip.gsub(/\A\{?:?|\}?\z/, '')
30
-
31
- attr_list = new
32
-
33
- # Check for ALD (has a name before colon)
34
- if content =~ /\A(\w+):\s*/
35
- attr_list.name = ::Regexp.last_match(1)
36
- content = ::Regexp.last_match.post_match
37
- end
38
-
39
- # Parse the content
40
- parse_attributes(content, attr_list)
41
-
42
- attr_list
43
- end
44
-
45
- # Merge another AttributeList into this one
46
- # @param other [AttributeList] The other attribute list to merge
47
- # @return [AttributeList] self for chaining
48
- def merge!(other)
49
- return self unless other
50
-
51
- self.id = other.id if other.id
52
- self.classes = (classes + other.classes).uniq
53
- merged = {}
54
- (attributes + other.attributes).each { |nv| merged[nv.name] = nv }
55
- self.attributes = merged.values
56
- self
57
- end
58
-
59
- # Create a merged copy
60
- # @param other [AttributeList] The other attribute list to merge
61
- # @return [AttributeList] New merged attribute list
62
- def merge(other)
63
- dup.merge!(other)
64
- end
65
-
66
- # Check if this has any attributes
67
- # @return [Boolean]
68
- def empty?
69
- id.nil? && classes.empty? && attributes.empty?
70
- end
71
-
72
- def self.parse_attributes(content, attr_list)
73
- # Use shared IalParser for consistent parsing
74
- ParserUtil::IalParser.tokenize(content).each do |token|
75
- case token[:type]
76
- when :class
77
- attr_list.classes << token[:value]
78
- when :id
79
- attr_list.id = token[:value]
80
- when :attribute
81
- attr_list.attributes << NamedValue.new(name: token[:key], value: token[:value])
82
- end
83
- end
84
- end
22
+ prepend Behaviors::AttributeList
85
23
  end
86
24
  end
87
25
  end
@@ -27,22 +27,7 @@ module Coradoc
27
27
  # without dragging YAML semantics into the Markdown model (MECE).
28
28
  attribute :frontmatter, :string
29
29
 
30
- # @param [Integer] index The index of the block to retrieve
31
- # @return [Coradoc::Markdown::Base] The block at the specified index
32
- def [](index)
33
- blocks[index]
34
- end
35
-
36
- # @param [Integer] index The index of the block to set
37
- # @param [Coradoc::Markdown::Base] value The block to set
38
- def []=(index, value)
39
- blocks[index] = value
40
- end
41
-
42
- # Create a document from an array of blocks
43
- def self.from_ast(elements)
44
- new(blocks: elements)
45
- end
30
+ prepend Behaviors::Document
46
31
  end
47
32
  end
48
33
  end
@@ -17,67 +17,7 @@ module Coradoc
17
17
  attribute :content, :string # For block extensions with content
18
18
  attribute :body, :string # Alias for content
19
19
 
20
- # Known extension types
21
- TYPES = {
22
- toc: :toc,
23
- options: :options,
24
- comment: :comment,
25
- nomarkdown: :nomarkdown,
26
- ignore: :ignore,
27
- if: :conditional,
28
- endif: :conditional
29
- }.freeze
30
-
31
- # Create a TOC extension
32
- # @param options [Hash] TOC options (levels, etc.)
33
- # @return [Extension]
34
- def self.toc(options = {})
35
- new(name: :toc, options: hash_to_named(options))
36
- end
37
-
38
- # Create an options extension
39
- # @param options [Hash] Parser options
40
- # @return [Extension]
41
- def self.options(options = {})
42
- new(name: :options, options: hash_to_named(options))
43
- end
44
-
45
- # Create a comment extension
46
- # @param content [String] Comment content
47
- # @return [Extension]
48
- def self.comment(content = '')
49
- new(name: :comment, content: content)
50
- end
51
-
52
- # Create a nomarkdown extension (passthrough)
53
- # @param content [String] Raw content to pass through
54
- # @return [Extension]
55
- def self.nomarkdown(content)
56
- new(name: :nomarkdown, content: content)
57
- end
58
-
59
- # Check if this is a specific extension type
60
- # @param type [Symbol] The extension type
61
- # @return [Boolean]
62
- def type?(type)
63
- name.to_sym == type
64
- end
65
-
66
- # Check if this is a self-closing extension
67
- # @return [Boolean]
68
- def self_closing?
69
- content.nil? || content.empty?
70
- end
71
-
72
- class << self
73
- private
74
-
75
- def hash_to_named(hash)
76
- return [] if hash.nil? || hash.empty?
77
-
78
- hash.map { |k, v| NamedValue.new(name: k.to_s, value: v.to_s) }
79
- end
80
- end
20
+ prepend Behaviors::Extension
81
21
  end
82
22
  end
83
23
  end
@@ -13,9 +13,6 @@ module Coradoc
13
13
  #
14
14
  # AsciiDoc `+` line-continuation maps to this element.
15
15
  class HardLineBreak < Base
16
- def initialize(**_rest)
17
- super
18
- end
19
16
  end
20
17
  end
21
18
  end
@@ -11,28 +11,7 @@ module Coradoc
11
11
  attribute :level, :integer, default: 1
12
12
  attribute :text, :string
13
13
 
14
- # Generate an auto ID from the heading text
15
- #
16
- # @return [String] A slugified version of the text suitable for use as an ID
17
- # @example
18
- # Heading.new(text: "Hello World!").auto_id #=> "hello-world"
19
- def auto_id
20
- return '' if text.nil? || text.empty?
21
-
22
- # Downcase, replace non-alphanumeric with hyphens, collapse multiple hyphens
23
- slug = text.to_s
24
- .downcase
25
- .gsub(/[^a-z0-9]+/, '-')
26
- .gsub(/^-+|-+$/, '')
27
- slug.empty? ? 'section' : slug
28
- end
29
-
30
- # Get the ID for this heading (uses explicit id if set, otherwise auto_id)
31
- #
32
- # @return [String] The ID to use for this heading
33
- def heading_id
34
- id || auto_id
35
- end
14
+ prepend Behaviors::Heading
36
15
  end
37
16
  end
38
17
  end
@@ -15,22 +15,7 @@ module Coradoc
15
15
  attribute :content, :string
16
16
  attribute :inline, :boolean, default: false
17
17
 
18
- # Check if this is inline math
19
- # @return [Boolean]
20
- def inline?
21
- inline == true
22
- end
23
-
24
- # Create an inline math element
25
- # @param content [String] The math content
26
- # @return [Math]
27
- def self.inline(content)
28
- new(content: content, inline: true)
29
- end
30
-
31
- def self.block(content)
32
- new(content: content, inline: false)
33
- end
18
+ prepend Behaviors::Math
34
19
  end
35
20
  end
36
21
  end
@@ -7,9 +7,7 @@ module Coradoc
7
7
  class Text < Base
8
8
  attribute :content, :string
9
9
 
10
- def to_s
11
- content
12
- end
10
+ prepend Behaviors::Text
13
11
  end
14
12
  end
15
13
  end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coradoc
4
+ module Markdown
5
+ module Schema
6
+ # Reads schema files and builds lutaml-model schema definitions
7
+ # (Lutaml::Model::Schema::Definitions::Model) from them.
8
+ class Definition
9
+ SCALARS = %w[string integer boolean float date].freeze
10
+
11
+ # `false` is a legitimate default but lutaml-model's renderer only
12
+ # emits `default:` for truthy values; this renders as the literal.
13
+ FalseDefault = Struct.new(:source) do
14
+ def inspect = source
15
+ end
16
+
17
+ # @param files [Array<String>] schema file paths
18
+ # @return [Array<Definition>] one per declared class, in file order
19
+ def self.read(files)
20
+ files.flat_map do |file|
21
+ YAML.safe_load_file(file).fetch('classes').map do |name, spec|
22
+ new(name, spec, source: File.basename(file))
23
+ end
24
+ end
25
+ end
26
+
27
+ attr_reader :name, :source
28
+
29
+ def initialize(name, spec, source: nil)
30
+ @name = name
31
+ @spec = spec
32
+ @source = source
33
+ end
34
+
35
+ # @return [Array<String>] names of Behaviors modules to prepend
36
+ def behaviors
37
+ Array(@spec['behaviors'])
38
+ end
39
+
40
+ # @param namespace [String] namespace generated sibling classes live in
41
+ # @return [Lutaml::Model::Schema::Definitions::Model]
42
+ def to_model(namespace:)
43
+ defs = Lutaml::Model::Schema::Definitions
44
+ members = @spec.fetch('attributes').map do |attr_name, opts|
45
+ attribute(attr_name, opts, namespace)
46
+ end
47
+ defs::Model.new(class_name: name,
48
+ xml_root: defs::XmlRoot.new(kind: :none),
49
+ members: members,
50
+ parent_class: "Coradoc::Markdown::#{@spec.fetch('parent')}")
51
+ end
52
+
53
+ private
54
+
55
+ def attribute(attr_name, opts, namespace)
56
+ Lutaml::Model::Schema::Definitions::Attribute.new(
57
+ name: attr_name.to_sym, type: type_ref(opts.fetch('type'), namespace),
58
+ xml_name: attr_name, kind: :attribute,
59
+ collection: opts['collection'] == true,
60
+ default: default_for(opts)
61
+ )
62
+ end
63
+
64
+ def default_for(opts)
65
+ return unless opts.key?('default')
66
+
67
+ opts['default'] == false ? FalseDefault.new('false') : opts['default']
68
+ end
69
+
70
+ def type_ref(type, namespace)
71
+ defs = Lutaml::Model::Schema::Definitions
72
+ return defs::TypeRef.new(kind: :symbol, value: type) if SCALARS.include?(type)
73
+
74
+ # Base is hand-written and shared; every other model is a sibling.
75
+ scope = type == 'Base' ? 'Coradoc::Markdown' : namespace
76
+ defs::TypeRef.new(kind: :class_ref, value: "#{scope}::#{type}")
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tmpdir'
4
+ require 'fileutils'
5
+
6
+ module Coradoc
7
+ module Markdown
8
+ module Schema
9
+ # Compiles Definitions into classes with lutaml-model's schema
10
+ # pipeline and applies the hand-written behavior modules.
11
+ #
12
+ # lutaml-model's ClassLoader writes class files under
13
+ # <dir>/<nested/namespace>/ but its registry requires them from
14
+ # <dir>/<last_segment>/, so a nested namespace cannot load; this
15
+ # subclass mirrors the files to where the registry looks for them.
16
+ class Loader
17
+ DEFAULT_NAMESPACE = 'Coradoc::Markdown::Generated'
18
+
19
+ # ClassLoader with the nested-namespace path corrected.
20
+ class NestedClassLoader < Lutaml::Model::Schema::ClassLoader
21
+ def load
22
+ Dir.mktmpdir do |dir|
23
+ Lutaml::Model::Schema::FileWriter.write(@output, dir, registry_generator: @registry_generator)
24
+ mirror_class_dir(dir)
25
+ require File.join(dir, "#{registry_basename}_registry")
26
+ call_register_all
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def mirror_class_dir(dir)
33
+ segments = @output.module_namespace.downcase.split('::')
34
+ FileUtils.cp_r(File.join(dir, *segments), File.join(dir, segments.last)) if segments.size > 1
35
+ end
36
+ end
37
+
38
+ def initialize(definitions, namespace: DEFAULT_NAMESPACE)
39
+ @definitions = definitions
40
+ @namespace = namespace
41
+ end
42
+
43
+ # @return [Array<Class>] generated classes, behaviors applied
44
+ def load
45
+ entries = @definitions.map do |definition|
46
+ Lutaml::Model::Schema::CompiledOutput::Entry.new(
47
+ definition.name, definition.to_model(namespace: @namespace), :model
48
+ )
49
+ end
50
+ output = Lutaml::Model::Schema::CompiledOutput.new(entries: entries, module_namespace: @namespace)
51
+ NestedClassLoader.load(output)
52
+ apply_behaviors
53
+ end
54
+
55
+ private
56
+
57
+ def apply_behaviors
58
+ host = Object.const_get(@namespace)
59
+ @definitions.map do |definition|
60
+ klass = host.const_get(definition.name)
61
+ definition.behaviors.each { |name| klass.prepend(Behaviors.const_get(name)) }
62
+ klass
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coradoc
4
+ module Markdown
5
+ module Schema
6
+ # Exhaustive IR-parity diff between two class sets, normally the
7
+ # hand-written models and the schema-generated classes.
8
+ #
9
+ # Compares, per class: presence, superclass, the ordered attribute
10
+ # list and, per attribute, its type, collection flag and default.
11
+ # Returns human-readable difference strings; empty means parity.
12
+ class Parity
13
+ # Differences accepted on purpose, each a defect of the hand-written
14
+ # models rather than of the schema.
15
+ #
16
+ # Base declares `attribute :attributes, NamedValue`, and NamedValue
17
+ # < Base, so NamedValue is autoloaded while Base is half-defined: the
18
+ # hand-written NamedValue ends up WITHOUT the inherited `attributes`
19
+ # attribute (when Heading loads first), or the require raises NameError
20
+ # (when NamedValue loads first). The generated NamedValue always has it.
21
+ KNOWN_DIFFERENCES = ['NamedValue.attributes: unexpected'].freeze
22
+
23
+ # @param expected [Module] namespace holding the reference classes
24
+ # @param actual [Module] namespace holding the classes under test
25
+ # @param names [Array<String>] class names to compare
26
+ # @param except [Array<String>] differences to ignore
27
+ def self.diff(expected:, actual:, names:, except: KNOWN_DIFFERENCES)
28
+ new(expected, actual).diff(names) - except
29
+ end
30
+
31
+ def initialize(expected, actual)
32
+ @expected = expected
33
+ @actual = actual
34
+ end
35
+
36
+ def diff(names)
37
+ names.flat_map { |name| class_diff(name) }
38
+ end
39
+
40
+ private
41
+
42
+ def class_diff(name)
43
+ want = lookup(@expected, name)
44
+ got = lookup(@actual, name)
45
+ return ["#{name}: missing from #{@expected}"] unless want
46
+ return ["#{name}: missing from #{@actual}"] unless got
47
+
48
+ superclass_diff(name, want, got) + attributes_diff(name, want, got)
49
+ end
50
+
51
+ def lookup(namespace, name)
52
+ namespace.const_get(name, false)
53
+ rescue NameError
54
+ nil
55
+ end
56
+
57
+ def superclass_diff(name, want, got)
58
+ return [] if short(want.superclass) == short(got.superclass)
59
+
60
+ ["#{name}: superclass #{short(want.superclass)} != #{short(got.superclass)}"]
61
+ end
62
+
63
+ def attributes_diff(name, want, got)
64
+ wa = want.attributes
65
+ ga = got.attributes
66
+ diffs = []
67
+ diffs << "#{name}: attribute order #{wa.keys} != #{ga.keys}" if wa.keys != ga.keys && wa.keys.sort == ga.keys.sort
68
+ (wa.keys - ga.keys).each { |a| diffs << "#{name}.#{a}: missing" }
69
+ (ga.keys - wa.keys).each { |a| diffs << "#{name}.#{a}: unexpected" }
70
+ (wa.keys & ga.keys).each { |a| diffs.concat(attribute_diff("#{name}.#{a}", wa[a], ga[a])) }
71
+ diffs
72
+ end
73
+
74
+ def attribute_diff(label, want, got)
75
+ facets = facets_of(want)
76
+ other = facets_of(got)
77
+ facets.filter_map do |facet, value|
78
+ "#{label}: #{facet} #{value.inspect} != #{other[facet].inspect}" unless value == other[facet]
79
+ end
80
+ end
81
+
82
+ def facets_of(attribute)
83
+ { type: short(attribute.type), collection: attribute.collection?, default: default_of(attribute) }
84
+ end
85
+
86
+ def default_of(attribute)
87
+ register = Lutaml::Model::Config.default_register
88
+ return :none unless attribute.default_set?(register)
89
+
90
+ attribute.default(register)
91
+ end
92
+
93
+ # Class identity is compared by unqualified name so Coradoc::Markdown::X
94
+ # and Coradoc::Markdown::Generated::X are the same type.
95
+ def short(type)
96
+ type.to_s.split('::').last
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module Coradoc
6
+ module Markdown
7
+ # Declarative schemas (coradoc-markdown/schemas/*.yml) for the Markdown
8
+ # model set, and the machinery that turns them into lutaml-model classes
9
+ # through lutaml-model's schema pipeline.
10
+ #
11
+ # Schema.load_generated(namespace: 'Coradoc::Markdown::Generated')
12
+ #
13
+ # Generated classes inherit from the hand-written Coradoc::Markdown::Base
14
+ # and get their hand-written behavior from Coradoc::Markdown::Behaviors.
15
+ module Schema
16
+ SCHEMA_DIR = File.expand_path('../../../schemas', __dir__)
17
+
18
+ autoload :Definition, 'coradoc/markdown/schema/definition'
19
+ autoload :Loader, 'coradoc/markdown/schema/loader'
20
+ autoload :Parity, 'coradoc/markdown/schema/parity'
21
+
22
+ class << self
23
+ # @return [Array<String>] paths of all schema files
24
+ def files
25
+ Dir[File.join(SCHEMA_DIR, '*.yml')]
26
+ end
27
+
28
+ # Replace the hand-written model classes with generated ones in
29
+ # Coradoc::Markdown itself (behind CORADOC_MARKDOWN_GENERATED=1).
30
+ def install!
31
+ load_generated(namespace: 'Coradoc::Markdown')
32
+ end
33
+
34
+ # Compile the schemas and load the resulting classes.
35
+ #
36
+ # @param namespace [String] module that hosts the generated classes
37
+ # @return [Array<Class>] the generated classes
38
+ def load_generated(namespace: Loader::DEFAULT_NAMESPACE)
39
+ Loader.new(Definition.read(files), namespace: namespace).load
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Coradoc
4
4
  module Markdown
5
- VERSION = '1.0.14'
5
+ VERSION = '1.0.15'
6
6
  end
7
7
  end
@@ -75,6 +75,10 @@ module Coradoc
75
75
  autoload :Literal, 'coradoc/markdown/model/literal'
76
76
  autoload :HardLineBreak, 'coradoc/markdown/model/hard_line_break'
77
77
 
78
+ # Declarative schemas + generated classes; hand-written behavior modules
79
+ autoload :Schema, 'coradoc/markdown/schema'
80
+ autoload :Behaviors, 'coradoc/markdown/behaviors'
81
+
78
82
  # Serializer
79
83
  autoload :Serializer, 'coradoc/markdown/serializer'
80
84
 
@@ -198,6 +202,10 @@ module Coradoc
198
202
  end
199
203
  end
200
204
 
205
+ # Opt-in: replace the hand-written model classes with the ones generated
206
+ # from coradoc-markdown/schemas/*.yml (see Coradoc::Markdown::Schema).
207
+ Markdown::Schema.install! if ENV['CORADOC_MARKDOWN_GENERATED'] == '1'
208
+
201
209
  # Register the Markdown format with Coradoc
202
210
  register_format(:markdown, Markdown,
203
211
  aliases: %w[md markdown mdown mkd],
@@ -0,0 +1,73 @@
1
+ # coradoc-markdown model schema: headings and block-level nodes.
2
+ # Source of truth for generated classes (see Coradoc::Markdown::Schema).
3
+ # Types: string|integer|boolean are scalars; anything else names a model class.
4
+ # `behaviors` names modules in Coradoc::Markdown::Behaviors prepended after load.
5
+ classes:
6
+ Heading:
7
+ parent: Base
8
+ behaviors: [Heading]
9
+ attributes:
10
+ level: { type: integer, default: 1 }
11
+ text: { type: string }
12
+ Paragraph:
13
+ parent: Base
14
+ attributes:
15
+ text: { type: string }
16
+ children: { type: Base, collection: true, default: [] }
17
+ CodeBlock:
18
+ parent: Base
19
+ attributes:
20
+ language: { type: string }
21
+ code: { type: string }
22
+ Blockquote:
23
+ parent: Base
24
+ attributes:
25
+ content: { type: string }
26
+ HorizontalRule:
27
+ parent: Base
28
+ attributes:
29
+ style: { type: string, default: '---' }
30
+ Admonition:
31
+ parent: Base
32
+ behaviors: [Admonition]
33
+ attributes:
34
+ admonition_type: { type: string }
35
+ content: { type: string }
36
+ title: { type: string }
37
+ children: { type: Base, collection: true, default: [] }
38
+ ExampleBlock:
39
+ parent: Base
40
+ attributes:
41
+ content: { type: string }
42
+ caption: { type: string }
43
+ children: { type: Base, collection: true, default: [] }
44
+ OpenBlock:
45
+ parent: Base
46
+ attributes:
47
+ children: { type: Base, collection: true, default: [] }
48
+ Sidebar:
49
+ parent: Base
50
+ attributes:
51
+ content: { type: string }
52
+ title: { type: string }
53
+ children: { type: Base, collection: true, default: [] }
54
+ Verse:
55
+ parent: Base
56
+ attributes:
57
+ content: { type: string }
58
+ attribution: { type: string }
59
+ citetitle: { type: string }
60
+ Pass:
61
+ parent: Base
62
+ attributes:
63
+ content: { type: string }
64
+ Literal:
65
+ parent: Base
66
+ attributes:
67
+ content: { type: string }
68
+ Math:
69
+ parent: Base
70
+ behaviors: [Math]
71
+ attributes:
72
+ content: { type: string }
73
+ inline: { type: boolean, default: false }
@@ -0,0 +1,55 @@
1
+ # coradoc-markdown model schema: document container, metadata and reference-like nodes.
2
+ # Source of truth for generated classes (see Coradoc::Markdown::Schema).
3
+ # Types: string|integer|boolean are scalars; anything else names a model class.
4
+ # `behaviors` names modules in Coradoc::Markdown::Behaviors prepended after load.
5
+ classes:
6
+ Document:
7
+ parent: Base
8
+ behaviors: [Document]
9
+ attributes:
10
+ blocks: { type: Base, collection: true }
11
+ frontmatter: { type: string }
12
+ NamedValue:
13
+ parent: Base
14
+ attributes:
15
+ name: { type: string }
16
+ value: { type: string }
17
+ AttributeList:
18
+ parent: Base
19
+ behaviors: [AttributeList]
20
+ attributes:
21
+ id: { type: string }
22
+ classes: { type: string, collection: true, default: [] }
23
+ attributes: { type: NamedValue, collection: true, default: [] }
24
+ name: { type: string }
25
+ Extension:
26
+ parent: Base
27
+ behaviors: [Extension]
28
+ attributes:
29
+ name: { type: string }
30
+ options: { type: NamedValue, collection: true, default: [] }
31
+ content: { type: string }
32
+ body: { type: string }
33
+ Comment:
34
+ parent: Base
35
+ attributes:
36
+ text: { type: string }
37
+ Abbreviation:
38
+ parent: Base
39
+ attributes:
40
+ term: { type: string }
41
+ definition: { type: string }
42
+ Footnote:
43
+ parent: Base
44
+ attributes:
45
+ content: { type: string }
46
+ inline_content: { type: string, collection: true }
47
+ backlink: { type: boolean, default: true }
48
+ FootnoteReference:
49
+ parent: Base
50
+ attributes: {}
51
+ CrossReference:
52
+ parent: Base
53
+ attributes:
54
+ text: { type: string }
55
+ target: { type: string }
@@ -0,0 +1,57 @@
1
+ # coradoc-markdown model schema: inline nodes.
2
+ # Source of truth for generated classes (see Coradoc::Markdown::Schema).
3
+ # Types: string|integer|boolean are scalars; anything else names a model class.
4
+ # `behaviors` names modules in Coradoc::Markdown::Behaviors prepended after load.
5
+ classes:
6
+ Text:
7
+ parent: Base
8
+ behaviors: [Text]
9
+ attributes:
10
+ content: { type: string }
11
+ Emphasis:
12
+ parent: Base
13
+ attributes:
14
+ text: { type: string }
15
+ Strong:
16
+ parent: Base
17
+ attributes:
18
+ text: { type: string }
19
+ Code:
20
+ parent: Base
21
+ attributes:
22
+ text: { type: string }
23
+ Link:
24
+ parent: Base
25
+ attributes:
26
+ text: { type: string }
27
+ url: { type: string }
28
+ title: { type: string }
29
+ Image:
30
+ parent: Base
31
+ attributes:
32
+ alt: { type: string }
33
+ src: { type: string }
34
+ title: { type: string }
35
+ Strikethrough:
36
+ parent: Base
37
+ attributes:
38
+ text: { type: string }
39
+ Highlight:
40
+ parent: Base
41
+ attributes:
42
+ text: { type: string }
43
+ Subscript:
44
+ parent: Base
45
+ attributes:
46
+ text: { type: string }
47
+ Superscript:
48
+ parent: Base
49
+ attributes:
50
+ text: { type: string }
51
+ Underline:
52
+ parent: Base
53
+ attributes:
54
+ text: { type: string }
55
+ HardLineBreak:
56
+ parent: Base
57
+ attributes: {}
data/schemas/lists.yml ADDED
@@ -0,0 +1,35 @@
1
+ # coradoc-markdown model schema: ordered/unordered and definition lists.
2
+ # Source of truth for generated classes (see Coradoc::Markdown::Schema).
3
+ # Types: string|integer|boolean are scalars; anything else names a model class.
4
+ # `behaviors` names modules in Coradoc::Markdown::Behaviors prepended after load.
5
+ classes:
6
+ List:
7
+ parent: Base
8
+ attributes:
9
+ ordered: { type: boolean, default: false }
10
+ items: { type: ListItem, collection: true }
11
+ start_number: { type: integer, default: 1 }
12
+ ListItem:
13
+ parent: Base
14
+ attributes:
15
+ text: { type: string }
16
+ checked: { type: boolean }
17
+ sublist: { type: List }
18
+ children: { type: Base, collection: true, default: [] }
19
+ DefinitionList:
20
+ parent: Base
21
+ attributes:
22
+ items: { type: DefinitionTerm, collection: true }
23
+ DefinitionTerm:
24
+ parent: Base
25
+ attributes:
26
+ text: { type: string }
27
+ definitions: { type: DefinitionItem, collection: true, default: [] }
28
+ text_children: { type: Base, collection: true, default: [] }
29
+ nested: { type: DefinitionList }
30
+ DefinitionItem:
31
+ parent: Base
32
+ attributes:
33
+ content: { type: string }
34
+ inline_content: { type: Base, collection: true, default: [] }
35
+ blocks: { type: string, collection: true }
@@ -0,0 +1,11 @@
1
+ # coradoc-markdown model schema: tables.
2
+ # Source of truth for generated classes (see Coradoc::Markdown::Schema).
3
+ # Types: string|integer|boolean are scalars; anything else names a model class.
4
+ # `behaviors` names modules in Coradoc::Markdown::Behaviors prepended after load.
5
+ classes:
6
+ Table:
7
+ parent: Base
8
+ attributes:
9
+ headers: { type: string, collection: true }
10
+ rows: { type: string, collection: true, default: [] }
11
+ alignments: { type: string, collection: true }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coradoc-markdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.14
4
+ version: 1.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -43,14 +43,14 @@ dependencies:
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: 1.3.79
46
+ version: 1.3.123
47
47
  type: :runtime
48
48
  prerelease: false
49
49
  version_requirements: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: 1.3.79
53
+ version: 1.3.123
54
54
  - !ruby/object:Gem::Dependency
55
55
  name: rspec
56
56
  requirement: !ruby/object:Gem::Requirement
@@ -104,6 +104,14 @@ extra_rdoc_files: []
104
104
  files:
105
105
  - LICENSE.txt
106
106
  - lib/coradoc/markdown.rb
107
+ - lib/coradoc/markdown/behaviors.rb
108
+ - lib/coradoc/markdown/behaviors/admonition.rb
109
+ - lib/coradoc/markdown/behaviors/attribute_list.rb
110
+ - lib/coradoc/markdown/behaviors/document.rb
111
+ - lib/coradoc/markdown/behaviors/extension.rb
112
+ - lib/coradoc/markdown/behaviors/heading.rb
113
+ - lib/coradoc/markdown/behaviors/math.rb
114
+ - lib/coradoc/markdown/behaviors/text.rb
107
115
  - lib/coradoc/markdown/errors.rb
108
116
  - lib/coradoc/markdown/lint.rb
109
117
  - lib/coradoc/markdown/lint/duplicate_headings.rb
@@ -158,6 +166,10 @@ files:
158
166
  - lib/coradoc/markdown/parser/inline_parser.rb
159
167
  - lib/coradoc/markdown/parser/parsanol_atoms.rb
160
168
  - lib/coradoc/markdown/parser_util.rb
169
+ - lib/coradoc/markdown/schema.rb
170
+ - lib/coradoc/markdown/schema/definition.rb
171
+ - lib/coradoc/markdown/schema/loader.rb
172
+ - lib/coradoc/markdown/schema/parity.rb
161
173
  - lib/coradoc/markdown/serializer.rb
162
174
  - lib/coradoc/markdown/serializer/builder.rb
163
175
  - lib/coradoc/markdown/serializer/config.rb
@@ -230,6 +242,11 @@ files:
230
242
  - lib/coradoc/markdown/transform/to_core_model.rb
231
243
  - lib/coradoc/markdown/transformer.rb
232
244
  - lib/coradoc/markdown/version.rb
245
+ - schemas/blocks.yml
246
+ - schemas/document.yml
247
+ - schemas/inline.yml
248
+ - schemas/lists.yml
249
+ - schemas/tables.yml
233
250
  homepage: https://github.com/metanorma/coradoc
234
251
  licenses:
235
252
  - MIT