kaleidl 0.1.6
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/LICENSE.txt +21 -0
- data/README.adoc +284 -0
- data/lib/kaleidl/character_sequence.rb +35 -0
- data/lib/kaleidl/content_element.rb +51 -0
- data/lib/kaleidl/errors.rb +16 -0
- data/lib/kaleidl/label.rb +30 -0
- data/lib/kaleidl/label_context.rb +24 -0
- data/lib/kaleidl/label_context_condition.rb +39 -0
- data/lib/kaleidl/label_style.rb +28 -0
- data/lib/kaleidl/label_template.rb +27 -0
- data/lib/kaleidl/labeller.rb +44 -0
- data/lib/kaleidl/lml/document.rb +30 -0
- data/lib/kaleidl/lml/instance.rb +22 -0
- data/lib/kaleidl/lml/parser.rb +388 -0
- data/lib/kaleidl/lml.rb +11 -0
- data/lib/kaleidl/numeric_sequence.rb +43 -0
- data/lib/kaleidl/profile.rb +66 -0
- data/lib/kaleidl/profile_loader.rb +176 -0
- data/lib/kaleidl/runtime/defaults.rb +35 -0
- data/lib/kaleidl/runtime/engine.rb +301 -0
- data/lib/kaleidl/runtime/renderer.rb +112 -0
- data/lib/kaleidl/runtime/resolver.rb +175 -0
- data/lib/kaleidl/runtime/result.rb +61 -0
- data/lib/kaleidl/runtime.rb +11 -0
- data/lib/kaleidl/sequences/formatters.rb +104 -0
- data/lib/kaleidl/sequences/registry.rb +61 -0
- data/lib/kaleidl/sequences.rb +100 -0
- data/lib/kaleidl/version.rb +5 -0
- data/lib/kaleidl/yaml_loader.rb +157 -0
- data/lib/kaleidl.rb +64 -0
- metadata +136 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kaleidl
|
|
4
|
+
module Sequences
|
|
5
|
+
# Labeling formats: renders a 1-based numeric value in a particular
|
|
6
|
+
# alphanumeric system (MN 112 "labeling format").
|
|
7
|
+
module Formatters
|
|
8
|
+
ROMAN_PAIRS = [
|
|
9
|
+
[1000, "M"], [900, "CM"], [500, "D"], [400, "CD"], [100, "C"],
|
|
10
|
+
[90, "XC"], [50, "L"], [40, "XL"], [10, "X"], [9, "IX"],
|
|
11
|
+
[5, "V"], [4, "IV"], [1, "I"],
|
|
12
|
+
].freeze
|
|
13
|
+
|
|
14
|
+
UNITS = {
|
|
15
|
+
"chinese" => %w[零 一 二 三 四 五 六 七 八 九],
|
|
16
|
+
"japanese" => %w[零 一 二 三 四 五 六 七 八 九],
|
|
17
|
+
"korean" => %w[영 일 이 삼 사 오 육 칠 팔 구],
|
|
18
|
+
}.freeze
|
|
19
|
+
SMALL_TEN = { "chinese" => "十", "japanese" => "十", "korean" => "십" }.freeze
|
|
20
|
+
SMALL_HUNDRED = { "chinese" => "百", "japanese" => "百", "korean" => "백" }.freeze
|
|
21
|
+
SMALL_THOUSAND = { "chinese" => "千", "japanese" => "千", "korean" => "천" }.freeze
|
|
22
|
+
|
|
23
|
+
module_function
|
|
24
|
+
|
|
25
|
+
# Renders the 1-based integer `number` in the given format.
|
|
26
|
+
#
|
|
27
|
+
# @param format [String] decimal, roman_upper, roman_lower,
|
|
28
|
+
# alpha_upper, alpha_lower, arabic_indic, chinese, japanese,
|
|
29
|
+
# korean
|
|
30
|
+
# @param number [Integer] a positive 1-based value
|
|
31
|
+
# @raise [Kaleidl::Error] on unknown format or non-positive value
|
|
32
|
+
def format(format, number)
|
|
33
|
+
raise Kaleidl::Error, "sequence value must be positive, got #{number}" if number.to_i < 1
|
|
34
|
+
|
|
35
|
+
case format
|
|
36
|
+
when "decimal" then number.to_s
|
|
37
|
+
when "roman_upper" then roman(number)
|
|
38
|
+
when "roman_lower" then roman(number).downcase
|
|
39
|
+
when "alpha_upper" then alpha(number)
|
|
40
|
+
when "alpha_lower" then alpha(number).downcase
|
|
41
|
+
when "arabic_indic" then arabic_indic(number)
|
|
42
|
+
when "chinese", "japanese", "korean" then east_asian(format, number)
|
|
43
|
+
else
|
|
44
|
+
raise Kaleidl::Error, "unknown labeling format: #{format.inspect}"
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def roman(number)
|
|
49
|
+
result = +""
|
|
50
|
+
n = number
|
|
51
|
+
ROMAN_PAIRS.each do |value, letters|
|
|
52
|
+
while n >= value
|
|
53
|
+
result << letters
|
|
54
|
+
n -= value
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
result
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Spreadsheet-style letters: 1 -> A ... 26 -> Z, 27 -> AA.
|
|
61
|
+
def alpha(number)
|
|
62
|
+
Kaleidl::Sequences.digits(number, 26)
|
|
63
|
+
.map { |d| (64 + d).chr }
|
|
64
|
+
.join
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def arabic_indic(number)
|
|
68
|
+
number.to_s.tr("0123456789", "٠١٢٣٤٥٦٧٨٩")
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# East-Asian numerals up to 9999: compound tens, hundreds and
|
|
72
|
+
# thousands (十, 百, 千 / 십, 백, 천). The leading unit is dropped
|
|
73
|
+
# before the tens marker only (十, not 一十), while hundreds and
|
|
74
|
+
# thousands keep the unit (一百, 一千) per formal CJK numbering.
|
|
75
|
+
# Internal zeros are not marked (108 -> 一百八), matching
|
|
76
|
+
# document-numbering practice.
|
|
77
|
+
def east_asian(format, number)
|
|
78
|
+
units = UNITS.fetch(format)
|
|
79
|
+
thousands, rest_thousands = number.divmod(1000)
|
|
80
|
+
hundreds, rest_hundreds = rest_thousands.divmod(100)
|
|
81
|
+
tens, ones = rest_hundreds.divmod(10)
|
|
82
|
+
|
|
83
|
+
out = +""
|
|
84
|
+
out << compound(units, thousands, SMALL_THOUSAND.fetch(format),
|
|
85
|
+
drop_single_unit: false) if thousands.positive?
|
|
86
|
+
out << compound(units, hundreds, SMALL_HUNDRED.fetch(format),
|
|
87
|
+
drop_single_unit: false) if hundreds.positive?
|
|
88
|
+
out << compound(units, tens, SMALL_TEN.fetch(format),
|
|
89
|
+
drop_single_unit: true) if tens.positive?
|
|
90
|
+
out << units[ones] if ones.positive?
|
|
91
|
+
out
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def compound(units, count, marker, drop_single_unit: true)
|
|
95
|
+
prefix = if count == 1 && drop_single_unit
|
|
96
|
+
""
|
|
97
|
+
else
|
|
98
|
+
units[count]
|
|
99
|
+
end
|
|
100
|
+
"#{prefix}#{marker}"
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kaleidl
|
|
4
|
+
module Sequences
|
|
5
|
+
# The default labeling sequences a LADL-compliant runtime must
|
|
6
|
+
# provide (MN 112 "NumericSequence"): left-to-right decimal, Roman,
|
|
7
|
+
# alphabetic, Cyrillic, Greek, Chinese, Japanese and Korean numeral
|
|
8
|
+
# sequences; right-to-left Arabic and Hebrew numeral sequences.
|
|
9
|
+
module Registry
|
|
10
|
+
CYRILLIC_UPPER = %w[А Б В Г Д Е Ж З И Й К Л М Н О П Р С Т У Ф Х Ц Ч Ш Щ Ъ Ы Ь Э Ю Я].freeze
|
|
11
|
+
CYRILLIC_LOWER = CYRILLIC_UPPER.map(&:downcase).freeze
|
|
12
|
+
GREEK_UPPER = %w[Α Β Γ Δ Ε Ζ Η Θ Ι Κ Λ Μ Ν Ξ Ο Π Ρ Σ Τ Υ Φ Χ Ψ Ω].freeze
|
|
13
|
+
GREEK_LOWER = GREEK_UPPER.map(&:downcase).freeze
|
|
14
|
+
HEBREW = %w[א ב ג ד ה ו ז ח ט י כ ל מ נ ס ע פ צ ק ר ש ת].freeze
|
|
15
|
+
|
|
16
|
+
module_function
|
|
17
|
+
|
|
18
|
+
# Default sequences by instance name, built fresh on each call so
|
|
19
|
+
# callers cannot mutate the shared registry.
|
|
20
|
+
def defaults
|
|
21
|
+
numeric = lambda do |format|
|
|
22
|
+
Kaleidl::NumericSequence.new(initial_value: 1, increment: 1, format: format)
|
|
23
|
+
end
|
|
24
|
+
arabic_indic_sequence = lambda do
|
|
25
|
+
Kaleidl::NumericSequence.new(
|
|
26
|
+
initial_value: 1, increment: 1, format: "arabic_indic",
|
|
27
|
+
char_direction: "rtl"
|
|
28
|
+
)
|
|
29
|
+
end
|
|
30
|
+
chars = lambda do |characters|
|
|
31
|
+
Kaleidl::CharacterSequence.new(
|
|
32
|
+
characters: characters, expansion_rule: "prepend_prefix"
|
|
33
|
+
)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
{
|
|
37
|
+
"DecimalNumericSequence" => numeric.call("decimal"),
|
|
38
|
+
"ArabicNumericSequence" => numeric.call("arabic_indic"),
|
|
39
|
+
"ArabicIndicNumericSequence" => arabic_indic_sequence.call,
|
|
40
|
+
"RomanUpperNumericSequence" => numeric.call("roman_upper"),
|
|
41
|
+
"RomanLowerNumericSequence" => numeric.call("roman_lower"),
|
|
42
|
+
"AlphaUpperNumericSequence" => numeric.call("alpha_upper"),
|
|
43
|
+
"AlphaLowerNumericSequence" => numeric.call("alpha_lower"),
|
|
44
|
+
"ChineseNumericSequence" => numeric.call("chinese"),
|
|
45
|
+
"JapaneseNumericSequence" => numeric.call("japanese"),
|
|
46
|
+
"KoreanNumericSequence" => numeric.call("korean"),
|
|
47
|
+
"CyrillicUpperCharacterSequence" => chars.call(CYRILLIC_UPPER),
|
|
48
|
+
"CyrillicLowerCharacterSequence" => chars.call(CYRILLIC_LOWER),
|
|
49
|
+
"GreekUpperCharacterSequence" => chars.call(GREEK_UPPER),
|
|
50
|
+
"GreekLowerCharacterSequence" => chars.call(GREEK_LOWER),
|
|
51
|
+
"HebrewNumericSequence" => chars.call(HEBREW),
|
|
52
|
+
}
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Looks up a sequence by instance name in the default registry.
|
|
56
|
+
def lookup(name)
|
|
57
|
+
defaults[name]
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kaleidl
|
|
4
|
+
# Sequence evaluation: computes the value of a sequence model at a
|
|
5
|
+
# position, and provides the default sequences required of a
|
|
6
|
+
# LADL-compliant runtime by MN 112.
|
|
7
|
+
module Sequences
|
|
8
|
+
autoload :Registry, "kaleidl/sequences/registry"
|
|
9
|
+
autoload :Formatters, "kaleidl/sequences/formatters"
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
# MN 112 Sequence#value_for_position. `lookup` resolves
|
|
14
|
+
# NumericSequence#base_sequence references; when omitted, base
|
|
15
|
+
# sequences resolve against the default registry only.
|
|
16
|
+
def value_for(sequence, position, lookup: nil)
|
|
17
|
+
case sequence
|
|
18
|
+
when NumericSequence
|
|
19
|
+
numeric_value(sequence, position, lookup)
|
|
20
|
+
when CharacterSequence
|
|
21
|
+
character_value(sequence, position)
|
|
22
|
+
else
|
|
23
|
+
raise Kaleidl::Error, "unsupported sequence: #{sequence.class}"
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def numeric_value(sequence, position, lookup)
|
|
28
|
+
raw = (sequence.initial_value || 1) + (position - 1) * (sequence.increment || 1)
|
|
29
|
+
if sequence.base_sequence && !sequence.base_sequence.empty?
|
|
30
|
+
base = lookup ? lookup.call(sequence.base_sequence) : Registry.lookup(sequence.base_sequence)
|
|
31
|
+
unless base
|
|
32
|
+
raise Kaleidl::ResolutionError,
|
|
33
|
+
"sequence references undefined base sequence " \
|
|
34
|
+
"#{sequence.base_sequence.inspect}"
|
|
35
|
+
end
|
|
36
|
+
return value_for(base, raw, lookup: lookup)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
format = sequence.format || "decimal"
|
|
40
|
+
length = sequence.cycle_length
|
|
41
|
+
if length && length.positive?
|
|
42
|
+
digits(raw, length).map { |d| Formatters.format(format, d) }.join
|
|
43
|
+
else
|
|
44
|
+
Formatters.format(format, raw)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def character_value(sequence, position)
|
|
49
|
+
chars = Array(sequence.characters)
|
|
50
|
+
raise Kaleidl::SequenceExhaustedError, "sequence has no characters" if chars.empty?
|
|
51
|
+
|
|
52
|
+
pos = position - 1
|
|
53
|
+
return chars[pos] if pos >= 0 && pos < chars.size
|
|
54
|
+
|
|
55
|
+
expand(chars, pos, sequence)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def expand(chars, pos, sequence)
|
|
59
|
+
size = chars.size
|
|
60
|
+
cycle = pos / size
|
|
61
|
+
rule = sequence.expansion_rule
|
|
62
|
+
case rule
|
|
63
|
+
when "repeat_label"
|
|
64
|
+
chars[pos % size]
|
|
65
|
+
when "prepend_prefix"
|
|
66
|
+
prefixes = prefixes_for(sequence, chars)
|
|
67
|
+
prefixes[(cycle - 1) % prefixes.size] + chars[pos % size]
|
|
68
|
+
when "double_character"
|
|
69
|
+
chars[pos % size] * (cycle + 1)
|
|
70
|
+
when "append_suffix"
|
|
71
|
+
suffixes = prefixes_for(sequence, chars)
|
|
72
|
+
chars[pos % size] + suffixes[(cycle - 1) % suffixes.size]
|
|
73
|
+
when "none", nil
|
|
74
|
+
raise Kaleidl::SequenceExhaustedError,
|
|
75
|
+
"sequence exhausted at position #{pos + 1} (expansion_rule none)"
|
|
76
|
+
else
|
|
77
|
+
raise Kaleidl::Error, "unknown expansion_rule: #{rule.inspect}"
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def prefixes_for(sequence, chars)
|
|
82
|
+
prefixes = Array(sequence.expansion_prefix).reject(&:empty?)
|
|
83
|
+
prefixes.empty? ? chars : prefixes
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Bijective (spreadsheet-style) decomposition of a 1-based number
|
|
87
|
+
# into digits of the given radix: 1 -> [1], radix -> [radix],
|
|
88
|
+
# radix + 1 -> [1, 1].
|
|
89
|
+
def digits(number, radix)
|
|
90
|
+
digits = []
|
|
91
|
+
n = number
|
|
92
|
+
while n.positive?
|
|
93
|
+
n -= 1
|
|
94
|
+
digits.unshift((n % radix) + 1)
|
|
95
|
+
n /= radix
|
|
96
|
+
end
|
|
97
|
+
digits
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "set"
|
|
4
|
+
require "yaml"
|
|
5
|
+
|
|
6
|
+
module Kaleidl
|
|
7
|
+
# Loads LADL profiles written in the YAML realization (MN 112 "Label
|
|
8
|
+
# style realizations"): a single document with `schema: mn112/1` and a
|
|
9
|
+
# sequence of `instances`, each carrying exactly `name`, `type` and
|
|
10
|
+
# `properties`; references are `{ref: Name}` mappings.
|
|
11
|
+
#
|
|
12
|
+
# Validation follows the specification's rejection list: aliases and
|
|
13
|
+
# custom tags are refused, repeated mapping keys are refused, the
|
|
14
|
+
# schema value must be `mn112/1`, instance names must be unique, a
|
|
15
|
+
# `ref` mapping carries no other key and a string value, and every
|
|
16
|
+
# reference resolves to a declared instance or a built-in sequence.
|
|
17
|
+
class YamlLoader
|
|
18
|
+
SCHEMA = "mn112/1"
|
|
19
|
+
|
|
20
|
+
class << self
|
|
21
|
+
def load(text)
|
|
22
|
+
new.load(text)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def load(text)
|
|
27
|
+
document = parse_document(text)
|
|
28
|
+
instances = document["instances"].map { |raw| build_instance(raw) }
|
|
29
|
+
ProfileLoader.new.load_instances(instances,
|
|
30
|
+
declared_names: declared_names(instances))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def parse_document(text)
|
|
36
|
+
document = safe_yaml(text)
|
|
37
|
+
unless document.is_a?(Hash) && document.keys.sort == %w[instances schema]
|
|
38
|
+
raise Kaleidl::ParseError,
|
|
39
|
+
"a YAML realization is a mapping with exactly the keys " \
|
|
40
|
+
"`schema` and `instances`"
|
|
41
|
+
end
|
|
42
|
+
unless document["schema"] == SCHEMA
|
|
43
|
+
raise Kaleidl::ParseError,
|
|
44
|
+
"unsupported schema #{document["schema"].inspect}; expected #{SCHEMA.inspect}"
|
|
45
|
+
end
|
|
46
|
+
raise Kaleidl::ParseError, "`instances` must be a sequence" unless document["instances"].is_a?(Array)
|
|
47
|
+
|
|
48
|
+
document
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Psych with aliases disabled refuses anchors/aliases; custom tags
|
|
52
|
+
# raise on safe_load. Repeated mapping keys are refused by walking
|
|
53
|
+
# the node tree, which Psych's scalar loaders otherwise tolerate.
|
|
54
|
+
def safe_yaml(text)
|
|
55
|
+
tree = Psych.parse(text)
|
|
56
|
+
check_node(tree)
|
|
57
|
+
YAML.safe_load(text, aliases: false)
|
|
58
|
+
rescue Psych::Exception => e
|
|
59
|
+
raise Kaleidl::ParseError, "invalid YAML realization: #{e.message}"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def check_node(node)
|
|
63
|
+
case node
|
|
64
|
+
when Psych::Nodes::Alias
|
|
65
|
+
raise Kaleidl::ParseError, "aliases are not allowed in a realization"
|
|
66
|
+
when Psych::Nodes::Mapping
|
|
67
|
+
keys = node.children.each_slice(2).map(&:first)
|
|
68
|
+
names = keys.map do |key|
|
|
69
|
+
unless key.is_a?(Psych::Nodes::Scalar) && !key.tag
|
|
70
|
+
raise Kaleidl::ParseError,
|
|
71
|
+
"mapping keys must be plain scalars"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
key.value
|
|
75
|
+
end
|
|
76
|
+
repeated = names.tally.select { |_, count| count > 1 }
|
|
77
|
+
unless repeated.empty?
|
|
78
|
+
raise Kaleidl::ParseError,
|
|
79
|
+
"repeated mapping keys: #{repeated.keys.join(", ")}"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
node.children.each { |child| check_node(child) }
|
|
83
|
+
when Psych::Nodes::Sequence, Psych::Nodes::Stream
|
|
84
|
+
node.children.each { |child| check_node(child) }
|
|
85
|
+
when Psych::Nodes::Scalar
|
|
86
|
+
if node.tag
|
|
87
|
+
raise Kaleidl::ParseError,
|
|
88
|
+
"custom tags are not allowed in a realization"
|
|
89
|
+
end
|
|
90
|
+
when Psych::Nodes::Document
|
|
91
|
+
check_node(node.root)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def build_instance(raw)
|
|
96
|
+
unless raw.is_a?(Hash) && raw.keys.sort == %w[name properties type]
|
|
97
|
+
raise Kaleidl::ParseError,
|
|
98
|
+
"each instance mapping has exactly the keys `name`, `type` " \
|
|
99
|
+
"and `properties`"
|
|
100
|
+
end
|
|
101
|
+
name = raw["name"]
|
|
102
|
+
type = raw["type"]
|
|
103
|
+
properties = raw["properties"] || {}
|
|
104
|
+
raise Kaleidl::ParseError, "instance name must be a non-empty string" unless name.is_a?(String) && !name.empty?
|
|
105
|
+
unless ProfileLoader::CLASS_FOR_BASE.key?(type)
|
|
106
|
+
raise Kaleidl::ParseError,
|
|
107
|
+
"instance #{name.inspect} realizes unknown model #{type.inspect}"
|
|
108
|
+
end
|
|
109
|
+
unless properties.is_a?(Hash)
|
|
110
|
+
raise Kaleidl::ParseError,
|
|
111
|
+
"instance #{name.inspect} properties must be a mapping"
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
@current_refs = []
|
|
115
|
+
Lml::Instance.new(
|
|
116
|
+
name: name,
|
|
117
|
+
base: type,
|
|
118
|
+
attributes: convert_properties(properties, name),
|
|
119
|
+
references: @current_refs.uniq,
|
|
120
|
+
)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# `{ref: Name}` mappings become the referenced name, the same
|
|
124
|
+
# representation the LML realization yields for a bare reference,
|
|
125
|
+
# and are recorded for the loader's resolution check.
|
|
126
|
+
def convert_properties(value, instance_name)
|
|
127
|
+
case value
|
|
128
|
+
when Hash
|
|
129
|
+
if value.key?("ref")
|
|
130
|
+
unless value.keys == ["ref"]
|
|
131
|
+
raise Kaleidl::ParseError,
|
|
132
|
+
"instance #{instance_name.inspect} has a ref mapping with " \
|
|
133
|
+
"keys other than `ref`"
|
|
134
|
+
end
|
|
135
|
+
ref = value["ref"]
|
|
136
|
+
unless ref.is_a?(String) && !ref.empty?
|
|
137
|
+
raise Kaleidl::ParseError,
|
|
138
|
+
"instance #{instance_name.inspect} has a ref mapping whose " \
|
|
139
|
+
"value is not a string"
|
|
140
|
+
end
|
|
141
|
+
@current_refs << ref
|
|
142
|
+
ref
|
|
143
|
+
else
|
|
144
|
+
value.transform_values { |v| convert_properties(v, instance_name) }
|
|
145
|
+
end
|
|
146
|
+
when Array
|
|
147
|
+
value.map { |v| convert_properties(v, instance_name) }
|
|
148
|
+
else
|
|
149
|
+
value
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def declared_names(instances)
|
|
154
|
+
(ProfileLoader::BUILTIN_SEQUENCE_NAMES + instances.map(&:name)).to_set
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
data/lib/kaleidl.rb
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "lutaml/model"
|
|
4
|
+
|
|
5
|
+
# LADL — the Label Auto-assignment Definition Language.
|
|
6
|
+
#
|
|
7
|
+
# A declarative modeling language for defining automatic label assignment
|
|
8
|
+
# across technical documentation and publishing systems, as specified in
|
|
9
|
+
# MN 112.
|
|
10
|
+
#
|
|
11
|
+
# The gem is organized around the three model trees of MN 112:
|
|
12
|
+
#
|
|
13
|
+
# * The content tree (Kaleidl::ContentElement) — the elements being labeled.
|
|
14
|
+
# * The context tree (Kaleidl::LabelContext) — the label contexts that scope
|
|
15
|
+
# labeller counters.
|
|
16
|
+
# * The labeller tree (Kaleidl::Labeller) — hierarchical labellers that assign
|
|
17
|
+
# labels to content elements.
|
|
18
|
+
#
|
|
19
|
+
# A profile (a LutaML `.lml` instance document) is loaded with
|
|
20
|
+
# Kaleidl::ProfileLoader into a Kaleidl::Profile, resolved into a
|
|
21
|
+
# Kaleidl::Runtime::Resolver, and evaluated against a content tree by the
|
|
22
|
+
# Kaleidl::Runtime::Engine.
|
|
23
|
+
module Kaleidl
|
|
24
|
+
autoload :VERSION, "kaleidl/version"
|
|
25
|
+
autoload :Error, "kaleidl/errors"
|
|
26
|
+
autoload :ParseError, "kaleidl/errors"
|
|
27
|
+
autoload :ResolutionError, "kaleidl/errors"
|
|
28
|
+
autoload :SequenceExhaustedError, "kaleidl/errors"
|
|
29
|
+
|
|
30
|
+
# Core models (MN 112 "Core models")
|
|
31
|
+
autoload :LabelStyle, "kaleidl/label_style"
|
|
32
|
+
autoload :LabelContext, "kaleidl/label_context"
|
|
33
|
+
autoload :Labeller, "kaleidl/labeller"
|
|
34
|
+
autoload :LabelTemplate, "kaleidl/label_template"
|
|
35
|
+
autoload :LabelContextCondition, "kaleidl/label_context_condition"
|
|
36
|
+
autoload :NumericSequence, "kaleidl/numeric_sequence"
|
|
37
|
+
autoload :CharacterSequence, "kaleidl/character_sequence"
|
|
38
|
+
autoload :ContentElement, "kaleidl/content_element"
|
|
39
|
+
autoload :Label, "kaleidl/label"
|
|
40
|
+
|
|
41
|
+
# Profile loading
|
|
42
|
+
autoload :Lml, "kaleidl/lml"
|
|
43
|
+
autoload :Profile, "kaleidl/profile"
|
|
44
|
+
autoload :ProfileLoader, "kaleidl/profile_loader"
|
|
45
|
+
|
|
46
|
+
# Default sequences provided by the runtime (MN 112 "NumericSequence")
|
|
47
|
+
autoload :Sequences, "kaleidl/sequences"
|
|
48
|
+
|
|
49
|
+
# Labeling engine
|
|
50
|
+
autoload :Runtime, "kaleidl/runtime"
|
|
51
|
+
|
|
52
|
+
module_function
|
|
53
|
+
|
|
54
|
+
# Loads a LADL profile from a file path, dispatching on the
|
|
55
|
+
# realization: `.lml` (the LML realization) or `.yaml`/`.yml` (the
|
|
56
|
+
# YAML realization) — MN 112 "Label style realizations" defines both
|
|
57
|
+
# as equally valid, and equivalent realizations derive the same
|
|
58
|
+
# instance data. The engine ships NO bundled profiles: per the
|
|
59
|
+
# release plan, each flavor profile lives in its flavor gem and each
|
|
60
|
+
# taste profile in metanorma-taste.
|
|
61
|
+
def load_profile(path)
|
|
62
|
+
ProfileLoader.load_file(path)
|
|
63
|
+
end
|
|
64
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: kaleidl
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.6
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Ronald Tse
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rhoella
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0.1'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0.1'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: lutaml-model
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0.8'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0.8'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: rake
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '13'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '13'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: rspec
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - "~>"
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '3'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - "~>"
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '3'
|
|
68
|
+
description: |
|
|
69
|
+
Declarative auto-numbering and labeling engine implementing MN 112
|
|
70
|
+
(Label Auto-assignment Definition Language, LADL). Parses labeling
|
|
71
|
+
style profiles written in the LutaML instance syntax and evaluates
|
|
72
|
+
labels, numbers and sequences against a content tree over the three
|
|
73
|
+
LADL model trees: the content tree, the context tree and the
|
|
74
|
+
labeller tree.
|
|
75
|
+
email:
|
|
76
|
+
- ronald.tse@ribose.com
|
|
77
|
+
executables: []
|
|
78
|
+
extensions: []
|
|
79
|
+
extra_rdoc_files: []
|
|
80
|
+
files:
|
|
81
|
+
- LICENSE.txt
|
|
82
|
+
- README.adoc
|
|
83
|
+
- lib/kaleidl.rb
|
|
84
|
+
- lib/kaleidl/character_sequence.rb
|
|
85
|
+
- lib/kaleidl/content_element.rb
|
|
86
|
+
- lib/kaleidl/errors.rb
|
|
87
|
+
- lib/kaleidl/label.rb
|
|
88
|
+
- lib/kaleidl/label_context.rb
|
|
89
|
+
- lib/kaleidl/label_context_condition.rb
|
|
90
|
+
- lib/kaleidl/label_style.rb
|
|
91
|
+
- lib/kaleidl/label_template.rb
|
|
92
|
+
- lib/kaleidl/labeller.rb
|
|
93
|
+
- lib/kaleidl/lml.rb
|
|
94
|
+
- lib/kaleidl/lml/document.rb
|
|
95
|
+
- lib/kaleidl/lml/instance.rb
|
|
96
|
+
- lib/kaleidl/lml/parser.rb
|
|
97
|
+
- lib/kaleidl/numeric_sequence.rb
|
|
98
|
+
- lib/kaleidl/profile.rb
|
|
99
|
+
- lib/kaleidl/profile_loader.rb
|
|
100
|
+
- lib/kaleidl/runtime.rb
|
|
101
|
+
- lib/kaleidl/runtime/defaults.rb
|
|
102
|
+
- lib/kaleidl/runtime/engine.rb
|
|
103
|
+
- lib/kaleidl/runtime/renderer.rb
|
|
104
|
+
- lib/kaleidl/runtime/resolver.rb
|
|
105
|
+
- lib/kaleidl/runtime/result.rb
|
|
106
|
+
- lib/kaleidl/sequences.rb
|
|
107
|
+
- lib/kaleidl/sequences/formatters.rb
|
|
108
|
+
- lib/kaleidl/sequences/registry.rb
|
|
109
|
+
- lib/kaleidl/version.rb
|
|
110
|
+
- lib/kaleidl/yaml_loader.rb
|
|
111
|
+
homepage: https://github.com/kaleidl/kaleidl
|
|
112
|
+
licenses:
|
|
113
|
+
- MIT
|
|
114
|
+
metadata:
|
|
115
|
+
homepage_uri: https://github.com/kaleidl/kaleidl
|
|
116
|
+
source_code_uri: https://github.com/kaleidl/kaleidl
|
|
117
|
+
changelog_uri: https://github.com/kaleidl/kaleidl/releases
|
|
118
|
+
rubygems_mfa_required: 'true'
|
|
119
|
+
rdoc_options: []
|
|
120
|
+
require_paths:
|
|
121
|
+
- lib
|
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
123
|
+
requirements:
|
|
124
|
+
- - ">="
|
|
125
|
+
- !ruby/object:Gem::Version
|
|
126
|
+
version: '3.1'
|
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
|
+
requirements:
|
|
129
|
+
- - ">="
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: '0'
|
|
132
|
+
requirements: []
|
|
133
|
+
rubygems_version: 4.0.20
|
|
134
|
+
specification_version: 4
|
|
135
|
+
summary: Label Auto-assignment Definition Language (LADL) engine
|
|
136
|
+
test_files: []
|