coradoc-markdown 1.0.13 → 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 +4 -4
- data/lib/coradoc/markdown/behaviors/admonition.rb +21 -0
- data/lib/coradoc/markdown/behaviors/attribute_list.rb +81 -0
- data/lib/coradoc/markdown/behaviors/document.rb +34 -0
- data/lib/coradoc/markdown/behaviors/extension.rb +69 -0
- data/lib/coradoc/markdown/behaviors/heading.rb +33 -0
- data/lib/coradoc/markdown/behaviors/math.rb +32 -0
- data/lib/coradoc/markdown/behaviors/text.rb +14 -0
- data/lib/coradoc/markdown/behaviors.rb +18 -0
- data/lib/coradoc/markdown/lint/duplicate_headings.rb +30 -0
- data/lib/coradoc/markdown/lint/heading_levels.rb +27 -0
- data/lib/coradoc/markdown/lint.rb +17 -0
- data/lib/coradoc/markdown/model/admonition.rb +1 -8
- data/lib/coradoc/markdown/model/attribute_list.rb +1 -63
- data/lib/coradoc/markdown/model/document.rb +1 -16
- data/lib/coradoc/markdown/model/extension.rb +1 -61
- data/lib/coradoc/markdown/model/hard_line_break.rb +0 -3
- data/lib/coradoc/markdown/model/heading.rb +1 -22
- data/lib/coradoc/markdown/model/math.rb +1 -16
- data/lib/coradoc/markdown/model/text.rb +1 -3
- data/lib/coradoc/markdown/parser/ast_processor.rb +8 -10
- data/lib/coradoc/markdown/parser/attribute_lists.rb +48 -0
- data/lib/coradoc/markdown/parser/block_parser.rb +12 -45
- data/lib/coradoc/markdown/parser/inline_parser.rb +41 -19
- data/lib/coradoc/markdown/parser/parsanol_atoms.rb +15 -114
- data/lib/coradoc/markdown/schema/definition.rb +81 -0
- data/lib/coradoc/markdown/schema/loader.rb +68 -0
- data/lib/coradoc/markdown/schema/parity.rb +101 -0
- data/lib/coradoc/markdown/schema.rb +44 -0
- data/lib/coradoc/markdown/serializer/registrations.rb +2 -0
- data/lib/coradoc/markdown/serializer/serializers/text.rb +19 -0
- data/lib/coradoc/markdown/transform/from_core_model.rb +18 -0
- data/lib/coradoc/markdown/transform/to_core_model.rb +14 -5
- data/lib/coradoc/markdown/transformer.rb +73 -2
- data/lib/coradoc/markdown/version.rb +1 -1
- data/lib/coradoc/markdown.rb +12 -0
- data/schemas/blocks.yml +73 -0
- data/schemas/document.yml +55 -0
- data/schemas/inline.yml +57 -0
- data/schemas/lists.yml +35 -0
- data/schemas/tables.yml +11 -0
- metadata +27 -5
|
@@ -40,17 +40,15 @@ module Coradoc
|
|
|
40
40
|
elements = []
|
|
41
41
|
remaining = text
|
|
42
42
|
|
|
43
|
-
#
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
# Footnote reference scan: [^name] — linear index search
|
|
44
|
+
# instead of a regex (CodeQL rb/polynomial-redos).
|
|
46
45
|
until remaining.empty?
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
elements <<
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
remaining = match.post_match
|
|
46
|
+
open_idx = remaining.index('[^')
|
|
47
|
+
close_idx = open_idx && remaining.index(']', open_idx + 2)
|
|
48
|
+
if close_idx
|
|
49
|
+
elements << remaining[0...open_idx] unless open_idx.zero?
|
|
50
|
+
elements << { fn_ref: remaining[(open_idx + 2)...close_idx] }
|
|
51
|
+
remaining = remaining[(close_idx + 1)..]
|
|
54
52
|
else
|
|
55
53
|
# No more matches - add remaining text
|
|
56
54
|
elements << remaining
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coradoc
|
|
4
|
+
module Markdown
|
|
5
|
+
module Parser
|
|
6
|
+
# Kramdown attribute-list rules shared by block and inline
|
|
7
|
+
# elements: IAL ({:.class #id key="value"}) and ALD
|
|
8
|
+
# ({:name: #id .class key="value"}).
|
|
9
|
+
module AttributeLists
|
|
10
|
+
def ial_class
|
|
11
|
+
str('.') >> match['\\w\\-'].repeat(1)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def ial_id
|
|
15
|
+
str('#') >> match['\\w\\-'].repeat(1)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def ial_key_value
|
|
19
|
+
match['\\w\\-'].repeat(1) >> str('=') >>
|
|
20
|
+
(
|
|
21
|
+
(str('"') >> match['^"'].repeat(0) >> str('"')) |
|
|
22
|
+
(str("'") >> match["^'"].repeat(0) >> str("'")) |
|
|
23
|
+
match['^\\s\\}'].repeat(1)
|
|
24
|
+
)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def ial_content
|
|
28
|
+
(
|
|
29
|
+
whitespace.repeat >>
|
|
30
|
+
(ial_class | ial_id | ial_key_value)
|
|
31
|
+
).repeat(1)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def ial
|
|
35
|
+
str('{:') >> ial_content.as(:ial) >> str('}')
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def ald_name
|
|
39
|
+
match['\\w'].repeat(1) >> str(':')
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def ald
|
|
43
|
+
str('{:') >> ald_name.as(:ald_name) >> whitespace.repeat(1) >> ial_content.as(:ial) >> str('}')
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -7,13 +7,19 @@ module Coradoc
|
|
|
7
7
|
require_relative 'parsanol_atoms'
|
|
8
8
|
|
|
9
9
|
class BlockParser < Parsanol::Parser
|
|
10
|
-
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
10
|
+
include AttributeLists
|
|
11
|
+
# Native engine is the default surface for the block grammar.
|
|
12
|
+
# Full tree parity incl. scope/continuation and lazy
|
|
13
|
+
# continuation (engine_differential_spec, all shapes always-on;
|
|
14
|
+
# Atoms::Constant 1.3.75, rs#160 bridge writeback 1.3.76,
|
|
15
|
+
# rs#199 map-key capture iteration 1.3.78, and the rs#201
|
|
16
|
+
# GC-marked host-registry fix in 1.3.79 that ended the linux
|
|
17
|
+
# segfault in scope-shape replay — the gemspec floor).
|
|
18
|
+
# InlineParser parses native-when-expressible (its grammar is
|
|
19
|
+
# wire-expressible since the #163 restructure).
|
|
14
20
|
def parse(input, mode_or_opts = {}, **kwargs)
|
|
15
21
|
opts = mode_or_opts.is_a?(Hash) ? mode_or_opts.merge(kwargs) : {}
|
|
16
|
-
super(input, **opts, mode: opts.fetch(:mode, :
|
|
22
|
+
super(input, **opts, mode: opts.fetch(:mode, :native))
|
|
17
23
|
end
|
|
18
24
|
|
|
19
25
|
# NOTE: Debug method for parser development. Outputs current parse position
|
|
@@ -292,46 +298,7 @@ module Coradoc
|
|
|
292
298
|
end
|
|
293
299
|
|
|
294
300
|
# ===== KRAMDOWN EXTENSIONS =====
|
|
295
|
-
|
|
296
|
-
# Inline Attribute List (IAL): {:.class #id key="value"}
|
|
297
|
-
# Can appear after any block element to add attributes
|
|
298
|
-
rule(:ial_class) do
|
|
299
|
-
str('.') >> match['\\w\\-'].repeat(1)
|
|
300
|
-
end
|
|
301
|
-
|
|
302
|
-
rule(:ial_id) do
|
|
303
|
-
str('#') >> match['\\w\\-'].repeat(1)
|
|
304
|
-
end
|
|
305
|
-
|
|
306
|
-
rule(:ial_key_value) do
|
|
307
|
-
match['\\w\\-'].repeat(1) >> str('=') >>
|
|
308
|
-
(
|
|
309
|
-
(str('"') >> match['^"'].repeat(0) >> str('"')) |
|
|
310
|
-
(str("'") >> match["^'"].repeat(0) >> str("'")) |
|
|
311
|
-
match['^\\s\\}'].repeat(1)
|
|
312
|
-
)
|
|
313
|
-
end
|
|
314
|
-
|
|
315
|
-
rule(:ial_content) do
|
|
316
|
-
(
|
|
317
|
-
whitespace.repeat >>
|
|
318
|
-
(ial_class | ial_id | ial_key_value)
|
|
319
|
-
).repeat(1)
|
|
320
|
-
end
|
|
321
|
-
|
|
322
|
-
rule(:ial) do
|
|
323
|
-
str('{:') >> ial_content.as(:ial) >> str('}')
|
|
324
|
-
end
|
|
325
|
-
|
|
326
|
-
# Attribute List Definition (ALD): {:name: #id .class key="value"}
|
|
327
|
-
# Defines a named attribute list that can be referenced
|
|
328
|
-
rule(:ald_name) do
|
|
329
|
-
match['\\w'].repeat(1) >> str(':')
|
|
330
|
-
end
|
|
331
|
-
|
|
332
|
-
rule(:ald) do
|
|
333
|
-
str('{:') >> ald_name.as(:ald_name) >> whitespace.repeat(1) >> ial_content.as(:ial) >> str('}')
|
|
334
|
-
end
|
|
301
|
+
# Attribute-list rules (ial/ald) live in AttributeLists.
|
|
335
302
|
|
|
336
303
|
# Block-level extension: {::extension_name options /}
|
|
337
304
|
# Common extensions: {::toc}, {::options ... /}
|
|
@@ -46,14 +46,13 @@ module Coradoc
|
|
|
46
46
|
|
|
47
47
|
rule(:escape) { str('\\').ignore >> match["!\"#$%&'\\(\\)*+,\\-./:;<=>?@\\[\\\\\\]\\^_`\\{\\|\\}~"] }
|
|
48
48
|
rule(:dec_entity) do
|
|
49
|
-
str('&#').ignore >> match['0-9'].repeat(1, 7).
|
|
49
|
+
str('&#').ignore >> match['0-9'].repeat(1, 7).as(:dec_entity) >> str(';').ignore
|
|
50
50
|
end
|
|
51
51
|
rule(:hex_entity) do
|
|
52
|
-
str('&#').ignore >> match['xX'].ignore >> match['A-Fa-f0-9'].repeat(1,
|
|
53
|
-
6).dynamic_output(method(:unicode_hex)) >> str(';').ignore
|
|
52
|
+
str('&#').ignore >> match['xX'].ignore >> match['A-Fa-f0-9'].repeat(1, 6).as(:hex_entity) >> str(';').ignore
|
|
54
53
|
end
|
|
55
54
|
rule(:entity) do
|
|
56
|
-
str('&').ignore >> match['A-Za-z0-9'].repeat(1).
|
|
55
|
+
str('&').ignore >> match['A-Za-z0-9'].repeat(1).as(:entity_name) >> str(';').ignore
|
|
57
56
|
end
|
|
58
57
|
rule(:nul_byte) { str("\0").output("\uFFFD") }
|
|
59
58
|
rule(:special_char) { escape | dec_entity | hex_entity | entity | nul_byte }
|
|
@@ -63,11 +62,11 @@ module Coradoc
|
|
|
63
62
|
end
|
|
64
63
|
|
|
65
64
|
rule(:code_span) do
|
|
66
|
-
|
|
65
|
+
Parsanol::Atoms::Lookbehind.new(1, '`', positive: false) >>
|
|
67
66
|
str('`').repeat(1).capture(:code_opener).ignore >>
|
|
68
67
|
dynamic do |_src, ctx|
|
|
69
|
-
ending = (
|
|
70
|
-
(ending.absent? >> any).repeat(1).
|
|
68
|
+
ending = (Parsanol::Atoms::Lookbehind.new(1, '`', positive: false) >> str(ctx.captures[:code_opener]).ignore >> str('`').absent?)
|
|
69
|
+
(ending.absent? >> any).repeat(1).as(:code_raw) >> ending
|
|
71
70
|
end
|
|
72
71
|
end
|
|
73
72
|
|
|
@@ -76,14 +75,14 @@ module Coradoc
|
|
|
76
75
|
end
|
|
77
76
|
|
|
78
77
|
rule(:both_flanking_delimiter_run) do
|
|
79
|
-
|
|
80
|
-
|
|
78
|
+
Parsanol::Atoms::Lookbehind.regex('[\\s\\S]') >>
|
|
79
|
+
Parsanol::Atoms::Lookbehind.regex('[\\p{Zs}\\t\\r\\n\\f]', positive: false) >> (
|
|
81
80
|
(
|
|
82
|
-
|
|
81
|
+
Parsanol::Atoms::Lookbehind.regex('[\\p{P}\\p{S}]') >>
|
|
83
82
|
delimiter_run.as(:bfdr) >>
|
|
84
83
|
unicode_punctuation.present?
|
|
85
84
|
) | (
|
|
86
|
-
|
|
85
|
+
Parsanol::Atoms::Lookbehind.regex('[\\p{P}\\p{S}]', positive: false) >>
|
|
87
86
|
delimiter_run.as(:bfdr) >>
|
|
88
87
|
unicode_punctuation.absent?
|
|
89
88
|
)
|
|
@@ -96,21 +95,21 @@ module Coradoc
|
|
|
96
95
|
delimiter_run.as(:lfdr) >>
|
|
97
96
|
unicode_punctuation.absent?
|
|
98
97
|
) | (
|
|
99
|
-
((
|
|
98
|
+
(Parsanol::Atoms::Lookbehind.regex('[\\p{Zs}\\t\\r\\n\\f\\p{P}\\p{S}]') | Parsanol::Atoms::Lookbehind.regex('[\\s\\S]', positive: false)) >>
|
|
100
99
|
delimiter_run.as(:lfdr)
|
|
101
100
|
)
|
|
102
101
|
) >> unicode_whitespace.absent?
|
|
103
102
|
end
|
|
104
103
|
|
|
105
104
|
rule(:right_flanking_delimiter_run) do
|
|
106
|
-
|
|
107
|
-
|
|
105
|
+
Parsanol::Atoms::Lookbehind.regex('[\\s\\S]') >>
|
|
106
|
+
Parsanol::Atoms::Lookbehind.regex('[\\p{Zs}\\t\\r\\n\\f]', positive: false) >> (
|
|
108
107
|
(
|
|
109
|
-
|
|
108
|
+
Parsanol::Atoms::Lookbehind.regex('[\\p{P}\\p{S}]') >>
|
|
110
109
|
delimiter_run.as(:rfdr) >>
|
|
111
110
|
(unicode_whitespace | unicode_punctuation).present?
|
|
112
111
|
) | (
|
|
113
|
-
|
|
112
|
+
Parsanol::Atoms::Lookbehind.regex('[\\p{P}\\p{S}]', positive: false) >>
|
|
114
113
|
delimiter_run.as(:rfdr)
|
|
115
114
|
)
|
|
116
115
|
)
|
|
@@ -125,11 +124,11 @@ module Coradoc
|
|
|
125
124
|
end
|
|
126
125
|
|
|
127
126
|
rule(:run_surrounded_by_punctuation) do
|
|
128
|
-
(
|
|
127
|
+
(Parsanol::Atoms::Lookbehind.regex('[\\p{P}\\p{S}]') >> flanking_delimiter_run >> unicode_punctuation.present?).as(:rsp)
|
|
129
128
|
end
|
|
130
129
|
|
|
131
130
|
rule(:run_preceded_by_punctuation) do
|
|
132
|
-
(
|
|
131
|
+
(Parsanol::Atoms::Lookbehind.regex('[\\p{P}\\p{S}]') >> flanking_delimiter_run).as(:rpp)
|
|
133
132
|
end
|
|
134
133
|
|
|
135
134
|
rule(:run_followed_by_punctuation) do
|
|
@@ -265,8 +264,31 @@ module Coradoc
|
|
|
265
264
|
# pp x
|
|
266
265
|
end
|
|
267
266
|
|
|
267
|
+
# Grammar-phase Ruby callables (parsanol-ruby#163: not
|
|
268
|
+
# wire-expressible) are replaced by raw captures, decoded here
|
|
269
|
+
# so parse() output is unchanged: {dec_entity: "65"} -> "A",
|
|
270
|
+
# {code_raw: s} -> {code: process_code(s)}.
|
|
271
|
+
def normalize_dynamic_captures(node)
|
|
272
|
+
case node
|
|
273
|
+
when Array
|
|
274
|
+
node.map { |child| normalize_dynamic_captures(child) }
|
|
275
|
+
when Hash
|
|
276
|
+
if node.size == 1
|
|
277
|
+
case node.keys.first
|
|
278
|
+
when :dec_entity then return unicode_dec(node.values.first)
|
|
279
|
+
when :hex_entity then return unicode_hex(node.values.first)
|
|
280
|
+
when :entity_name then return lookup_entity(node.values.first)
|
|
281
|
+
when :code_raw then return { code: process_code(node.values.first.to_s) }
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
node.transform_values { |v| normalize_dynamic_captures(v) }
|
|
285
|
+
else
|
|
286
|
+
node
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
|
|
268
290
|
def parse(io, options = {})
|
|
269
|
-
process_emphasis(super)
|
|
291
|
+
process_emphasis(normalize_dynamic_captures(super))
|
|
270
292
|
end
|
|
271
293
|
end
|
|
272
294
|
end
|
|
@@ -5,130 +5,31 @@ require 'parsanol'
|
|
|
5
5
|
module Coradoc
|
|
6
6
|
module Markdown
|
|
7
7
|
module Parser
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
8
|
+
# DSL surface for the Markdown parser. The custom atoms this
|
|
9
|
+
# module once carried (Output, DynamicOutput, Lookbehind) are
|
|
10
|
+
# retired: fixed-yield sites use Atoms::Constant, callable
|
|
11
|
+
# transforms moved to the AST phase, and guards use the
|
|
12
|
+
# built-in Atoms::Lookbehind — the grammar is fully
|
|
13
|
+
# wire-expressible (parsanol-ruby#163).
|
|
12
14
|
module ParsanolAtoms
|
|
13
|
-
# Matches +parslet+ but yields a fixed +value+.
|
|
14
|
-
class Output < Parsanol::Atoms::Custom
|
|
15
|
-
attr_reader :parslet, :value
|
|
16
|
-
|
|
17
|
-
def initialize(parslet, value)
|
|
18
|
-
@parslet = parslet
|
|
19
|
-
@value = value
|
|
20
|
-
super()
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def try_match(source, context, consume_all)
|
|
24
|
-
success, = parslet.apply(source, context, consume_all)
|
|
25
|
-
[success, success ? value : nil]
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
# Matches +parslet+ and yields the callable applied to the
|
|
30
|
-
# flattened match value.
|
|
31
|
-
class DynamicOutput < Parsanol::Atoms::Custom
|
|
32
|
-
include Parsanol::Atoms::CanFlatten
|
|
33
|
-
|
|
34
|
-
attr_reader :parslet, :callable
|
|
35
|
-
|
|
36
|
-
def initialize(parslet, callable)
|
|
37
|
-
@parslet = parslet
|
|
38
|
-
@callable = callable
|
|
39
|
-
super()
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def try_match(source, context, consume_all)
|
|
43
|
-
success, value = parslet.apply(source, context, consume_all)
|
|
44
|
-
return [false, nil] unless success
|
|
45
|
-
|
|
46
|
-
[true, callable.call(flatten(value))]
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
# Positive/negative lookbehind: succeeds (zero-width) when the
|
|
51
|
-
# preceding +number+ characters are/are not matched by
|
|
52
|
-
# +bound_parslet+. Character-accurate for multibyte input —
|
|
53
|
-
# rewinds over UTF-8 continuation bytes.
|
|
54
|
-
class Lookbehind < Parsanol::Atoms::Custom
|
|
55
|
-
attr_reader :positive, :number, :bound_parslet
|
|
56
|
-
|
|
57
|
-
def initialize(bound_parslet, number, positive: true)
|
|
58
|
-
@positive = positive
|
|
59
|
-
@number = number
|
|
60
|
-
@bound_parslet = bound_parslet
|
|
61
|
-
super()
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
def error_msgs
|
|
65
|
-
@error_msgs ||= {
|
|
66
|
-
positive: ['Input should be preceded by ', bound_parslet],
|
|
67
|
-
negative: ['Input should not be preceded by ', bound_parslet]
|
|
68
|
-
}
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
def try_match(source, context, _consume_all)
|
|
72
|
-
rewind_pos = source.bytepos
|
|
73
|
-
if rewind_pos.zero?
|
|
74
|
-
return [true, nil] unless positive
|
|
75
|
-
|
|
76
|
-
return context.err_at(self, source, error_msgs[:positive], source.pos)
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
source.bytepos = rewind_chars(source, number)
|
|
80
|
-
error_pos = source.pos
|
|
81
|
-
success, = bound_parslet.apply(source, context, false)
|
|
82
|
-
|
|
83
|
-
if positive
|
|
84
|
-
return [true, nil] if success
|
|
85
|
-
|
|
86
|
-
context.err_at(self, source, error_msgs[:positive], error_pos)
|
|
87
|
-
elsif success
|
|
88
|
-
context.err_at(self, source, error_msgs[:negative], error_pos)
|
|
89
|
-
else
|
|
90
|
-
[true, nil]
|
|
91
|
-
end
|
|
92
|
-
ensure
|
|
93
|
-
source.bytepos = rewind_pos
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
private
|
|
97
|
-
|
|
98
|
-
def rewind_chars(source, nchars)
|
|
99
|
-
input = source.input
|
|
100
|
-
pos = source.bytepos
|
|
101
|
-
nchars.times do
|
|
102
|
-
pos -= 1
|
|
103
|
-
pos -= 1 while pos.positive? && (input.getbyte(pos) & 0xC0) == 0x80
|
|
104
|
-
end
|
|
105
|
-
[pos, 0].max
|
|
106
|
-
end
|
|
107
|
-
end
|
|
108
15
|
end
|
|
109
16
|
end
|
|
110
17
|
end
|
|
111
18
|
end
|
|
112
19
|
|
|
113
|
-
# Additive DSL surface (no overrides): exposes the custom
|
|
114
|
-
# every atom
|
|
20
|
+
# Additive DSL surface (no overrides): exposes the remaining custom
|
|
21
|
+
# DSL on every atom. The custom DynamicOutput/Lookbehind atoms are
|
|
22
|
+
# GONE — raw captures decode in the AST phase and guards use the
|
|
23
|
+
# built-in Parsanol::Atoms::Lookbehind (literal + regex forms,
|
|
24
|
+
# parsanol >= 1.3.77) — parsanol-ruby#163.
|
|
115
25
|
module Parsanol
|
|
116
26
|
module Atoms
|
|
117
27
|
module DSL
|
|
28
|
+
# Matches self (discarding its captures) and yields a fixed
|
|
29
|
+
# value — built on Atoms::Constant so the shape is
|
|
30
|
+
# wire-expressible for the native engine (rs#137 follow-up).
|
|
118
31
|
def output(value)
|
|
119
|
-
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
def dynamic_output(callable)
|
|
123
|
-
Coradoc::Markdown::Parser::ParsanolAtoms::DynamicOutput.new(self, callable)
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
def precedes?(num = 1)
|
|
127
|
-
Coradoc::Markdown::Parser::ParsanolAtoms::Lookbehind.new(self, num, positive: true)
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
def does_not_precede?(num = 1)
|
|
131
|
-
Coradoc::Markdown::Parser::ParsanolAtoms::Lookbehind.new(self, num, positive: false)
|
|
32
|
+
ignore >> Constant.new(value)
|
|
132
33
|
end
|
|
133
34
|
end
|
|
134
35
|
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
|