ladl 0.1.0

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.
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ladl
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
+ Ladl::NumericSequence.new(initial_value: 1, increment: 1, format: format)
23
+ end
24
+ arabic_indic_sequence = lambda do
25
+ Ladl::NumericSequence.new(
26
+ initial_value: 1, increment: 1, format: "arabic_indic",
27
+ char_direction: "rtl"
28
+ )
29
+ end
30
+ chars = lambda do |characters|
31
+ Ladl::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 Ladl
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, "ladl/sequences/registry"
9
+ autoload :Formatters, "ladl/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 Ladl::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 Ladl::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 Ladl::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 Ladl::SequenceExhaustedError,
75
+ "sequence exhausted at position #{pos + 1} (expansion_rule none)"
76
+ else
77
+ raise Ladl::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,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ladl
4
+ VERSION = "0.1.0"
5
+ end
data/lib/ladl.rb ADDED
@@ -0,0 +1,78 @@
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 (Ladl::ContentElement) — the elements being labeled.
14
+ # * The context tree (Ladl::LabelContext) — the label contexts that scope
15
+ # labeller counters.
16
+ # * The labeller tree (Ladl::Labeller) — hierarchical labellers that assign
17
+ # labels to content elements.
18
+ #
19
+ # A profile (a LutaML `.lml` instance document) is loaded with
20
+ # Ladl::ProfileLoader into a Ladl::Profile, resolved into a
21
+ # Ladl::Runtime::Resolver, and evaluated against a content tree by the
22
+ # Ladl::Runtime::Engine.
23
+ module Ladl
24
+ autoload :VERSION, "ladl/version"
25
+ autoload :Error, "ladl/errors"
26
+ autoload :ParseError, "ladl/errors"
27
+ autoload :ResolutionError, "ladl/errors"
28
+ autoload :SequenceExhaustedError, "ladl/errors"
29
+
30
+ # Core models (MN 112 "Core models")
31
+ autoload :LabelStyle, "ladl/label_style"
32
+ autoload :LabelContext, "ladl/label_context"
33
+ autoload :Labeller, "ladl/labeller"
34
+ autoload :LabelTemplate, "ladl/label_template"
35
+ autoload :LabelContextCondition, "ladl/label_context_condition"
36
+ autoload :NumericSequence, "ladl/numeric_sequence"
37
+ autoload :CharacterSequence, "ladl/character_sequence"
38
+ autoload :ContentElement, "ladl/content_element"
39
+ autoload :Label, "ladl/label"
40
+
41
+ # Profile loading
42
+ autoload :Lml, "ladl/lml"
43
+ autoload :Profile, "ladl/profile"
44
+ autoload :ProfileLoader, "ladl/profile_loader"
45
+
46
+ # Default sequences provided by the runtime (MN 112 "NumericSequence")
47
+ autoload :Sequences, "ladl/sequences"
48
+
49
+ # Labeling engine
50
+ autoload :Runtime, "ladl/runtime"
51
+
52
+ # Input adapters (thin wrappers over host document models)
53
+ autoload :Adapters, "ladl/adapters"
54
+
55
+ # Path of the bundled ISO/IEC label style profile (MN 112 worked profile).
56
+ ISO_IEC_PROFILE_PATH = File.expand_path("ladl/profiles/iso_iec_label_style.lml", __dir__).freeze
57
+
58
+ # Path of the bundled IETF label style profile (metanorma-ietf#355,
59
+ # conventions verified against xml2rfc and idnits).
60
+ IETF_PROFILE_PATH = File.expand_path("ladl/profiles/ietf_label_style.lml", __dir__).freeze
61
+
62
+ module_function
63
+
64
+ # Loads a LADL profile from a `.lml` file path.
65
+ def load_profile(path)
66
+ ProfileLoader.load_file(path)
67
+ end
68
+
69
+ # Loads the bundled ISO/IEC label style profile (MN 112, ISO/IEC DIR 2).
70
+ def iso_iec_profile
71
+ ProfileLoader.load_file(ISO_IEC_PROFILE_PATH)
72
+ end
73
+
74
+ # Loads the bundled IETF label style profile (RFC XML v3 / xml2rfc).
75
+ def ietf_profile
76
+ ProfileLoader.load_file(IETF_PROFILE_PATH)
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ladl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
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: lutaml-model
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '0.8'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '0.8'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '13'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '13'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rspec
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3'
54
+ description: |
55
+ Declarative auto-numbering and labeling engine implementing MN 112
56
+ (Label Auto-assignment Definition Language, LADL). Parses labeling
57
+ style profiles written in the LutaML instance syntax and evaluates
58
+ labels, numbers and sequences against a content tree over the three
59
+ LADL model trees: the content tree, the context tree and the
60
+ labeller tree.
61
+ email:
62
+ - ronald.tse@ribose.com
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - LICENSE.txt
68
+ - README.adoc
69
+ - lib/ladl.rb
70
+ - lib/ladl/adapters.rb
71
+ - lib/ladl/adapters/metanorma_document.rb
72
+ - lib/ladl/character_sequence.rb
73
+ - lib/ladl/content_element.rb
74
+ - lib/ladl/errors.rb
75
+ - lib/ladl/label.rb
76
+ - lib/ladl/label_context.rb
77
+ - lib/ladl/label_context_condition.rb
78
+ - lib/ladl/label_style.rb
79
+ - lib/ladl/label_template.rb
80
+ - lib/ladl/labeller.rb
81
+ - lib/ladl/lml.rb
82
+ - lib/ladl/lml/document.rb
83
+ - lib/ladl/lml/instance.rb
84
+ - lib/ladl/lml/parser.rb
85
+ - lib/ladl/numeric_sequence.rb
86
+ - lib/ladl/profile.rb
87
+ - lib/ladl/profile_loader.rb
88
+ - lib/ladl/profiles/ietf_label_style.lml
89
+ - lib/ladl/profiles/iso_iec_label_style.lml
90
+ - lib/ladl/runtime.rb
91
+ - lib/ladl/runtime/engine.rb
92
+ - lib/ladl/runtime/renderer.rb
93
+ - lib/ladl/runtime/resolver.rb
94
+ - lib/ladl/runtime/result.rb
95
+ - lib/ladl/sequences.rb
96
+ - lib/ladl/sequences/formatters.rb
97
+ - lib/ladl/sequences/registry.rb
98
+ - lib/ladl/version.rb
99
+ homepage: https://github.com/metanorma/ladl
100
+ licenses:
101
+ - MIT
102
+ metadata:
103
+ homepage_uri: https://github.com/metanorma/ladl
104
+ source_code_uri: https://github.com/metanorma/ladl
105
+ rubygems_mfa_required: 'true'
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '3.1'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubygems_version: 4.0.20
121
+ specification_version: 4
122
+ summary: Label Auto-assignment Definition Language (LADL) engine
123
+ test_files: []