Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keeptitle attributes to retain provided title text: https://github.co… #876

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions lib/metanorma/standoc/biblio.rng
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@
</define>
<define name="roledescription">
<element name="description">
<ref name="FormattedString"/>
<oneOrMore>
<ref name="TextElement"/>
</oneOrMore>
</element>
</define>
<define name="person">
Expand Down Expand Up @@ -333,7 +335,9 @@
</define>
<define name="affiliationdescription">
<element name="description">
<ref name="FormattedString"/>
<oneOrMore>
<ref name="TextElement"/>
</oneOrMore>
</element>
</define>
<define name="organization">
Expand Down Expand Up @@ -897,7 +901,9 @@
</define>
<define name="formattedref">
<element name="formattedref">
<ref name="FormattedString"/>
<oneOrMore>
<ref name="TextElement"/>
</oneOrMore>
</element>
</define>
<define name="license">
Expand Down Expand Up @@ -942,7 +948,9 @@
<optional>
<attribute name="type"/>
</optional>
<ref name="FormattedString"/>
<zeroOrMore>
<ref name="TextElement"/>
</zeroOrMore>
</define>
<!-- TitleType = ( "alternative" | "original" | "unofficial" | "subtitle" | "main" ) -->
<define name="TypedUri">
Expand Down Expand Up @@ -1042,7 +1050,9 @@
<optional>
<attribute name="script"/>
</optional>
<text/>
<oneOrMore>
<ref name="TextElement"/>
</oneOrMore>
</element>
</define>
<define name="docnumber">
Expand Down Expand Up @@ -1267,12 +1277,16 @@
<optional>
<attribute name="type"/>
</optional>
<ref name="FormattedString"/>
<oneOrMore>
<ref name="TextElement"/>
</oneOrMore>
</element>
</define>
<define name="bibabstract">
<element name="abstract">
<ref name="FormattedString"/>
<oneOrMore>
<ref name="TextElement"/>
</oneOrMore>
</element>
</define>
<define name="copyright">
Expand Down Expand Up @@ -1377,7 +1391,9 @@
</attribute>
<optional>
<element name="description">
<ref name="FormattedString"/>
<oneOrMore>
<ref name="TextElement"/>
</oneOrMore>
</element>
</optional>
<element name="bibitem">
Expand Down
16 changes: 6 additions & 10 deletions lib/metanorma/standoc/cleanup_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ def sections_order_cleanup(xml)
def make_annexes(xml)
xml.xpath("//*[@annex]").each do |y|
y.delete("annex")
next if y.name == "annex" || !y.ancestors("annex").empty?

y.name == "annex" || !y.ancestors("annex").empty? and next
y.wrap("<annex/>")
y.parent["id"] = "_#{UUIDTools::UUID.random_create}"
y.parent["obligation"] = y["obligation"]
Expand All @@ -117,8 +116,7 @@ def maxlevel(xml)

def sections_level_cleanup(xml)
m = maxlevel(xml)
return if m < 6

m < 6 and return
m.downto(6).each do |l|
xml.xpath("//clause[@level = '#{l}']").each do |c|
c.delete("level")
Expand Down Expand Up @@ -173,7 +171,7 @@ def obligations_cleanup_inherit(xml)
r["obligation"] = "normative" unless r["obligation"]
end
xml.xpath(Utils::SUBCLAUSE_XPATH).each do |r|
o = r&.at("./ancestor::*/@obligation")&.text and r["obligation"] = o
o = r.at("./ancestor::*/@obligation")&.text and r["obligation"] = o
end
end

Expand All @@ -183,8 +181,7 @@ def clausebefore_cleanup(xmldoc)
end

def preface_clausebefore_cleanup(xmldoc)
return unless xmldoc.at("//preface")

xmldoc.at("//preface") or return
ins = insert_before(xmldoc, "//preface")
xmldoc.xpath("//preface//*[@beforeclauses = 'true']").each do |x|
x.delete("beforeclauses")
Expand Down Expand Up @@ -232,9 +229,8 @@ def pop_floating_title(xmldoc)
loop do
found = false
xmldoc.xpath("//floating-title").each do |t|
next unless t.next_element.nil?
next if %w(sections annex preface).include? t.parent.name

t.next_element.nil? or next
%w(sections annex preface).include? t.parent.name and next
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.

t.parent.next = t
found = true
end
Expand Down
29 changes: 18 additions & 11 deletions lib/metanorma/standoc/cleanup_section_names.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,32 @@ def replace_title(doc, xpath, text, first = false)
text or return
doc.xpath(xpath).each_with_index do |node, i|
first && !i.zero? and next
node["keeptitle"] == "true" and next
title = get_or_make_title(node)
fn = title.xpath("./fn | ./bookmark")
fn.each(&:remove)
title.children = text
fn.each { |n| title << n }
set_title_with_footnotes(title, text)
end
end

def set_title_with_footnotes(title, text)
fn = title.xpath("./fn | ./bookmark | ./index")
fn.each(&:remove)
title.children = text
fn.each { |n| title << n }
end

def sections_names_cleanup(xml)
replace_title(xml, "//clause[@type = 'scope']", @i18n&.scope)
sections_names_pref_cleanup(xml)
section_names_refs_cleanup(xml)
section_names_terms_cleanup(xml)
xml.xpath("//*[@keeptitle]").each { |s| s.delete("keeptitle") }
end

def sections_names_pref_cleanup(xml)
replace_title(xml, "//preface//abstract", @i18n&.abstract)
replace_title(xml, "//foreword", @i18n&.foreword)
replace_title(xml, "//introduction", @i18n&.introduction)
replace_title(xml, "//acknowledgements", @i18n&.acknowledgements)
section_names_refs_cleanup(xml)
section_names_terms_cleanup(xml)
end

def section_names_refs_cleanup(xml)
Expand Down Expand Up @@ -82,11 +92,8 @@ def section_names_terms1_cleanup(xml)
@i18n&.termsdefsymbolsabbrev, true)
replace_title(xml, "//terms#{NO_SYMABBR} | //clause[.//terms]#{NO_SYMABBR}",
@i18n&.termsdefsymbolsabbrev, true)
replace_title(
xml,
"//terms[not(.//definitions)] | //clause[.//terms][not(.//definitions)]",
@i18n&.termsdef, true
)
replace_title(xml, "//terms[not(.//definitions)] | //clause[.//terms][not(.//definitions)]",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [100/80]

@i18n&.termsdef, true)
end

# do not auto-name terms sections if there are terms subclauses
Expand Down
49 changes: 21 additions & 28 deletions lib/metanorma/standoc/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ module Section
@norm_ref = false

def sectiontype1(node)
return "abstract" if node.attr("style") == "abstract"

node&.attr("heading")&.downcase ||
node.attr("style") == "abstract" and return "abstract"
node.attr("heading")&.downcase ||
node.title
.gsub(%r{<index>.*?</index>}m, "")
.gsub(%r{<fn[^>]*>.*?</fn>}m, "")
Expand All @@ -25,7 +24,7 @@ def sectiontype(node, level = true)
ret = sectiontype1(node)
ret1 = preface_main_filter(sectiontype_streamline(ret), node)
ret1 == "symbols and abbreviated terms" and return ret1
!level || node.level == 1 or return nil
!level || node.level == 1 || node.attr("heading") or return nil
@seen_headers.include? ret and return nil
@seen_headers << ret unless ret1.nil?
@seen_headers_canonical << ret1 unless ret1.nil?
Expand Down Expand Up @@ -59,9 +58,12 @@ def sectiontype_streamline(ret)
["normative references", "terms and definitions", "scope",
"symbols and abbreviated terms", "clause", "bibliography"].freeze

def start_main_section(ret, node)
return if node.role == "preface" || node.attr("style") == "preface"
def role_style(node, value)
node.role == value || node.attr("style") == value
end

def start_main_section(ret, node)
role_style(node, "preface") and return
@preface = false if self.class::MAIN_CLAUSE_NAMES.include?(ret)
@preface = false if self.class::PREFACE_CLAUSE_NAMES
.intersection(@seen_headers_canonical + [ret]).empty?
Expand All @@ -79,29 +81,20 @@ def preface_main_filter(ret, node)
end

def section_attributes(node)
ret = { id: Metanorma::Utils::anchor_or_uuid(node),
language: node.attributes["language"],
script: node.attributes["script"],
number: node.attributes["number"],
"branch-number": node.attributes["branch-number"],
type: node.attributes["type"],
annex: (if (node.attr("style") == "appendix" ||
node.role == "appendix") &&
node.level == 1
true
end),
tag: node&.attr("tag"),
"multilingual-rendering": node&.attr("multilingual-rendering"),
colophon: (if node.role == "colophon" ||
node.attr("style") == "colophon"
true
end),
preface: (if node.role == "preface" ||
node.attr("style") == "preface"
true
end) }
return ret unless node.attributes["change"]
ret =
{ id: Metanorma::Utils::anchor_or_uuid(node),
annex: role_style(node, "appendix") && node.level == 1 ? true : nil,
colophon: role_style(node, "colophon") ? true : nil,
preface: role_style(node, "preface") ? true : nil }
%w(language script number branch-number type tag keeptitle
multilingual-rendering).each do |k|
a = node.attr(k) and ret[k.to_sym] = a
end
section_attributes_change(node, ret).compact
end

def section_attributes_change(node, ret)
node.attributes["change"] or return ret
ret.merge(change: node.attributes["change"],
path: node.attributes["path"],
path_end: node.attributes["path_end"],
Expand Down
14 changes: 5 additions & 9 deletions lib/metanorma/standoc/term_lookup_cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ module Standoc
# Intelligent term lookup xml modifier
class TermLookupCleanup
AUTO_GEN_ID_REGEXP = /\A_/.freeze
EXISTING_TERM_REGEXP = /\Aterm-/.freeze
EXISTING_SYMBOL_REGEXP = /\Asymbol-/.freeze

attr_reader :xmldoc, :lookup, :log

Expand Down Expand Up @@ -61,13 +59,12 @@ def concept_cleanup2
def related_cleanup
xmldoc.xpath("//related").each do |n|
refterm = n.at("./refterm") or next
repl = "<preferred><expression>" \
"<name>#{refterm.children.to_xml}</name></expression></preferred>"
lookup = norm_ref_id_text(refterm.text.strip)
p = @lookup[:sec2prim][lookup] and refterm.children = @c.encode(p)
p || @lookup[:term][lookup] and
refterm.replace(<<~XML,
<preferred><expression><name>#{refterm.children.to_xml}</name></expression></preferred>
XML
)
refterm.replace(repl)
end
end

Expand Down Expand Up @@ -96,7 +93,7 @@ def remove_missing_refs
def remove_missing_ref?(node)
node.at("../eref | ../termref") and return false
xref = node.at("../xref") or return true
xref["target"] && !xref["target"]&.empty? and return false
xref["target"] && !xref["target"].empty? and return false
xref.remove # if xref supplied by user, we won't delete
true
end
Expand Down Expand Up @@ -251,8 +248,7 @@ def norm_ref_id_text(text)
end

def unique_text_id(text, prefix)
@idhash["#{prefix}-#{text}"] or
return "#{prefix}-#{text}"
@idhash["#{prefix}-#{text}"] or return "#{prefix}-#{text}"
(1..Float::INFINITY).lazy.each do |index|
@idhash["#{prefix}-#{text}-#{index}"] or
break("#{prefix}-#{text}-#{index}")
Expand Down
Loading
Loading