Skip to content

Commit

Permalink
Provide error reports if non-existing IEV reference supplied: metanor…
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Feb 8, 2024
1 parent e08acbf commit be50143
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 264 deletions.
34 changes: 24 additions & 10 deletions lib/relaton_iev.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ class << self
def links_iev2iec60050part(xmldoc)
parts = Set.new
xmldoc.xpath(IEVPATH).each do |x|
m = x&.at(".//locality[@type = 'clause']/referenceFrom")&.text
m = x.at(".//locality[@type = 'clause']/referenceFrom")&.text
&.sub(/^(\d+).*$/, "\\1") or next

parts << m
x["citeas"] = @c.decode(x["citeas"]).sub(/60050/, "60050-#{m}")
x["bibitemid"] = "IEC60050-#{m}"
Expand All @@ -40,18 +39,31 @@ def links_iev2iec60050part(xmldoc)
# @param bibdb [Relaton::Db, NilClass]
# @return [Nokogiri::XML::Element]
def refs_iev2iec60050part(xml, parts, bibdb = nil)
bibdb or return ""
new_iev = ""
parts.sort.each do |p|
hit = bibdb&.fetch("IEC 60050-#{p}", nil, keep_year: true) or next
hit = get_iev_part(bibdb, p) or next
new_iev += refs_iev2iec60050part1(xml, p, hit)
xml.xpath(IEVPATH.gsub(/60050/, "60050-#{p}")).each do |x|
x["citeas"] = @c.decode(x["citeas"])
.sub(/:2011$/, ":#{hit.date[0].on(:year)}")
end
update_iev_refs(xml, p, hit)
end
new_iev
end

def get_iev_part(bibdb, part)
unless hit = bibdb.fetch("IEC 60050-#{part}", nil, keep_year: true)
@err << "The IEV document 60050-#{part} that has been cited " \
"does not exist"
end
hit
end

def update_iev_refs(xml, part, hit)
xml.xpath(IEVPATH.gsub(/60050/, "60050-#{part}")).each do |x|
x["citeas"] = @c.decode(x["citeas"])
.sub(/:2011$/, ":#{hit.date[0].on(:year)}")
end
end

def refs_iev2iec60050part1(xmldoc, part, hit)
date = hit.date[0].on(:year)
return "" if already_contains_ref(xmldoc, part, date)
Expand All @@ -68,12 +80,14 @@ def already_contains_ref(xmldoc, part, date)

# @param xmldoc [Nokogiri::XML::Document]
# @param bibdb [Relaton::Db, NilClass]
# @return [Nokogiri::XML::Element]
# @return [Nokogiri::XML::Element], [String]
def iev_cleanup(xmldoc, bibdb = nil)
@c = HTMLEntities.new
iev = xmldoc.at("//bibitem[docidentifier = 'IEC 60050:2011']") || return
@err = []
iev = xmldoc.at("//bibitem[docidentifier = 'IEC 60050:2011']") or
return [nil, @err]
parts = links_iev2iec60050part(xmldoc)
iev.replace(refs_iev2iec60050part(xmldoc, parts, bibdb))
[iev.replace(refs_iev2iec60050part(xmldoc, parts, bibdb)), @err]
end
end
end
2 changes: 1 addition & 1 deletion lib/relaton_iev/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RelatonIev
VERSION = "1.1.6".freeze
VERSION = "1.2.0".freeze
end

0 comments on commit be50143

Please sign in to comment.