Skip to content

Commit

Permalink
deal with XML when doing CJK extend on titles: #617
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Nov 16, 2024
1 parent b3413bd commit 09bf8b4
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 143 deletions.
100 changes: 1 addition & 99 deletions lib/isodoc/presentation_function/section.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
require_relative "refs"
require_relative "title"

module IsoDoc
class PresentationXMLConvert < ::IsoDoc::Convert
def middle_title(docxml)
s = docxml.at(ns("//sections")) or return
t = @meta.get[:doctitle]
t.nil? || t.empty? and return
s.add_first_child "<p class='zzSTDTitle1'>#{t}</p>"
end

def missing_title(docxml)
docxml.xpath(ns("//definitions[not(./title)]")).each do |d|
# should only be happening for subclauses
d.add_first_child "<title>#{@i18n.symbols}</title>"
end
docxml.xpath(ns("//foreword[not(./title)]")).each do |d|
d.add_first_child "<title>#{@i18n.foreword}</title>"
end
end

def clause(docxml)
docxml.xpath(ns("//clause | //terms | //definitions | //references | " \
"//introduction | //foreword | //preface/abstract | " \
Expand Down Expand Up @@ -57,24 +41,6 @@ def clause1(elem)
t = elem.at(ns("./fmt-title")) and t["depth"] = level
end

def floattitle(docxml)
p = "//clause | //annex | //appendix | //introduction | //foreword | " \
"//preface/abstract | //acknowledgements | //terms | " \
"//definitions | //references | //colophon | //indexsect"
docxml.xpath(ns(p)).each { |f| floattitle1(f) }
# top-level
docxml.xpath(ns("//sections | //preface | //colophon"))
.each { |f| floattitle1(f) }
end

# TODO not currently doing anything with the @depth attribute of floating-title
def floattitle1(elem)
elem.xpath(ns(".//floating-title")).each do |p|
p.name = "p"
p["type"] = "floating-title"
end
end

def annex(docxml)
docxml.xpath(ns("//annex")).each do |f|
@xrefs.klass.single_term_clause?(f) and single_term_clause_retitle(f)
Expand Down Expand Up @@ -134,19 +100,6 @@ def display_order_xpath(docxml, xpath, idx)
idx
end

def preceding_floating_titles(node, idx)
out = node.xpath("./preceding-sibling::*")
.reverse.each_with_object([]) do |p, m|
%w(note admonition p).include?(p.name) or break m
m << p
end
out.reject { |c| c["displayorder"] }.reverse_each do |c|
c["displayorder"] = idx
idx += 1
end
idx
end

def display_order(docxml)
i = 0
d = @xrefs.clause_order(docxml)
Expand All @@ -160,27 +113,6 @@ def display_order(docxml)
end
end

def clausetitle(docxml)
cjk_extended_title(docxml)
end

def cjk_search
lang = %w(zh ja ko).map { |x| "@language = '#{x}'" }.join(" or ")
%(Hans Hant Jpan Hang Kore).include?(@script) and
lang += " or not(@language)"
lang
end

def cjk_extended_title(doc)
l = cjk_search
doc.xpath(ns("//bibdata/title[#{l}] | //floating-title[#{l}] | " \
"//fmt-title[@depth = '1' or not(@depth)][#{l}]")).each do |t|
t.text.size < 4 or next
t.elements.empty? or next # can't be bothered
t.children = @i18n.cjk_extend(t.text)
end
end

def preface_rearrange(doc)
preface_move(doc.at(ns("//preface/abstract")),
%w(foreword introduction clause acknowledgements), doc)
Expand Down Expand Up @@ -214,41 +146,11 @@ def preface_move1(clause, preface, float, prev, xpath)
end
end

def preceding_floats(clause)
ret = []
p = clause
while prev = p.previous_element
if prev.name == "floating-title"
ret << prev
p = prev
else break end
end
ret
end

def rearrange_clauses(docxml)
preface_rearrange(docxml) # feeds toc_title
toc_title(docxml)
end

def toc_title(docxml)
docxml.at(ns("//preface/clause[@type = 'toc']")) and return
ins = toc_title_insert_pt(docxml) or return
id = UUIDTools::UUID.random_create.to_s
ins.previous = <<~CLAUSE
<clause type = 'toc' id='_#{id}'><fmt-title depth='1'>#{@i18n.table_of_contents}</fmt-title></clause>
CLAUSE
end

def toc_title_insert_pt(docxml)
ins = docxml.at(ns("//preface")) ||
docxml.at(ns("//sections | //annex | //bibliography"))
&.before("<preface> </preface>")
&.previous_element or return nil
ins.children.empty? and ins << " "
ins.children.first
end

def toc(docxml)
toc_refs(docxml)
end
Expand Down
107 changes: 107 additions & 0 deletions lib/isodoc/presentation_function/title.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
require_relative "refs"

module IsoDoc
class PresentationXMLConvert < ::IsoDoc::Convert
def middle_title(docxml)
s = docxml.at(ns("//sections")) or return
t = @meta.get[:doctitle]
t.nil? || t.empty? and return
s.add_first_child "<p class='zzSTDTitle1'>#{t}</p>"
end

def missing_title(docxml)
docxml.xpath(ns("//definitions[not(./title)]")).each do |d|
# should only be happening for subclauses
d.add_first_child "<title>#{@i18n.symbols}</title>"
end
docxml.xpath(ns("//foreword[not(./title)]")).each do |d|
d.add_first_child "<title>#{@i18n.foreword}</title>"
end
end

def floattitle(docxml)
p = "//clause | //annex | //appendix | //introduction | //foreword | " \
"//preface/abstract | //acknowledgements | //terms | " \
"//definitions | //references | //colophon | //indexsect"
docxml.xpath(ns(p)).each { |f| floattitle1(f) }
# top-level
docxml.xpath(ns("//sections | //preface | //colophon"))
.each { |f| floattitle1(f) }
end

# TODO not currently doing anything with the @depth attribute of floating-title
def floattitle1(elem)
elem.xpath(ns(".//floating-title")).each do |p|
p.name = "p"
p["type"] = "floating-title"
end
end

def preceding_floating_titles(node, idx)
out = node.xpath("./preceding-sibling::*")
.reverse.each_with_object([]) do |p, m|
%w(note admonition p).include?(p.name) or break m
m << p
end
out.reject { |c| c["displayorder"] }.reverse_each do |c|
c["displayorder"] = idx
idx += 1
end
idx
end

def clausetitle(docxml)
cjk_extended_title(docxml)
end

def cjk_search
lang = %w(zh ja ko).map { |x| "@language = '#{x}'" }.join(" or ")
%(Hans Hant Jpan Hang Kore).include?(@script) and
lang += " or not(@language)"
lang
end

def cjk_extended_title(doc)
l = cjk_search
doc.xpath(ns("//bibdata/title[#{l}] | //floating-title[#{l}] | " \
"//fmt-title[@depth = '1' or not(@depth)][#{l}]"))
.each do |t|
t.text.size < 4 or next
t.traverse do |n|
n.text? or next
n.replace(@i18n.cjk_extend(n.text))
end
end
end

def preceding_floats(clause)
ret = []
p = clause
while prev = p.previous_element
if prev.name == "floating-title"
ret << prev
p = prev
else break end
end
ret
end

def toc_title(docxml)
docxml.at(ns("//preface/clause[@type = 'toc']")) and return
ins = toc_title_insert_pt(docxml) or return
id = UUIDTools::UUID.random_create.to_s
ins.previous = <<~CLAUSE
<clause type = 'toc' id='_#{id}'><fmt-title depth='1'>#{@i18n.table_of_contents}</fmt-title></clause>
CLAUSE
end

def toc_title_insert_pt(docxml)
ins = docxml.at(ns("//preface")) ||
docxml.at(ns("//sections | //annex | //bibliography"))
&.before("<preface> </preface>")
&.previous_element or return nil
ins.children.empty? and ins << " "
ins.children.first
end
end
end
133 changes: 89 additions & 44 deletions spec/isodoc/i18n_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1315,51 +1315,96 @@
</iso-standard>
INPUT
presxml = <<~OUTPUT
<iso-standard xmlns="http://riboseinc.com/isoxml" type="presentation">
<bibdata>
<language current="true">zh</language>
<script current="true">Hans</script>
</bibdata>
<boilerplate>
<copyright-statement>
<clause>
<title depth="1">版 權</title>
</clause>
<clause>
<title depth="1">版權聲明</title>
</clause>
<clause language="en">
<title depth="1">版 權</title>
</clause>
</copyright-statement>
</boilerplate>
<preface>
<clause type="toc" id="_" displayorder="1">
<title depth="1">目 次</title>
</clause>
<p type="floating-title" displayorder="2">樣 板</p>
<abstract obligation="informative" language="jp" displayorder="3">
<title>解 題</title>
</abstract>
<foreword obligation="informative" displayorder="4">
<title>文件序言</title>
<p id="A">This is a preamble</p>
</foreword>
<p type="floating-title" displayorder="5">介紹性陳述</p>
<introduction id="B" obligation="informative" displayorder="6">
<title>簡 介</title>
<clause id="C" inline-header="false" obligation="informative">
<title depth="3">引言部分</title>
<clause id="C" inline-header="false" obligation="informative">
<title depth="3">附則</title>
<iso-standard xmlns="http://riboseinc.com/isoxml" type="presentation">
<bibdata>
<language current="true">zh</language>
<script current="true">Hans</script>
</bibdata>
<boilerplate>
<copyright-statement>
<clause>
<title id="_">版權</title>
<fmt-title depth="1">
<span class="fmt-caption-label">
<semx element="title" source="_">版 權</semx>
</span>
</fmt-title>
</clause>
<clause>
<title id="_">版權聲明</title>
<fmt-title depth="1">
<span class="fmt-caption-label">
<semx element="title" source="_">版權聲明</semx>
</span>
</fmt-title>
</clause>
<clause language="en">
<title id="_">版權</title>
<fmt-title depth="1">
<span class="fmt-caption-label">
<semx element="title" source="_">版 權</semx>
</span>
</fmt-title>
</clause>
</copyright-statement>
</boilerplate>
<preface>
<clause type="toc" id="_" displayorder="1">
<fmt-title depth="1">目 次</fmt-title>
</clause>
</clause>
<clause id="D">
<title language="en" depth="2">Ad</title>
</clause>
</introduction>
</preface>
</iso-standard>
<p type="floating-title" displayorder="2">樣 板</p>
<abstract obligation="informative" language="jp" displayorder="3">
<title id="_">解題</title>
<fmt-title depth="1">
<span class="fmt-caption-label">
<semx element="title" source="_">解 題</semx>
</span>
</fmt-title>
</abstract>
<foreword obligation="informative" displayorder="4">
<title id="_">文件序言</title>
<fmt-title depth="1">
<span class="fmt-caption-label">
<semx element="title" source="_">文件序言</semx>
</span>
</fmt-title>
<p id="A">This is a preamble</p>
</foreword>
<p type="floating-title" displayorder="5">介紹性陳述</p>
<introduction id="B" obligation="informative" displayorder="6">
<title id="_">簡介</title>
<fmt-title depth="1">
<span class="fmt-caption-label">
<semx element="title" source="_">簡 介</semx>
</span>
</fmt-title>
<clause id="C" inline-header="false" obligation="informative">
<title id="_">引言部分</title>
<fmt-title depth="3">
<span class="fmt-caption-label">
<semx element="title" source="_">引言部分</semx>
</span>
</fmt-title>
<clause id="C" inline-header="false" obligation="informative">
<title id="_">附則</title>
<fmt-title depth="3">
<span class="fmt-caption-label">
<semx element="title" source="_">附則</semx>
</span>
</fmt-title>
</clause>
</clause>
<clause id="D">
<title language="en" id="_">Ad</title>
<fmt-title depth="2">
<span class="fmt-caption-label">
<semx element="title" source="_">Ad</semx>
</span>
</fmt-title>
</clause>
</introduction>
</preface>
</iso-standard>
OUTPUT
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert
.new(presxml_options).convert("test", input, true)
Expand Down

0 comments on commit 09bf8b4

Please sign in to comment.