Skip to content

Commit

Permalink
Merge pull request #594 from metanorma/fix/defensive_add_first_child
Browse files Browse the repository at this point in the history
Fix/defensive add first child
  • Loading branch information
opoudjis authored Aug 20, 2024
2 parents 0ac08e3 + 92cc1ff commit 2e24301
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 71 deletions.
12 changes: 6 additions & 6 deletions lib/isodoc-yaml/i18n-ja.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ symbols: 記号
table_of_contents: 目次
introduction: 序文
foreword: まえがき
abstract: Abstract
acknowledgements: Acknowledgements
abstract: はじめに
acknowledgements: 謝辞
termsdef: 用語及び定義
termsdefsymbolsabbrev: 用語及び定義並びに記号及び略語
termsdefsymbols: 用語及び定義並びに記号
termsdefabbrev: 用語及び定義並びに略語
normref: 引用規格
bibliography: 参考文献
preface: Preface
section: Section
preface: 序文
section: セクション
clause: 箇条
annex: 附属書
appendix: Appendix
Expand Down Expand Up @@ -123,7 +123,7 @@ locality: {
subclause: 細分箇条,
part: 部,
paragraph: 段落,
chapter: Chapter,
chapter: 第章,
page: ページ,
table: 表,
annex: 附属書,
Expand Down Expand Up @@ -154,7 +154,7 @@ relatedterms:
equivalent: equivalent
compare: compare
contrast: contrast
see: see
see: 参照
seealso: see also
inflection:
Clause:
Expand Down
3 changes: 2 additions & 1 deletion lib/isodoc/function/cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def inline_header_cleanup(docxml)
if n.nil?
x.name = "p"
else
n.children.first.previous = x.remove
#n.children.first.previous = x.remove
n.add_first_child(x.remove)
end
end
docxml
Expand Down
3 changes: 2 additions & 1 deletion lib/isodoc/function/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def symbols_abbrevs(node, out)
# subclause
def symbols_parse(isoxml, out)
isoxml.at(ns("./title")) or
isoxml.children.first.previous = "<title>#{@i18n.symbols}</title>"
#isoxml.children.first.previous = "<title>#{@i18n.symbols}</title>"
isoxml.add_first_child "<title>#{@i18n.symbols}</title>"
clause_parse(isoxml, out)
end

Expand Down
10 changes: 5 additions & 5 deletions lib/isodoc/function/utils.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "metanorma-utils"
require_relative "../../nokogiri/xml/node"

module IsoDoc
module Function
Expand Down Expand Up @@ -27,8 +28,8 @@ def insert_tab(out, count)
[1..count].each { out << tab }
end

def noko(&block)
Metanorma::Utils::noko_html(&block)
def noko(&)
Metanorma::Utils::noko_html(&)
end

def attr_code(attributes)
Expand All @@ -40,8 +41,7 @@ def attr_code(attributes)
DOCTYPE_HDR = "<!DOCTYPE html SYSTEM " \
'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'.freeze

HUGESTRICT =
Nokogiri::XML::ParseOptions::HUGE |
HUGESTRICT = Nokogiri::XML::ParseOptions::HUGE |
Nokogiri::XML::ParseOptions::STRICT

def to_xhtml(xml)
Expand Down Expand Up @@ -243,7 +243,7 @@ def external_path(path)
win = !!((RUBY_PLATFORM =~ /(win|w)(32|64)$/) ||
(RUBY_PLATFORM =~ /mswin|mingw/))
if win
path.gsub!(%{/}, "\\")
path.tr!(%{/}, "\\")
path[/\s/] ? "\"#{path}\"" : path
else path
end
Expand Down
6 changes: 4 additions & 2 deletions lib/isodoc/html_function/postprocess_cover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,17 @@ def coverpage_note_cleanup(docxml)
def html_cover(docxml)
doc = to_xhtml_fragment(File.read(@htmlcoverpage, encoding: "UTF-8"))
d = docxml.at('//div[@class="title-section"]')
d.children.first.add_previous_sibling(
#d.children.first.add_previous_sibling(
d.add_first_child(
populate_template(doc.to_xml(encoding: "US-ASCII"), :html),
)
end

def html_intro(docxml)
doc = to_xhtml_fragment(File.read(@htmlintropage, encoding: "UTF-8"))
d = docxml.at('//div[@class="prefatory-section"]')
d.children.first.add_previous_sibling(
#d.children.first.add_previous_sibling(
d.add_first_child(
populate_template(doc.to_xml(encoding: "US-ASCII"), :html),
)
end
Expand Down
99 changes: 52 additions & 47 deletions lib/isodoc/html_function/postprocess_footnotes.rb
Original file line number Diff line number Diff line change
@@ -1,59 +1,64 @@
module IsoDoc::HtmlFunction
module Html
def update_footnote_filter(fn, x, i, seen)
if seen[fn.text]
x.at("./sup").content = seen[fn.text][:num].to_s
fn.remove unless x["href"] == seen[fn.text][:href]
x["href"] = seen[fn.text][:href]
else
seen[fn.text] = { num: i, href: x["href"] }
x.at("./sup").content = i.to_s
i += 1
module IsoDoc
module HtmlFunction
module Html
def update_footnote_filter(fnote, xref, idx, seen)
if seen[fnote.text]
xref.at("./sup").content = seen[fnote.text][:num].to_s
fnote.remove unless xref["href"] == seen[fnote.text][:href]
xref["href"] = seen[fnote.text][:href]
else
seen[fnote.text] = { num: idx, href: xref["href"] }
xref.at("./sup").content = idx.to_s
idx += 1
end
[idx, seen]
end
[i, seen]
end

def html_footnote_filter(docxml)
seen = {}
i = 1
docxml.xpath('//a[@class = "FootnoteRef"]').each do |x|
fn = docxml.at(%<//*[@id = '#{x['href'].sub(/^#/, '')}']>) || next
i, seen = update_footnote_filter(fn, x, i, seen)
def html_footnote_filter(docxml)
seen = {}
i = 1
docxml.xpath('//a[@class = "FootnoteRef"]').each do |x|
fn = docxml.at(%<//*[@id = '#{x['href'].sub(/^#/, '')}']>) || next
i, seen = update_footnote_filter(fn, x, i, seen)
end
docxml
end
docxml
end

def footnote_backlinks1(x, fn)
xdup = x.dup
xdup.remove["id"]
if fn.elements.empty?
fn.children.first.previous = xdup
else
fn.elements.first.children.first.previous = xdup
def footnote_backlinks1(xref, footnote)
xdup = xref.dup
xdup.remove["id"]
if footnote.elements.empty?
#footnote.children.empty? and footnote << " "
#footnote.children.first.previous = xdup
footnote.add_first_child xdup
else
#footnote.elements.first.children.first.previous = xdup
footnote.elements.first.add_first_child xdup
end
end
end

def footnote_backlinks(docxml)
seen = {}
docxml.xpath('//a[@class = "FootnoteRef"]').each_with_index do |x, i|
(seen[x["href"]] and next) or seen[x["href"]] = true
fn = docxml.at(%<//*[@id = '#{x['href'].sub(/^#/, '')}']>) || next
footnote_backlinks1(x, fn)
x["id"] ||= "fnref:#{i + 1}"
fn.add_child "<a href='##{x['id']}'>&#x21A9;</a>"
def footnote_backlinks(docxml)
seen = {}
docxml.xpath('//a[@class = "FootnoteRef"]').each_with_index do |x, i|
(seen[x["href"]] and next) or seen[x["href"]] = true
fn = docxml.at(%<//*[@id = '#{x['href'].sub(/^#/, '')}']>) || next
footnote_backlinks1(x, fn)
x["id"] ||= "fnref:#{i + 1}"
fn.add_child "<a href='##{x['id']}'>&#x21A9;</a>"
end
docxml
end
docxml
end

def footnote_format(docxml)
docxml.xpath("//a[@class = 'FootnoteRef']/sup").each do |x|
footnote_reference_format(x)
end
docxml.xpath("//a[@class = 'TableFootnoteRef'] | "\
"//span[@class = 'TableFootnoteRef']").each do |x|
table_footnote_reference_format(x)
def footnote_format(docxml)
docxml.xpath("//a[@class = 'FootnoteRef']/sup").each do |x|
footnote_reference_format(x)
end
docxml.xpath("//a[@class = 'TableFootnoteRef'] | "\
"//span[@class = 'TableFootnoteRef']").each do |x|
table_footnote_reference_format(x)
end
docxml
end
docxml
end
end
end
5 changes: 3 additions & 2 deletions lib/isodoc/presentation_function/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ def block_delim
def prefix_name(node, delim, number, elem)
number.nil? || number.empty? and return
unless name = node.at(ns("./#{elem}"))
(node.children.empty? and node.add_child("<#{elem}></#{elem}>")) or
node.children.first.previous = "<#{elem}></#{elem}>"
#(node.children.empty? and node.add_child("<#{elem}></#{elem}>")) or
# node.children.first.previous = "<#{elem}></#{elem}>"
node.add_first_child "<#{elem}></#{elem}>"
name = node.children.first
end
if name.children.empty? then name.add_child(cleanup_entities(number.strip))
Expand Down
3 changes: 2 additions & 1 deletion lib/isodoc/presentation_function/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def eps2svg(img)
def svg_emf_double(img)
if emf?(img["mimetype"])
img = emf_encode(img)
img.children.first.previous = emf_to_svg(img)
#img.children.first.previous = emf_to_svg(img)
img.add_first_child emf_to_svg(img)
elsif img["mimetype"] == "image/svg+xml"
src = svg_to_emf(img) or return
img.add_child("<emf/>")
Expand Down
7 changes: 4 additions & 3 deletions lib/isodoc/presentation_function/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ def middle_title(docxml)
s = docxml.at(ns("//sections")) or return
t = @meta.get[:doctitle]
t.nil? || t.empty? and return
s.children.first.previous =
"<p class='zzSTDTitle1'>#{t}</p>"
#s.children.first.previous =
s.add_first_child "<p class='zzSTDTitle1'>#{t}</p>"
end

def clause(docxml)
Expand Down Expand Up @@ -248,7 +248,8 @@ def toc(docxml)
def toc_refs(docxml)
docxml.xpath(ns("//toc//xref[text()]")).each do |x|
lbl = @xrefs.anchor(x["target"], :label) or next
x.children.first.previous = "#{lbl}<tab/>"
#x.children.first.previous = "#{lbl}<tab/>"
x.add_first_child "#{lbl}<tab/>"
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/isodoc/presentation_function/terms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def designation(docxml)
end

def deprecates(elem)
elem.children.first.previous = @i18n.l10n("#{@i18n.deprecated}: ")
#elem.children.first.previous = @i18n.l10n("#{@i18n.deprecated}: ")
elem.add_first_child @i18n.l10n("#{@i18n.deprecated}: ")
end

def admits(elem); end
Expand Down
6 changes: 4 additions & 2 deletions lib/isodoc/word_function/postprocess_cover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def word_cover(docxml)
cover = File.read(@wordcoverpage, encoding: "UTF-8")
cover = populate_template(cover, :word)
coverxml = to_word_xhtml_fragment(cover)
ins.children.first.previous = coverxml.to_xml(encoding: "US-ASCII")
#ins.children.first.previous = coverxml.to_xml(encoding: "US-ASCII")
ins.add_first_child coverxml.to_xml(encoding: "US-ASCII")
end

def word_intro(docxml, level)
Expand All @@ -31,7 +32,8 @@ def word_intro(docxml, level)
docxml, level)
intro = populate_template(intro, :word)
introxml = to_word_xhtml_fragment(intro)
ins.children.first.previous = introxml.to_xml(encoding: "US-ASCII")
#ins.children.first.previous = introxml.to_xml(encoding: "US-ASCII")
ins.add_first_child introxml.to_xml(encoding: "US-ASCII")
end

# add namespaces for Word fragments
Expand Down
13 changes: 13 additions & 0 deletions lib/nokogiri/xml/node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Nokogiri
module XML
class Node
def add_first_child(content)
if children.empty?
add_child(content)
else
children.first.previous = content
end
end
end
end
end

0 comments on commit 2e24301

Please sign in to comment.