Skip to content

Commit

Permalink
Presentation XML refactor, terms: #630
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Dec 29, 2024
1 parent 47a7a8d commit 4be4eff
Show file tree
Hide file tree
Showing 9 changed files with 1,786 additions and 952 deletions.
1 change: 1 addition & 0 deletions lib/isodoc/function/inline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def children_parse(node, out)
def xref_label_parse(node, out); end

def name_parse(node, out); end
def semx_definition_parse(node, out); end

def floating_title_parse(node, out); end

Expand Down
40 changes: 30 additions & 10 deletions lib/isodoc/function/terms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,42 @@ def modification_parse(node, out)
para.children.each { |n| parse(n, out) }
end

def semx_deprecated_term_parse(node, out); end

def deprecated_term_parse(node, out)
out.p class: "DeprecatedTerms", style: "text-align:left;" do |p|
node.children.each { |c| parse(c, p) }
node.children.each do |c|
if c.name == "p"
out.p class: "DeprecatedTerms", style: "text-align:left;" do |p|
c.children.each { |c1| parse(c1, p) }
end
else parse(c, out)
end
end
end

def semx_admitted_term_parse(node, out); end

def admitted_term_parse(node, out)
out.p class: "AltTerms", style: "text-align:left;" do |p|
node.children.each { |c| parse(c, p) }
node.children.each do |c|
if c.name == "p"
out.p class: "AltTerms", style: "text-align:left;" do |p|
c.children.each { |c1| parse(c1, p) }
end
else parse(c, out)
end
end
end

def semx_term_parse(node, out); end

def term_parse(node, out)
out.p class: "Terms", style: "text-align:left;" do |p|
node.children.each { |c| parse(c, p) }
node.children.each do |c|
if c.name == "p"
out.p class: "Terms", style: "text-align:left;" do |p|
c.children.each { |c1| parse(c1, p) }
end
else parse(c, out)
end
end
end

Expand All @@ -51,16 +72,15 @@ def termnote_parse1(node, para, div)
para&.name != "p" and parse(para, div)
end

def semx_termref_parse(node, out); end

def termref_parse(node, out)
out.p do |p|
node.children.each { |n| parse(n, p) }
end
end

def termdomain_parse(node, out)
node["hidden"] == "true" and return
node.children.each { |n| parse(n, out) }
end
def termdomain_parse(node, out); end

def termdef_parse(node, out)
name = node.at(ns("./fmt-name"))&.remove
Expand Down
15 changes: 10 additions & 5 deletions lib/isodoc/function/to_word_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,17 @@ def parse(node, out)
when "pre" then pre_parse(node, out)
when "annotation" then annotation_parse(node, out)
when "term" then termdef_parse(node, out)
when "preferred" then term_parse(node, out)
when "admitted" then admitted_term_parse(node, out)
when "deprecates" then deprecated_term_parse(node, out)
when "preferred" then semx_term_parse(node, out)
when "fmt-preferred" then term_parse(node, out)
when "admitted" then semx_admitted_term_parse(node, out)
when "fmt-admitted" then admitted_term_parse(node, out)
when "deprecates" then semx_deprecated_term_parse(node, out)
when "fmt-deprecates" then deprecated_term_parse(node, out)
when "domain" then termdomain_parse(node, out)
when "definition" then definition_parse(node, out)
when "termsource" then termref_parse(node, out)
when "definition" then semx_definition_parse(node, out)
when "fmt-definition" then definition_parse(node, out)
when "termsource" then semx_termref_parse(node, out)
when "fmt-termsource" then termref_parse(node, out)
when "modification" then modification_parse(node, out)
when "termnote" then termnote_parse(node, out)
when "terms" then terms_parse(node, out)
Expand Down
17 changes: 15 additions & 2 deletions lib/isodoc/presentation_function/autonum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,23 @@ def transfer_id(old, new)
old.delete("id")
end

def semx_fmt_dup(elem)
elem["id"] ||= "_#{UUIDTools::UUID.random_create}"
new = Nokogiri::XML(<<~XML).root
<semx xmlns='#{elem.namespace.href}' element='#{elem.name}' source='#{elem['original-id'] || elem['id']}'>#{to_xml(elem.children)}</semx>
XML
strip_duplicate_ids(nil, elem, new)
new
end

def gather_all_ids(elem)
elem.xpath(".//*[@id]").each_with_object([]) do |i, m|
m << i["id"]
end
end

# remove ids duplicated between title and fmt-title
# index terms are assumed transferred to fmt-title from title
# remove ids duplicated between sem_title and pres_title
# index terms are assumed transferred to pres_title from sem_title
def strip_duplicate_ids(_node, sem_title, pres_title)
sem_title && pres_title or return
ids = gather_all_ids(pres_title)
Expand All @@ -75,6 +84,10 @@ def autonum(id, num)
"<semx element='autonum' source='#{id}'>#{num}</semx>"
end

def semx_orig(node)
node.parent.parent.at(".//*[@id = '#{node['source']}']")
end

def labelled_autonum(label, id, num)
elem = "<span class='fmt-element-name'>#{label}</span>"
num.blank? and return elem
Expand Down
146 changes: 142 additions & 4 deletions lib/isodoc/presentation_function/concepts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,12 @@ def concept1_ref_content(ref)
end

def related(docxml)
docxml.xpath(ns("//related")).each { |f| related1(f) }
docxml.xpath(ns("//fmt-related/semx")).each { |f| related1(f) }
end

def related1(node)
p = node.at(ns("./preferred"))
ref = node.at(ns("./xref | ./eref | ./termref"))
label = @i18n.relatedterms[node["type"]].upcase
p, ref, orig = related1_prep(node)
label = @i18n.relatedterms[orig["type"]].upcase
if p && ref
node.replace(l10n("<p><strong>#{label}:</strong> " \
"<em>#{to_xml(p)}</em> (#{Common::to_xml(ref)})</p>"))
Expand All @@ -86,5 +85,144 @@ def related1(node)
"<strong>**RELATED TERM NOT FOUND**</strong></p>"))
end
end

def related1_prep(node)
p = node.at(ns("./fmt-preferred"))
ref = node.at(ns("./xref | ./eref | ./termref"))
orig = semx_orig(node)
[p, ref, orig]
end

def related_designation1(desgn)
out = desgn.parent.at(ns("./fmt-#{desgn.name}"))
d1 = semx_fmt_dup(desgn)
%w(preferred admitted deprecates).each do |w|
d = d1.at(ns("./#{w}[last()]")) and d.after("<fmt-#{w}/>")
end
out << d1
end

def designation(docxml)
docxml.xpath(ns("//related")).each { |p| related_designation1(p) }
docxml.xpath(ns("//preferred | //admitted | //deprecates"))
.each { |p| designation1(p) }
docxml.xpath(ns("//fmt-preferred | //fmt-admitted | //fmt-deprecates"))
.each { |t| merge_second_preferred(t) }
docxml.xpath(ns("//fmt-deprecates")).each { |d| deprecates(d) }
docxml.xpath(ns("//fmt-admitted")).each { |d| admits(d) }
end

def deprecates(elem)
ins = elem.at(ns("./p")) || elem
ins.add_first_child @i18n.l10n("#{@i18n.deprecated}: ")
end

def admits(elem); end

def merge_second_preferred(term)
pref = nil
out = term.xpath(ns("./semx")).each_with_index
.with_object([]) do |(p, i), m|
if (i.zero? && (pref = p)) || merge_preferred_eligible?(pref, p)
m << p
else
p.wrap("<p></p>")
end
end
pref&.replace(merge_second_preferred1(out, term))
end

def merge_second_preferred1(desgns, term)
desgns[1..].each(&:remove)
ret = l10n(desgns.map { |x| to_xml(x) }.join("; "))
term.ancestors("fmt-related").empty? and ret = "<p>#{ret}</p>"
ret
end

def merge_preferred_eligible?(first, second)
orig_first, orig_second, firstex, secondex =
merge_preferred_eligible_prep(first, second)
orig_first["geographic-area"] == orig_second["geographic-area"] &&
firstex["language"] == secondex["language"] &&
!orig_first.at(ns("./pronunciation | ./grammar | ./graphical-symbol")) &&
!orig_second.at(ns("./pronunciation | ./grammar | ./graphical-symbol")) &&
orig_first.name == "preferred" && orig_second.name == "preferred"
end

def merge_preferred_eligible_prep(first, second)
orig_first = semx_orig(first)
orig_second = semx_orig(second)
firstex = orig_first.at(ns("./expression")) || {}
secondex = orig_second.at(ns("./expression")) || {}
[orig_first, orig_second, firstex, secondex]
end

def designation1(desgn)
desgn.parent.name == "related" and return
out = desgn.parent.at(ns("./fmt-#{desgn.name}"))
d1 = semx_fmt_dup(desgn)
s = d1.at(ns("./termsource"))
name = d1.at(ns("./expression/name | ./letter-symbol/name | " \
"./graphical-symbol")) or return
designation_annotate(d1, name, desgn)
out << d1
s and out.next = s.wrap("<fmt-termsource></fmt-termsource>")
end

def designation_annotate(desgn, name, orig)
designation_boldface(desgn)
designation_field(desgn, name)
g = desgn.at(ns("./expression/grammar")) and
name << ", #{designation_grammar(g).join(', ')}"
designation_localization(desgn, name, orig)
designation_pronunciation(desgn, name)
designation_bookmarks(desgn, name)
desgn.children = name.children
end

def designation_boldface(desgn)
desgn["element"] == "preferred" or return
name = desgn.at(ns("./expression/name | ./letter-symbol/name")) or return
name.children = "<strong>#{name.children}</strong>"
end

def designation_field(desgn, name)
f = desgn.xpath(ns("./field-of-application | ./usage-info"))
&.map { |u| to_xml(u.children) }&.join(", ")
f&.empty? and return nil
name << ", &#x3c;#{f}&#x3e;"
end

def designation_grammar(grammar)
ret = []
grammar.xpath(ns("./gender | ./number")).each do |x|
ret << @i18n.grammar_abbrevs[x.text]
end
%w(isPreposition isParticiple isAdjective isVerb isAdverb isNoun)
.each do |x|
grammar.at(ns("./#{x}[text() = 'true']")) and
ret << @i18n.grammar_abbrevs[x]
end
ret
end

def designation_localization(desgn, name, orig_desgn)
loc = [desgn.at(ns("./expression/@language"))&.text,
desgn.at(ns("./expression/@script"))&.text,
orig_desgn.at("./@geographic-area")&.text].compact
loc.empty? and return
name << ", #{loc.join(' ')}"
end

def designation_pronunciation(desgn, name)
f = desgn.at(ns("./expression/pronunciation")) or return
name << ", /#{to_xml(f.children)}/"
end

def designation_bookmarks(desgn, name)
desgn.xpath(ns(".//bookmark")).each do |b|
name << b.remove
end
end
end
end
2 changes: 2 additions & 0 deletions lib/isodoc/presentation_function/inline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def quotesource(docxml)
end

def xref1(node)
# Semantic XML
node.ancestors("related, definition, termsource").empty? or return
get_linkend(node)
end

Expand Down
Loading

0 comments on commit 4be4eff

Please sign in to comment.