Skip to content

Commit

Permalink
first cut, pubid-itu integration: #487
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Feb 20, 2024
1 parent bf770ce commit 9252a03
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 deletions.
74 changes: 63 additions & 11 deletions lib/metanorma/itu/front_id.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
require "pubid-itu"

module Metanorma
module ITU
class Converter < Standoc::Converter
def metadata_id(node, xml)
provisional_id(node, xml)
td_id(node, xml)
itu_id(node, xml)
if id = node.attr("docidentifier")
xml.docidentifier id, **attr_code(type: "ITU")
else itu_id(node, xml)
end
recommendation_id(node, xml)
end

Expand Down Expand Up @@ -37,19 +42,67 @@ def itu_id1(node, lang)
end

def itu_id(node, xml)
return unless node.attr("docnumber") || node.attr("docidentifier")
node.attr("docnumber") or return
params = itu_id_params(node)
itu_id_out(xml, params)
end

xml.docidentifier type: "ITU" do |i|
i << (node.attr("docidentifier") || itu_id1(node, false))
end
xml.docidentifier type: "ITU-lang" do |i|
i << itu_id1(node, true)
def compact_blank(hash)
hash.compact.reject { |_, v| v.is_a?(String) && v.empty? }
end

def itu_id_pub(node)
(node.attr("publisher") || default_publisher).split(/[;,]/)
.map(&:strip).map { |x| org_abbrev[x] || x }
end

def itu_id_params(node)
itu_id_params_core(node).merge(itu_id_params_add(node))
end

def itu_id_params_core(node)
pub = itu_id_pub(node)
num = node.attr("docnumber")
ret = { sector: node.attr("bureau") || "T",
publisher: pub[0],
copublisher: pub[1..-1] }
ret.merge!(itu_id_params_num(num))
ret[:copublisher].empty? and ret.delete(:copublisher)
ret
end

def itu_id_params_num(num)
if m = /^(?:(?<series>[A-Z])\.)?(?<number>\d+)$/.match(num)
{ series: m[:series], number: m[:number] }
else { number: num }
end
end

def recommendation_id(node, xml)
return unless node.attr("recommendationnumber")
def itu_id_params_add(node)
ret = { part: node.attr("partnumber"),
language: node.attr("language") || "en" }
compact_blank(ret)
end

def itu_id_out(xml, params)
xml.docidentifier itu_id_default(params).to_s,
**attr_code(type: "ITU")
xml.docidentifier itu_id_lang(params).to_s,
**attr_code(type: "ITU-lang")
end

def itu_id_default(params)
p = params.dup
p.delete(:language)
Pubid::Itu::Identifier.create(**p)
end

def itu_id_lang(params)
Pubid::Itu::Identifier.create(**params)
end

def recommendation_id(node, xml)
node.attr("recommendationnumber") or return
node.attr("recommendationnumber").split("/").each do |s|
xml.docidentifier type: "ITU-Recommendation" do |i|
i << s
Expand All @@ -58,8 +111,7 @@ def recommendation_id(node, xml)
end

def structured_id(node, xml)
return unless node.attr("docnumber")

node.attr("docnumber") or return
xml.structuredidentifier do |i|
i.bureau node.attr("bureau") || "T"
i.docnumber node.attr("docnumber")
Expand Down
1 change: 0 additions & 1 deletion spec/metanorma/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,6 @@
<title language='fr' format='text/plain' type='subtitle'>Soustitre</title>
<docidentifier type='ITU-provisional'>ABC</docidentifier>
<docidentifier type='ITU'>OVERRIDE</docidentifier>
<docidentifier type='ITU-lang'>ITU-R 1000-E</docidentifier>
<docnumber>1000</docnumber>
<contributor>
<role type='author'/>
Expand Down

0 comments on commit 9252a03

Please sign in to comment.