Skip to content

Commit

Permalink
Merge pull request #631 from metanorma/feature/presentation-terms
Browse files Browse the repository at this point in the history
Feature/presentation terms
  • Loading branch information
opoudjis authored Jan 20, 2025
2 parents ffd4c9d + 6905127 commit 9152e58
Show file tree
Hide file tree
Showing 14 changed files with 4,567 additions and 3,450 deletions.
3 changes: 2 additions & 1 deletion lib/isodoc/function/inline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def location_parse(node, out); end
SPAN_UNWRAP_CLASSES =
%w[fmt-caption-label fmt-label-delim fmt-caption-delim fmt-autonum-delim
fmt-element-name fmt-conn fmt-comma fmt-enum-comma fmt-obligation
fmt-xref-container].freeze
fmt-xref-container fmt-designation-field].freeze

def span_parse(node, out)
klass = node["style"] || node["class"]
Expand Down 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
42 changes: 32 additions & 10 deletions lib/isodoc/function/terms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,44 @@ 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 semx_related_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 +74,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
16 changes: 11 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,18 @@ 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 "related" then semx_related_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
18 changes: 16 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,11 @@ def autonum(id, num)
"<semx element='autonum' source='#{id}'>#{num}</semx>"
end

def semx_orig(node, orig = nil)
orig ||= node.parent.parent
orig.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
153 changes: 147 additions & 6 deletions lib/isodoc/presentation_function/concepts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def concept(docxml)
end

def concept1(node)
node.ancestors("definition, termsource, related").empty? or return
xref = node&.at(ns("./xref/@target"))&.text or
return concept_render(node, ital: "true", ref: "true", bold: "false",
linkref: "true", linkmention: "false")
Expand Down Expand Up @@ -71,20 +72,160 @@ 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> " \
node.children =(l10n("<p><strong>#{label}:</strong> " \
"<em>#{to_xml(p)}</em> (#{Common::to_xml(ref)})</p>"))
else
node.replace(l10n("<p><strong>#{label}:</strong> " \
node.children = (l10n("<p><strong>#{label}:</strong> " \
"<strong>**RELATED TERM NOT FOUND**</strong></p>"))
end
end

def related1_prep(node)
p = node.at(ns("./fmt-preferred"))&.children
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)
elem.xpath(ns(".//semx[@element = 'deprecates']")).each do |t|
t.previous = @i18n.l10n("#{@i18n.deprecated}: ")
end
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 << s.wrap("<fmt-termsource></fmt-termsource>").parent
end

def designation_annotate(desgn, name, orig)
designation_boldface(desgn)
designation_field(desgn, name, orig)
#g = desgn.at(ns("./expression/grammar")) and
#name << ", #{designation_grammar(g).join(', ')}"
designation_grammar(desgn, name)
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, orig)
f = orig.xpath(ns("./field-of-application | ./usage-info"))
&.map { |u| to_xml(semx_fmt_dup(u)) }&.join(", ")
f&.empty? and return nil
name << "<span class='fmt-designation-field'>, &#x3c;#{f}&#x3e;</span>"
end

def designation_grammar(desgn, name)
g = desgn.at(ns("./expression/grammar")) or return
ret = []
g.xpath(ns("./gender | ./number")).each do |x|
ret << @i18n.grammar_abbrevs[x.text]
end
%w(isPreposition isParticiple isAdjective isVerb isAdverb isNoun)
.each do |x|
g.at(ns("./#{x}[text() = 'true']")) and ret << @i18n.grammar_abbrevs[x]
end
name << ", #{ret.join(', ')}"
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 9152e58

Please sign in to comment.