relaton 3.1.7 → 3.1.9

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,292 @@
1
+ module Relaton
2
+ module Eu
3
+ #
4
+ # Map one CELLAR result row (`Cellar::QUERY`) to a generic Bib::ItemData.
5
+ #
6
+ # The act identifier comes from the ELI, never from the CELEX number:
7
+ # CELEX numbers are CELLAR's own sequence, and they differ from the act
8
+ # number for many decisions (ECB decision 2016/245 is CELEX 32016D0002).
9
+ #
10
+ class Parser
11
+ # A work of an act this flavor covers, in its original version. The
12
+ # ELI type segment can carry a subtype (`reg_impl`, `dec_del`, …):
13
+ # implementing and delegated acts are numbered with their act type,
14
+ # and `Pubid::Eu` reads the subtype (pubid >= 2.0.46), so the ELI goes
15
+ # to pubid unchanged; a subtype pubid does not know is a crawl error.
16
+ # A corrigendum (`…/corrigendum/<date>/oj`), and every other ELI type
17
+ # (`C`, `convention`, …), does not match.
18
+ ELI = %r{\Ahttp://data\.europa\.eu/eli/(?:reg|dir|dec)(?:_[a-z]+)?/\d{4}/\d+/oj\z}
19
+
20
+ # The act citation in an Official Journal title, in English or (for an
21
+ # act with no English version) French: `Regulation (EU) No 1227/2011`,
22
+ # `Directive 93/42/EEC`, `Decision (EU) 2016/245`,
23
+ # `Règlement (CEE) n 2015/68`. Only the pair of numbers is read.
24
+ CITATION = %r{\b(Regulation|Directive|Decision|Règlement|Décision)\b[^\d]{0,40}?(\d+)/(\d+)}
25
+
26
+ # The French act words of CITATION, by pubid's class name.
27
+ ACT_WORDS = { "Règlement" => "Regulation", "Décision" => "Decision" }.freeze
28
+
29
+ # CELLAR's language authority code (`…/language/ENG`) → the title's
30
+ # language (ISO 639-1) and script (ISO 15924). A code not here (a
31
+ # language of no EU act) gives no title.
32
+ LANGUAGES = {
33
+ "BUL" => %w[bg Cyrl], "CES" => %w[cs Latn], "DAN" => %w[da Latn],
34
+ "DEU" => %w[de Latn], "ELL" => %w[el Grek], "ENG" => %w[en Latn],
35
+ "EST" => %w[et Latn], "FIN" => %w[fi Latn], "FRA" => %w[fr Latn],
36
+ "GLE" => %w[ga Latn], "HRV" => %w[hr Latn], "HUN" => %w[hu Latn],
37
+ "ITA" => %w[it Latn], "LAV" => %w[lv Latn], "LIT" => %w[lt Latn],
38
+ "MLT" => %w[mt Latn], "NLD" => %w[nl Latn], "POL" => %w[pl Latn],
39
+ "POR" => %w[pt Latn], "RON" => %w[ro Latn], "SLK" => %w[sk Latn],
40
+ "SLV" => %w[sl Latn], "SPA" => %w[es Latn], "SWE" => %w[sv Latn]
41
+ }.freeze
42
+
43
+ # Titles go English, French, German first (EUR-Lex shows the French
44
+ # title of an act with no English version), then by CELLAR code.
45
+ FIRST_LANGUAGES = %w[ENG FRA DEU].freeze
46
+
47
+ # The EEA relevance note in each language (regex source), in the form
48
+ # the Official Journal prints now and in the older forms a sample of
49
+ # ten years (1994-2024) showed five times or more. Rarer typing
50
+ # errors, and the longer note that also names the EU-Switzerland
51
+ # agreement, stay in the title. An apostrophe may be typographic.
52
+ EEA_TEXTS = [
53
+ "Text with EEA relevance", "[ТтT]екст от значение за ЕИП",
54
+ "Text ?s významem pro EHP", "EØS- ?relevant tekst",
55
+ "Tekst af betydning for EØS", "Text (?:von|mit) Bedeutung für den EWR",
56
+ "Κείμενο που παρουσιάζει ενδιαφέρον για τον ΕΟΧ",
57
+ "EMPs kohaldatav tekst", "ETA: ?n kannalta merkityksellinen(?: teksti)?",
58
+ "Texte présentant de l['’] ?intérêt pour l['’] ?EEE",
59
+ "Téacs atá ábhartha maidir le(?:is an)? LEE", "Tekst značajan za EGP",
60
+ "EGT[- ]vonatkozású szöveg", "Testo rilevante ai fini del(?:lo)? SEE",
61
+ "Dokuments attiecas uz EEZ", "Tekstas svarbus EEE",
62
+ "Test b['’] ?r[ei]levanza għ(?:aż|all)-ŻEE", "Voor de EER relevante tekst",
63
+ "Tekst mający znaczenie dla EOG", "Texto relevante para efeitos do EEE",
64
+ "Text cu relevan[țţ][ăa] (?:pentru )?SEE",
65
+ "Text s (?:významom|platnosťou) pre EHP",
66
+ "Besedilo (?:velja za|se nanaša na) EGP",
67
+ "Texto pertinente a (?:efectos|los fines) del EEE",
68
+ "Text av betydelse för EES"
69
+ ].freeze
70
+
71
+ # The note that closes many titles; it is not part of the title.
72
+ # It is matched after `#initialize` collapses each whitespace run in
73
+ # the title to one space, so ` ?` stands for any run (no `\s*`:
74
+ # CodeQL rb/polynomial-redos). The full stop can be inside or after
75
+ # the brackets.
76
+ EEA_NOTE = / ?\(? ?(?:#{EEA_TEXTS.join('|')}) ?\.? ?\)? ?\.?\z/i
77
+
78
+ # A placeholder CELLAR uses for an unknown entry-into-force date.
79
+ PLACEHOLDER_DATE = /\A1001-/
80
+
81
+ PUBLISHER = %w[OP Publications\ Office\ of\ the\ European\ Union].freeze
82
+
83
+ EURLEX = "https://eur-lex.europa.eu/legal-content/EN/TXT/?uri=CELEX:".freeze
84
+
85
+ #
86
+ # @param binding [Hash{String=>Hash}] one SPARQL result row
87
+ # @return [Bib::ItemData, nil] nil for a work the flavor does not cover
88
+ #
89
+ def self.parse(binding)
90
+ new(binding).parse
91
+ end
92
+
93
+ def initialize(binding)
94
+ @row = binding.transform_values { |v| v["value"].to_s.strip }
95
+ .reject { |_, v| v.empty? }
96
+ # The Official Journal writes a no-break space after `No`
97
+ # (`No 1227/2011`); a title keeps plain spaces.
98
+ @titles = read_titles @row["titles"].to_s
99
+ end
100
+
101
+ # @return [Bib::ItemData, nil]
102
+ def parse
103
+ return unless pubid
104
+
105
+ Bib::ItemData.new(
106
+ id: pubid.to_s.gsub(/\W/, ""), type: "standard", title: title,
107
+ docidentifier: docidentifier, date: date, contributor: contributor,
108
+ status: status, relation: relation, source: source,
109
+ language: title.map(&:language), script: title.map(&:script).uniq,
110
+ ext: ext
111
+ )
112
+ end
113
+
114
+ #
115
+ # The act identifier, from the ELI, in the order the act's own title
116
+ # prints it.
117
+ #
118
+ # @return [Pubid::Eu::Identifier, nil] nil when the work has no ELI of
119
+ # an act this flavor covers, or pubid cannot read it
120
+ #
121
+ def pubid
122
+ return @pubid if defined? @pubid
123
+
124
+ @pubid = parse_eli
125
+ end
126
+
127
+ # @return [String, nil] why a work of a covered act was not read: an
128
+ # ELI that matches `ELI` but pubid cannot read. Nil for a work the
129
+ # flavor does not cover, which is skipped and not an error.
130
+ def error
131
+ pubid
132
+ @error
133
+ end
134
+
135
+ private
136
+
137
+ def parse_eli
138
+ return unless ELI.match? @row["eli"]
139
+
140
+ keep_title_order ::Pubid::Eu.parse(@row["eli"])
141
+ rescue ::Pubid::Errors::Error, ArgumentError => e
142
+ @error = "CELEX #{@row['celex']}: pubid cannot read ELI " \
143
+ "`#{@row['eli']}` (#{e.message})"
144
+ nil
145
+ end
146
+
147
+ #
148
+ # Before 2015 a decision or a directive can print `<number>/<year>`
149
+ # (`Decision No 1082/2013/EU`) or `<year>/<number>`
150
+ # (`Decision 2010/87/EU`), and only its title tells. A pair that does
151
+ # not name this act (the title cites another act first), or that reads
152
+ # the same both ways (`2003/2003`), changes nothing.
153
+ #
154
+ def keep_title_order(id)
155
+ number_first = title_order(id)
156
+ unless number_first.nil? || number_first == id.default_number_first?
157
+ id.number_first = number_first
158
+ end
159
+ id
160
+ end
161
+
162
+ def title_order(id)
163
+ type = id.class.name.split("::").last
164
+ year = id.year.to_s
165
+ years = [year, year[2..]]
166
+ citing_title.scan(CITATION) do |act, first, second|
167
+ next unless ACT_WORDS.fetch(act, act) == type
168
+
169
+ number_first = first == id.number && years.include?(second)
170
+ year_first = years.include?(first) && second == id.number
171
+ return number_first if number_first != year_first
172
+ end
173
+ nil
174
+ end
175
+
176
+ def docidentifier
177
+ [
178
+ Bib::Docidentifier.new(type: "EU", content: pubid.to_s, primary: true),
179
+ Bib::Docidentifier.new(type: "CELEX", content: "CELEX #{@row['celex']}"),
180
+ Bib::Docidentifier.new(type: "ELI", content: @row["eli"]),
181
+ ]
182
+ end
183
+
184
+ # The title the act order is read from: English, or French for an act
185
+ # with no English version.
186
+ def citing_title
187
+ @titles["ENG"] || @titles["FRA"] || ""
188
+ end
189
+
190
+ #
191
+ # One title a language, in FIRST_LANGUAGES order then by CELLAR code,
192
+ # without the EEA relevance note.
193
+ #
194
+ # @return [Array<Bib::Title>]
195
+ #
196
+ def title
197
+ @title ||= @titles.sort_by { |code, _| [FIRST_LANGUAGES.index(code) || 3, code] }
198
+ .filter_map do |code, text|
199
+ content = text.sub(EEA_NOTE, "")
200
+ next if content.empty?
201
+
202
+ lang, script = LANGUAGES.fetch(code)
203
+ Bib::Title.new(type: "main", content: content, language: lang, script: script)
204
+ end
205
+ end
206
+
207
+ #
208
+ # `?titles` is `<code>|<title>` pairs joined by Cellar::TITLE_SEP. A
209
+ # language CELLAR has two titles for keeps the first; a code not in
210
+ # LANGUAGES is dropped. The Official Journal writes a no-break space
211
+ # after `No`; a title keeps plain spaces.
212
+ #
213
+ # @return [Hash{String=>String}] CELLAR language code => title
214
+ #
215
+ def read_titles(value)
216
+ value.split(Cellar::TITLE_SEP).each_with_object({}) do |pair, titles|
217
+ code, text = pair.split("|", 2)
218
+ next unless LANGUAGES.key?(code) && !titles.key?(code)
219
+
220
+ text = text.to_s.gsub(/[[:space:]]+/, " ").strip
221
+ titles[code] = text unless text.empty?
222
+ end
223
+ end
224
+
225
+ #
226
+ # `issued`: the date of the document (adoption or signature).
227
+ # `implemented`: the first entry into force; an act can enter into
228
+ # force in stages (`2016/679`: 2016-05-24, then 2018-05-25).
229
+ #
230
+ def date
231
+ dates = []
232
+ dates << Bib::Date.new(type: "issued", at: @row["date"]) if @row["date"]
233
+ force = @row["force"].to_s.split.grep_v(PLACEHOLDER_DATE).min
234
+ dates << Bib::Date.new(type: "implemented", at: force) if force
235
+ dates
236
+ end
237
+
238
+ #
239
+ # Authors from the corporate-body authority (`…/corporate-body/EP`,
240
+ # with its English label), ordered by code; the Publications Office
241
+ # publishes every act.
242
+ #
243
+ def contributor
244
+ authors = @row["authors"].to_s.split("\n").filter_map do |line|
245
+ uri, label = line.split("|", 2)
246
+ code = uri.to_s.split("/").last
247
+ next if code.to_s.empty?
248
+
249
+ [code, label.to_s.empty? ? code : label]
250
+ end
251
+ authors.uniq.sort.map { |code, name| contrib("author", code, name) } <<
252
+ contrib("publisher", *PUBLISHER)
253
+ end
254
+
255
+ def contrib(role, code, name)
256
+ org = Bib::Organization.new(
257
+ name: [Bib::TypedLocalizedString.new(content: name)],
258
+ abbreviation: Bib::LocalizedString.new(content: code),
259
+ )
260
+ Bib::Contributor.new(role: [Bib::Contributor::Role.new(type: role)],
261
+ organization: org)
262
+ end
263
+
264
+ def status
265
+ stage = { "1" => "in-force", "0" => "not-in-force" }[@row["inforce"]]
266
+ Bib::Status.new(stage: Bib::Status::Stage.new(content: stage)) if stage
267
+ end
268
+
269
+ #
270
+ # The acts this act repeals and amends, by CELEX: the target's act
271
+ # identifier is not in the row, and a CELEX number cannot give it.
272
+ #
273
+ def relation
274
+ { "repeals" => "obsoletes", "amends" => "updates" }.flat_map do |key, type|
275
+ @row[key].to_s.split.map do |celex|
276
+ docid = Bib::Docidentifier.new(type: "CELEX", content: "CELEX #{celex}")
277
+ Bib::Relation.new(type: type, bibitem: Bib::ItemData.new(docidentifier: [docid]))
278
+ end
279
+ end
280
+ end
281
+
282
+ def source
283
+ [Bib::Uri.new(type: "src", content: "#{EURLEX}#{@row['celex']}")]
284
+ end
285
+
286
+ def ext
287
+ doctype = pubid.class.name.split("::").last.downcase
288
+ Bib::Ext.new(doctype: Bib::Doctype.new(content: doctype), flavor: "eu")
289
+ end
290
+ end
291
+ end
292
+ end
@@ -0,0 +1,61 @@
1
+ require_relative "../core/processor"
2
+
3
+ module Relaton
4
+ module Eu
5
+ class Processor < Relaton::Core::Processor
6
+ attr_reader :idtype
7
+
8
+ def initialize # rubocop:disable Lint/MissingSuper
9
+ @short = :relaton_eu
10
+ @prefix = "EU"
11
+ @defaultprefix = /^EU\s/
12
+ @idtype = "EU"
13
+ @datasets = %w[eu-cellar eu-cellar-updates]
14
+ # Db cache key and prefixes: `Pubid::Eu` (pubid 2.0.44, pubid#752).
15
+ @pubid_flavor = :Eu
16
+ end
17
+
18
+ # @param code [String, Pubid::Eu::Identifier]
19
+ # @param date [String, nil] year
20
+ # @param opts [Hash]
21
+ # @return [Relaton::Bib::ItemData, nil]
22
+ def get(code, date, opts)
23
+ require_relative "../eu"
24
+ Bibliography.get(code, date, opts)
25
+ end
26
+
27
+ # @param source [String] `eu-cellar` (full crawl) or `eu-cellar-updates`
28
+ # @param opts [Hash] `output:`, `format:`
29
+ def fetch_data(source, opts)
30
+ require_relative "data_fetcher"
31
+ DataFetcher.fetch(source, **opts)
32
+ end
33
+
34
+ # @param xml [String]
35
+ # @return [Relaton::Bib::ItemData]
36
+ def from_xml(xml)
37
+ require_relative "../eu"
38
+ Bib::Item.from_xml xml
39
+ end
40
+
41
+ # @param yaml [String]
42
+ # @return [Relaton::Bib::ItemData]
43
+ def from_yaml(yaml)
44
+ require_relative "../eu"
45
+ Bib::Item.from_yaml yaml
46
+ end
47
+
48
+ # @return [String]
49
+ def grammar_hash
50
+ require_relative "../eu"
51
+ @grammar_hash ||= Eu.grammar_hash
52
+ end
53
+
54
+ def remove_index_file
55
+ require_relative "../eu"
56
+ Relaton::Index.find_or_create(:eu, url: true,
57
+ file: "#{INDEXFILE}.yaml").remove_file
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,9 @@
1
+ module Relaton
2
+ module Eu
3
+ module Util
4
+ extend Relaton::Bib::Util
5
+
6
+ PROGNAME = "relaton-eu".freeze
7
+ end
8
+ end
9
+ end
data/lib/relaton/eu.rb ADDED
@@ -0,0 +1,34 @@
1
+ require "digest/md5"
2
+ require "pubid"
3
+ require_relative "bib"
4
+ require_relative "index"
5
+ require_relative "version"
6
+
7
+ # Relaton::Eu is the EU legal acts flavor (relaton#44): regulations,
8
+ # directives and decisions, cited as `EU Regulation 1227/2011`.
9
+ #
10
+ # `DataFetcher` crawls the Publications Office CELLAR SPARQL endpoint into the
11
+ # `relaton-data-eu` data repo with a pubid `index-v2`, and `Bibliography`
12
+ # reads that index. See docs/eu.md.
13
+ module Relaton
14
+ module Eu
15
+ INDEXFILE = "index-v2".freeze
16
+ # The machine index on the data repo's Pages site (relaton#206).
17
+ PAGES_URL = "https://relaton.github.io/relaton-data-eu/".freeze
18
+ # Where the documents the index names are read from.
19
+ ENDPOINT = "https://raw.githubusercontent.com/relaton/relaton-data-eu/main/".freeze
20
+
21
+ autoload :Util, "relaton/eu/util"
22
+ autoload :Cellar, "relaton/eu/cellar"
23
+ autoload :Parser, "relaton/eu/parser"
24
+ autoload :Bibliography, "relaton/eu/bibliography"
25
+ autoload :DataFetcher, "relaton/eu/data_fetcher"
26
+ autoload :Processor, "relaton/eu/processor"
27
+
28
+ # Returns hash of XML grammar
29
+ # @return [String]
30
+ def self.grammar_hash
31
+ Digest::MD5.hexdigest Relaton::VERSION
32
+ end
33
+ end
34
+ end
@@ -204,16 +204,15 @@ module Relaton
204
204
 
205
205
  # Position of a raw row by its id hash — the raw-side equivalent of
206
206
  # `id_lookup`, built once, so a crawl's add_or_update stays O(1) per row.
207
+ # One pass over the rows; of two rows with one id, the first wins. (A
208
+ # search of the rows for each id made the build O(n²): 2800 s on the
209
+ # 184,528-row relaton-data-eu index.)
207
210
  def raw_position(raw_hash)
208
- @raw_lookup ||= raw_index[1].each_with_object({}) do |row, h|
209
- h[row[:id]] = h.key?(row[:id]) ? h[row[:id]] : rows_index_of(h, row)
211
+ @raw_lookup ||= raw_index[1].each_with_index.with_object({}) do |(row, i), h|
212
+ h[row[:id]] = i unless h.key?(row[:id])
210
213
  end
211
214
  @raw_lookup[raw_hash]
212
215
  end
213
-
214
- def rows_index_of(lookup, row)
215
- raw_index[1].index { |r| r[:id].equal?(row[:id]) || r[:id] == row[:id] }
216
- end
217
216
  # The pre-#242 add path, kept verbatim for seeded/materialized and
218
217
  # pubid-less indexes.
219
218
  def legacy_add(id, file)
@@ -159,7 +159,7 @@ module Relaton
159
159
  year = edition_year(refid)
160
160
  result.each do |r|
161
161
  /\((?:\d{2}\/)?(?<pyear>\d{4})\)/ =~ r.hit[:code]
162
- if !year || year == pyear
162
+ if year_selects?(r.hit, year, pyear)
163
163
  ret = r.item
164
164
  return { ret: ret } if ret
165
165
  end
@@ -171,6 +171,12 @@ module Relaton
171
171
  { years: missed_years }
172
172
  end
173
173
 
174
+ # Does a hit have the year? An ITU-R index hit has no `:code`:
175
+ # HitCollection#request_document has already applied the year.
176
+ def year_selects?(hit, year, pyear)
177
+ !year || year == pyear || !hit[:code]
178
+ end
179
+
174
180
  def itubib_get1(refid)
175
181
  result = search_filter(refid) || return
176
182
  ret = isobib_results_filter(result, refid)
@@ -62,7 +62,7 @@ module Relaton
62
62
  # R-REC the displayed code "BO.1130-5 (02/2026)" -> ITU-R BO.1130-5
63
63
  # R-REP the same, prefixed "BO.1227-2 (1998)" -> Report ITU-R BO.1227-2
64
64
  # R-QUE the same, + a colon "202-2/1" -> ITU-R 202-2/1:
65
- # R-RES the **page id** R-RES-R.1-9-2023 -> ITU-R R.1-9
65
+ # R-RES the **page id** R-RES-R.1-9-2023 -> Resolution ITU-R 1-9
66
66
  # R-HDB the **page id** R-HDB-43-2013 -> ITU-R 43.HDB (2013)
67
67
  #
68
68
  # Three of those need saying out loud. A **Report** is prefixed because
@@ -70,8 +70,13 @@ module Relaton
70
70
  # alone names two different documents (pubid #327 types the prefixed form
71
71
  # `pubid:itu:report`, and `output_file` sends it to `report-itu-r-*.yaml`
72
72
  # instead of colliding). A **Resolution** cannot use its displayed code at
73
- # all: the page renders "Res.1-9 (2023)" while the published record is
74
- # `ITU-R R.1-9`, which only the id carries. A **Handbook** likewise: the
73
+ # all: the page renders "Res.1-9 (2023)", and only the id carries the
74
+ # number. It is prefixed like a Report, in the form ITU cites
75
+ # (`Resolution ITU-R 1-9`), which pubid 2.0.46 types
76
+ # `pubid:itu:resolution` (relaton#402); the `ITU-R R.1-9` the dataset
77
+ # carried before read as a Recommendation of series R, and
78
+ # `output_file` now sends a Resolution to `resolution-itu-r-*.yaml`.
79
+ # A **Handbook** likewise: the
75
80
  # index lists its *title* and the leaf shows only a year — see
76
81
  # #handbook_docid.
77
82
  #
@@ -92,7 +97,7 @@ module Relaton
92
97
  # space. Same result, no ambiguity.
93
98
  code = row[:code].to_s.tr(" ", " ").sub(/\(.*\z/m, "").strip
94
99
  # The two id-derived families, before `code` gets a say.
95
- return "ITU-R #{resolution_number row}" if row[:family] == "R-RES"
100
+ return resolution_docid(row) if row[:family] == "R-RES"
96
101
  return handbook_docid(row) if row[:family] == "R-HDB"
97
102
  return nil if code.empty?
98
103
 
@@ -125,13 +130,18 @@ module Relaton
125
130
  m[:year] ? "ITU-R #{m[:number]}.HDB (#{m[:year]})" : "ITU-R #{m[:number]}.HDB"
126
131
  end
127
132
 
128
- # "R-RES-R.1-9-2023" -> "R.1-9"
133
+ # "R-RES-R.1-9-2023" -> "Resolution ITU-R 1-9".
134
+ #
135
+ # The year is dropped: the revision part names the edition, as a
136
+ # Report's does. The `R.` of the page id is the sector, which the
137
+ # `ITU-R` prefix already states.
129
138
  #
130
139
  # @param row [Hash]
131
140
  # @return [String, nil]
132
- def resolution_number(row)
133
- n = row[:id].to_s.sub(/\AR-RES-/, "").sub(/-(?:19|20)\d{2}\z/, "")
134
- n.empty? ? nil : n
141
+ def resolution_docid(row)
142
+ n = row[:id].to_s.delete_prefix("R-RES-").delete_prefix("R.")
143
+ .sub(/-(?:19|20)\d{2}\z/, "")
144
+ n.empty? ? nil : "Resolution ITU-R #{n}"
135
145
  end
136
146
 
137
147
  # @param row [Hash]
@@ -160,11 +160,10 @@ module Relaton
160
160
  # Pass the reference as a pubid (not a String) so Relaton::Index can
161
161
  # narrow candidates by document number before the block; `index_match?`
162
162
  # then matches every edition when the reference omits the part and the
163
- # exact edition when it names one. Rows are Pubid::Itu objects (not
164
- # Comparable), so rank by the numeric edition in `code.parts` (`["3"]` → 3)
165
- # to return the latest — index order isn't by edition.
163
+ # exact edition when it names one. The hit carries no `:code`, so the
164
+ # stated year is applied here (`#same_year?`), not in Bibliography.
166
165
  row = index.search(ref) { |i| index_match?(ref, i[:id]) }
167
- .max_by { |i| i[:id].code&.parts&.last.to_i }
166
+ .select { |i| same_year? i[:id] }.max_by { |i| edition_rank i[:id] }
168
167
  return unless row
169
168
 
170
169
  url = "#{GH_ITU}#{row[:file]}"
@@ -176,6 +175,40 @@ module Relaton
176
175
  @array = [hit]
177
176
  end
178
177
 
178
+ # Rows are Pubid::Itu objects (not Comparable), so rank by the numeric
179
+ # edition in `code.parts` (`["3"]` → 3), then by year (a handbook edition
180
+ # has no part, only a year) — index order isn't by edition.
181
+ #
182
+ # @param id [::Pubid::Itu::Identifier] an index row's id
183
+ # @return [Array<Integer>]
184
+ def edition_rank(id)
185
+ [id.code&.parts&.last.to_i, id.year.to_i]
186
+ end
187
+
188
+ # Does an ITU-R index row have the year the reference states? A dated row
189
+ # (a handbook edition, `ITU-R 43.HDB (2013)`) must have that year. An
190
+ # undated row (a recommendation, `ITU-R P.838-3`) is named by its edition
191
+ # part, so it stands for any year when the reference names that part
192
+ # (`ITU-R P.838-3 (03/2005)`): its record holds the publication date, and
193
+ # a reference cites the approval date, which can differ in year. A
194
+ # reference with a year and no part (`ITU-R P.838 (1999)`) cannot pick an
195
+ # undated edition, so it answers not-found rather than the newest one.
196
+ #
197
+ # @param id [::Pubid::Itu::Identifier] an index row's id
198
+ # @return [Boolean]
199
+ def same_year?(id)
200
+ year = ref.year
201
+ return true if year.nil?
202
+ return ref_part? if id.year.nil?
203
+
204
+ id.year.to_s == year.to_s
205
+ end
206
+
207
+ # @return [Boolean] true when the reference names an edition part
208
+ def ref_part?
209
+ !ref.code&.parts.to_a.empty?
210
+ end
211
+
179
212
  # Does an index row's id match the reference? When the reference omits the
180
213
  # part/edition (a bare `ITU-R P.838`), every edition of that exact document
181
214
  # matches — "search all parts"; when it names a part (`ITU-R P.838-2`), only
@@ -132,24 +132,37 @@ module Relaton
132
132
  suffixes
133
133
  end
134
134
 
135
- # The bibliography terminator; flavors override to suppress it
135
+ # The bibliography terminator is the i18n `punct.biblio-terminator`
136
+ # declaration, defaulting to "." (the 1.x render1 behaviour,
137
+ # relaton#597): a flavor declaring "" in its isodoc i18n hash --
138
+ # metanorma-iso since metanorma-jis#388 -- suppresses the
139
+ # entry-final period. Flavors still override this method outright
136
140
  # (1.x use_terminator?), receiving the item it terminates. A
137
141
  # reference closing on a footnote closes the sentence before it:
138
142
  # the terminator lands ahead of the trailing fn.
139
143
  FOOTNOTE_CLOSE = "</fn>".freeze
140
144
 
141
- def terminate_reference(ref, _item = nil)
142
- return ref if ref.empty? || ref.rstrip.end_with?(".")
145
+ def terminate_reference(ref, item = nil)
146
+ final = biblio_terminator(item)
147
+ return ref if ref.empty? || ref.rstrip.end_with?(final)
143
148
 
144
149
  if (fn = ref.rindex("<fn"))
145
150
  head = ref[0...fn].rstrip
146
151
  return ref if head.empty?
147
152
 
148
- sep = head.end_with?(".") ? "" : "."
153
+ sep = head.end_with?(final) ? "" : final
149
154
  return "#{head}#{sep}#{ref[fn..]}"
150
155
  end
151
156
 
152
- "#{ref}."
157
+ "#{ref}#{final}"
158
+ end
159
+
160
+ # The terminator as the item's language renders it: each item takes
161
+ # the i18n of the renderer localizing it (the 1.x
162
+ # `@i18n.select(obj)` selection)
163
+ def biblio_terminator(item = nil)
164
+ renderer_for(Array(item&.language).first)
165
+ .i18n.punct_fetch("biblio-terminator", ".")
153
166
  end
154
167
 
155
168
  # The style-keyed capsule ("citation" => { author_date: "..." })
@@ -38,7 +38,12 @@ module Relaton
38
38
  end
39
39
 
40
40
  # Runtime label overrides (a caller's i18n hash): the pack's label
41
- # names win as the key mapping; unknown keys are ignored.
41
+ # names win as the key mapping; unknown keys are ignored. The
42
+ # caller's `punct` subtree carries through (the 1.x behaviour,
43
+ # relaton#597): a flavor declares its bibliography punctuation --
44
+ # e.g. metanorma-iso's `punct: biblio-terminator: ""` suppressing
45
+ # the entry-final period -- in the same isodoc i18n hash that
46
+ # passes the labels.
42
47
  def overlay_hash!(hash)
43
48
  known = %w[
44
49
  edition series_no report_no available_from in and oxford_comma
@@ -48,6 +53,11 @@ module Relaton
48
53
  hash.each do |key, value|
49
54
  labels[key.to_s] = value if known.include?(key.to_s) && !value.nil?
50
55
  end
56
+ # callers symbolize the hash to varying depths (Relaton::Render
57
+ # General#deep_symbolize, metanorma-utils); the punct keys land
58
+ # stringified either way
59
+ sub = hash["punct"] || hash[:punct]
60
+ punct.merge!(sub.to_h { |k, v| [k.to_s, v] }) if sub.is_a?(Hash)
51
61
  self
52
62
  end
53
63
 
@@ -36,6 +36,12 @@ module Relaton
36
36
  @script = script
37
37
  end
38
38
 
39
+ # The engine's localized labels and punctuation, as resolved from
40
+ # the language pack, the caller's i18n hash, and the style's own
41
+ # locale (in rising precedence). General#terminate_reference reads
42
+ # the bibliography terminator here.
43
+ attr_reader :i18n
44
+
39
45
  def render(model, disambiguator: nil)
40
46
  kind = @style.kind_for(model.type)
41
47
  template = @style.template_for(kind, home: home_docid?(model))
@@ -1,3 +1,3 @@
1
1
  module Relaton
2
- VERSION = "3.1.7".freeze
2
+ VERSION = "3.1.9".freeze
3
3
  end