6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
# File 'lib/pubid/csa/identifier.rb', line 6
def self.parse(input)
return nil if input.start_with?("#")
return nil if input.match?(/^CSA (Communities|Group|Learning|OnDemand|Update)/)
input = input.gsub("CEI/IEC", "IEC").gsub(/\bCEI\b/, "IEC")
if input.start_with?("CAN/")
wrapped_input = input.sub(/^CAN\//, "")
if wrapped_input.include?("+") ||
(wrapped_input.include?("/") && wrapped_input.match?(/\s+CSA-/)) ||
(wrapped_input.include?("/") && wrapped_input.scan("CSA-").length > 1)
normalized = wrapped_input.sub(/^CSA-/, "CSA ")
normalized = normalized.gsub("CAN/CSA-", "CSA ").gsub("CAN3-",
"CSA ")
normalized = normalized.gsub(/\s+/, " ").strip
tree = Parser.new.parse(normalized)
result = Builder.new.build(tree)
if result
set_publisher_prefix(result, "CAN/CSA-")
if (wrapped_input =~ /\(R(\d{4})\)/) && result.class.attributes.key?(:reaffirmation)
result.reaffirmation = $1
end
end
return result
end
reaffirm_year = nil
reaffirmation_was_4digit = false if wrapped_input =~ /\(R(\d{2,4})\)/
reaffirm_year = $1
reaffirmation_was_4digit = ($1.length == 4) if reaffirm_year.length == 2
year_int = reaffirm_year.to_i
reaffirm_year = year_int < 50 ? "20#{reaffirm_year}" : "19#{reaffirm_year}"
end
wrapped_input = wrapped_input.sub(/\s*\(R\d{2,4}\)/, "")
end
original_prefix = if wrapped_input.start_with?("CSA-")
"CSA-"
elsif wrapped_input.start_with?("CSA ")
"CSA"
end
is_can_csa = true
wrapped_input = wrapped_input.sub(/^CSA-/, "CSA ")
wrapped_identifier = parse(wrapped_input)
return nil unless wrapped_identifier
if wrapped_identifier.class.attributes.key?(:publisher_prefix)
if is_can_csa && wrapped_identifier.is_a?(Identifiers::Series)
wrapped_identifier.publisher_prefix = "CAN/CSA-"
elsif original_prefix
if wrapped_identifier.is_a?(Identifiers::Combined) && wrapped_identifier.first
wrapped_identifier.first.publisher_prefix = original_prefix if wrapped_identifier.first.class.attributes.key?(:publisher_prefix)
else
wrapped_identifier.publisher_prefix = original_prefix
end
end
end
if wrapped_identifier.class.attributes.key?(:reaffirmation) && reaffirm_year
wrapped_identifier.reaffirmation = reaffirm_year
if wrapped_identifier.class.attributes.key?(:original_reaffirmation_4digit)
wrapped_identifier.original_reaffirmation_4digit = reaffirmation_was_4digit
end
end
result = Identifiers::CanadianAdopted.new
result.wrapped_identifier = wrapped_identifier
result.reaffirmation = reaffirm_year if reaffirm_year
return result
end
if input.start_with?("CAN3-")
wrapped_input = input.sub(/^CAN3-/, "CSA ")
has_dash_year = wrapped_input.match?(/-\d{2}\b/)
reaffirm_year = nil
reaffirmation_was_4digit = false if wrapped_input =~ /\(R(\d{2,4})\)/
reaffirm_year = $1
reaffirmation_was_4digit = ($1.length == 4) if reaffirm_year.length == 2
year_int = reaffirm_year.to_i
reaffirm_year = year_int < 50 ? "20#{reaffirm_year}" : "19#{reaffirm_year}"
end
wrapped_input = wrapped_input.sub(/\s*\(R\d{2,4}\)/, "")
end
wrapped_identifier = parse(wrapped_input)
return nil unless wrapped_identifier
if has_dash_year && wrapped_identifier.class.attributes.key?(:year_format)
wrapped_identifier.year_format = "dash"
if wrapped_identifier.class.attributes.key?(:original_year_4digit)
wrapped_identifier.original_year_4digit = false
end
end
if wrapped_identifier.is_a?(Identifiers::Series)
wrapped_identifier.publisher_prefix = "CAN3-"
wrapped_identifier.reaffirmation = reaffirm_year if reaffirm_year
wrapped_identifier.original_reaffirmation_4digit = reaffirmation_was_4digit
return wrapped_identifier
end
if wrapped_identifier.class.attributes.key?(:publisher_prefix)
wrapped_identifier.publisher_prefix = "CAN3-"
end
if wrapped_identifier.class.attributes.key?(:reaffirmation) && reaffirm_year
wrapped_identifier.reaffirmation = reaffirm_year
if wrapped_identifier.class.attributes.key?(:original_reaffirmation_4digit)
wrapped_identifier.original_reaffirmation_4digit = reaffirmation_was_4digit
end
end
result = Identifiers::CanadianAdopted.new
result.wrapped_identifier = wrapped_identifier
result.reaffirmation = reaffirm_year if reaffirm_year
return result
end
if input.match?(/^CSA (ISO\/IEC|CEI\/IEC|CISPR|IEC|CEI|ISO)\s/)
wrapped_input = input.sub(/^CSA\s+/, "")
reaffirm_year = nil
if wrapped_input =~ /\(R(\d{2,4})\)/
reaffirm_year = $1
if reaffirm_year.length == 2
year_int = reaffirm_year.to_i
reaffirm_year = year_int < 50 ? "20#{reaffirm_year}" : "19#{reaffirm_year}"
end
wrapped_input = wrapped_input.sub(/\s*\(R\d{2,4}\)/, "")
end
if wrapped_input =~ /:(\d{2})\b/
short_year_str = $1 short_year_int = short_year_str.to_i
full_year = short_year_int < 50 ? "20#{short_year_str}" : "19#{short_year_str}"
wrapped_input = wrapped_input.sub(/:#{short_year_str}\b/,
":#{full_year}")
end
wrapped_input = wrapped_input.gsub(%r{/A(\d+)([:/-])(\d{2,4})\b}) do
amendment_num = $1
separator = $2
amend_year_str = $3
amend_year_int = amend_year_str.to_i
if amend_year_str.length == 2
amend_full_year = amend_year_int < 50 ? "20#{amend_year_str}" : "19#{amend_year_str}"
else
amend_full_year = amend_year_str
end
"/Amd #{amendment_num}#{separator}#{amend_full_year}"
end
wrapped_identifier = parse_external_standard(wrapped_input)
return nil unless wrapped_identifier
result = Identifiers::CsaAdopted.new
result.wrapped_identifier = wrapped_identifier
result.reaffirmation = reaffirm_year if reaffirm_year
return result
end
if input.match?(/\sPACKAGE\b/i)
case input
when /\sPACKAGE\s+[A-Z]/i
base_input, package_materials = input.split(/\s+PACKAGE\s+/i, 2)
base_input = base_input.strip
package_materials = package_materials ? package_materials.strip : ""
materials_after = true
when /:(\d{2,4})(\s+[^P]+)\s+PACKAGE/i
$1
$2
if input.match?(/,\s+CSA/)
combined_match = input.match(/^(.+?)(\s+&[^P]+)\s+PACKAGE$/i)
if combined_match
base_input = combined_match[1].strip
package_materials = "#{combined_match[2].strip} Package" else
year_match = input.match(/:\d{2,4}/)
if year_match
base_input = input[0..(year_match.end(0) - 1)]
materials_input = input[year_match.end(0)..].strip
package_materials = materials_input
end
end
else
year_match = input.match(/:\d{2,4}/)
if year_match
base_input = input[0..(year_match.end(0) - 1)]
materials_input = input[year_match.end(0)..].strip
package_materials = materials_input base_input = base_input.strip
end
end
materials_after = false
when /^(.+?)\s+PACKAGE\s*$/i
base_input = input.sub(/\s+PACKAGE\s*$/i, "").strip
package_materials = "" materials_after = true
else
tokens = input.split(/\s+/)
base_input = ""
tokens.each do |token|
break if token.match?(/^PACKAGE$/i)
test_input = base_input.empty? ? token : "#{base_input} #{token}"
parsed = parse(test_input)
if parsed
base_input = test_input
else
break
end
end
if base_input && base_input.length < input.length
materials_input = input[base_input.length..].strip
package_materials = materials_input.sub(/\s+PACKAGE\s*$/i,
"").strip
materials_after = !package_materials.empty?
end
end
base_identifier = base_input ? parse(base_input) : nil
return nil unless base_identifier
result = Identifiers::Package.new
result.base_identifier = base_identifier
if package_materials && !package_materials.empty?
result.package_materials = package_materials
end
result.package_keyword = "PACKAGE"
result.materials_after_keyword = materials_after
return result
end
publisher_prefix = if input.start_with?("CAN/CSA-")
"CAN/CSA-"
elsif input.start_with?("CAN3-")
"CAN3-"
elsif input.start_with?("CSA ")
"CSA"
end
has_dash_year = input.match?(/-\d{2}\b/)
normalized = input.gsub("CAN/CSA-", "CSA ")
normalized = normalized.gsub("CAN3-", "CSA ")
tree = Parser.new.parse(normalized)
result = Builder.new.build(tree)
if result && publisher_prefix
set_publisher_prefix(result, publisher_prefix)
end
if result && has_dash_year && result.year_format.nil?
result.year_format = "dash"
end
result
rescue Parslet::ParseFailed => e
raise e
end
|