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

Fix/table capitalisation #498

Merged
merged 4 commits into from
Feb 24, 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
96 changes: 96 additions & 0 deletions lib/isodoc/itu/presentation_ref.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
require_relative "init"
require "roman-numerals"
require "isodoc"
require_relative "../../relaton/render/general"
require_relative "presentation_bibdata"
require_relative "presentation_preface"

module IsoDoc
module ITU
class PresentationXMLConvert < IsoDoc::PresentationXMLConvert
def bibrenderer
::Relaton::Render::ITU::General.new(language: @lang)
end

def bibrender_formattedref(formattedref, _xml)
formattedref << "." unless /\.$/.match?(formattedref.text)
id = reference_format_start(formattedref.parent) and
formattedref.children.first.previous = id
end

def bibrender_relaton(xml, renderings)
f = renderings[xml["id"]][:formattedref]
ids = reference_format_start(xml)
f &&= "<formattedref>#{ids}#{f}</formattedref>"
# retain date in order to generate reference tag
keep = "./docidentifier | ./uri | ./note | ./date | ./biblio-tag"
xml.children = "#{f}#{xml.xpath(ns(keep)).to_xml}"
end

def multi_bibitem_ref_code(bib)
Copy link

Choose a reason for hiding this comment

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

Assignment Branch Condition size for multi_bibitem_ref_code is too high. [<8, 14, 7> 17.58/15]
Cyclomatic complexity for multi_bibitem_ref_code is too high. [7/6]

skip = IsoDoc::Function::References::SKIP_DOCID
skip1 = "@type = 'metanorma' or @type = 'metanorma-ordinal'"
prim = "[@primary = 'true']"
id = bib.xpath(ns("./docidentifier#{prim}[not(#{skip} or #{skip1})]"))
id.empty? and id = bib.xpath(ns("./docidentifier#{prim}[not(#{skip1})]"))
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. [81/80]

id.empty? and id = bib.xpath(ns("./docidentifier[not(#{skip} or #{skip1})]"))
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. [85/80]

id.empty? and id = bib.xpath(ns("./docidentifier[not(#{skip1})]"))
id.empty? and return id
id.sort_by { |i| i["type"] == "ITU" ? 0 : 1 }
end

def render_multi_identifiers(ids)
ids.map do |id|
if id["type"] == "ITU" then doctype_title(id)
else
docid_prefix(id["type"], id.text.sub(/^\[/, "").sub(/\]$/, ""))
end
end.join("&#xA0;| ")
end

def reference_format_start(bib)
id = multi_bibitem_ref_code(bib)
id1 = render_multi_identifiers(id)
out = id1
date = bib.at(ns("./date[@type = 'published']/on | " \
"./date[@type = 'published']/from")) and
out << " (#{date.text.sub(/-.*$/, '')})"
out += ", " if date || !id1.empty?
out
end

def bibliography_bibitem_number1(bib, idx)
mn = bib.at(ns(".//docidentifier[@type = 'metanorma']")) and
/^\[?\d+\]?$/.match?(mn.text) and
mn["type"] = "metanorma-ordinal"
if (mn = bib.at(ns(".//docidentifier[@type = 'metanorma-ordinal']"))) &&
!bibliography_bibitem_number_skip(bib)
idx += 1
mn.children = "[#{idx}]"
end
idx
end

def bibliography_bibitem_number_skip(bibitem)
@xrefs.klass.implicit_reference(bibitem) ||
bibitem["hidden"] == "true" || bibitem.parent["hidden"] == "true"
end

def norm_ref_entry_code(_ordinal, idents, _ids, _standard, datefn, _bib)
Copy link

Choose a reason for hiding this comment

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

Avoid parameter lists longer than 5 parameters. [6/5]

ret = (idents[:metanorma] || idents[:ordinal] || idents[:sdo]).to_s
/^\[.+\]$/.match?(ret) or ret = "[#{ret}]"
ret += datefn
ret.empty? and return ret
ret.gsub("-", "&#x2011;").gsub(/ /, "&#xa0;")
end

def biblio_ref_entry_code(_ordinal, idents, _id, _standard, datefn, _bib)
Copy link

Choose a reason for hiding this comment

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

Avoid parameter lists longer than 5 parameters. [6/5]

ret = (idents[:metanorma] || idents[:ordinal] || idents[:sdo]).to_s
/^\[.+\]$/.match?(ret) or ret = "[#{ret}]"
ret += datefn
ret.empty? and return ret
ret.gsub("-", "&#x2011;").gsub(/ /, "&#xa0;")
end
end
end
end
115 changes: 31 additions & 84 deletions lib/isodoc/itu/presentation_xml_convert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
require_relative "../../relaton/render/general"
require_relative "presentation_bibdata"
require_relative "presentation_preface"
require_relative "presentation_ref"

module Nokogiri
module XML
class Node
def traverse_topdown(&block)
yield(self)
children.each { |j| j.traverse_topdown(&block) }
end
end
end
end

module IsoDoc
module ITU
Expand Down Expand Up @@ -39,6 +51,25 @@ def note1(elem)
super
end

def table1(elem)
elem.xpath(ns("./name | ./thead/tr/th")).each do |n|
capitalise_unless_text_transform(n)
end
super
end

def capitalise_unless_text_transform(elem)
Copy link

Choose a reason for hiding this comment

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

Cyclomatic complexity for capitalise_unless_text_transform is too high. [7/6]

css = nil
elem.traverse_topdown do |n|
n.name == "span" && /text-transform:/.match?(n["style"]) and
css = n
n.text? && /\S/.match?(n.text) or next
css && n.ancestors.include?(css) or
n.replace(::Metanorma::Utils.strict_capitalize_first(n.text))
break
end
end

def get_eref_linkend(node)
non_locality_elems(node).select do |c|
!c.text? || /\S/.match(c)
Expand All @@ -52,57 +83,6 @@ def get_eref_linkend(node)
node.add_child(link)
end

def bibrenderer
::Relaton::Render::ITU::General.new(language: @lang)
end

def bibrender_formattedref(formattedref, _xml)
formattedref << "." unless /\.$/.match?(formattedref.text)
id = reference_format_start(formattedref.parent) and
formattedref.children.first.previous = id
end

def bibrender_relaton(xml, renderings)
f = renderings[xml["id"]][:formattedref]
ids = reference_format_start(xml)
f &&= "<formattedref>#{ids}#{f}</formattedref>"
# retain date in order to generate reference tag
keep = "./docidentifier | ./uri | ./note | ./date | ./biblio-tag"
xml.children = "#{f}#{xml.xpath(ns(keep)).to_xml}"
end

def multi_bibitem_ref_code(bib)
skip = IsoDoc::Function::References::SKIP_DOCID
skip1 = "@type = 'metanorma' or @type = 'metanorma-ordinal'"
prim = "[@primary = 'true']"
id = bib.xpath(ns("./docidentifier#{prim}[not(#{skip} or #{skip1})]"))
id.empty? and id = bib.xpath(ns("./docidentifier#{prim}[not(#{skip1})]"))
id.empty? and id = bib.xpath(ns("./docidentifier[not(#{skip} or #{skip1})]"))
id.empty? and id = bib.xpath(ns("./docidentifier[not(#{skip1})]"))
id.empty? and return id
id.sort_by { |i| i["type"] == "ITU" ? 0 : 1 }
end

def render_multi_identifiers(ids)
ids.map do |id|
if id["type"] == "ITU" then doctype_title(id)
else
docid_prefix(id["type"], id.text.sub(/^\[/, "").sub(/\]$/, ""))
end
end.join("&#xA0;| ")
end

def reference_format_start(bib)
id = multi_bibitem_ref_code(bib)
id1 = render_multi_identifiers(id)
out = id1
date = bib.at(ns("./date[@type = 'published']/on | " \
"./date[@type = 'published']/from")) and
out << " (#{date.text.sub(/-.*$/, '')})"
out += ", " if date || !id1.empty?
out
end

def titlecase(str)
str.gsub(/ |_|-/, " ").split(/ /).map(&:capitalize).join(" ")
end
Expand Down Expand Up @@ -165,39 +145,6 @@ def info(isoxml, out)
super
end

def bibliography_bibitem_number1(bib, idx)
mn = bib.at(ns(".//docidentifier[@type = 'metanorma']")) and
/^\[?\d+\]?$/.match?(mn.text) and
mn["type"] = "metanorma-ordinal"
if (mn = bib.at(ns(".//docidentifier[@type = 'metanorma-ordinal']"))) &&
!bibliography_bibitem_number_skip(bib)
idx += 1
mn.children = "[#{idx}]"
end
idx
end

def bibliography_bibitem_number_skip(bibitem)
@xrefs.klass.implicit_reference(bibitem) ||
bibitem["hidden"] == "true" || bibitem.parent["hidden"] == "true"
end

def norm_ref_entry_code(_ordinal, idents, _ids, _standard, datefn, _bib)
ret = (idents[:metanorma] || idents[:ordinal] || idents[:sdo]).to_s
/^\[.+\]$/.match?(ret) or ret = "[#{ret}]"
ret += datefn
ret.empty? and return ret
ret.gsub("-", "&#x2011;").gsub(/ /, "&#xa0;")
end

def biblio_ref_entry_code(_ordinal, idents, _id, _standard, datefn, _bib)
ret = (idents[:metanorma] || idents[:ordinal] || idents[:sdo]).to_s
/^\[.+\]$/.match?(ret) or ret = "[#{ret}]"
ret += datefn
ret.empty? and return ret
ret.gsub("-", "&#x2011;").gsub(/ /, "&#xa0;")
end

def toc_title(docxml)
@doctype == "resolution" and return
super
Expand Down
7 changes: 7 additions & 0 deletions lib/metanorma/itu/cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ def table_cleanup(xmldoc)
end
end

def header_rows_cleanup(xmldoc)
super
xmldoc.xpath("//table/thead/tr/th").each do |x|
x["align"] = "center"
end
end

def insert_missing_sections(xml)
xml.at("//metanorma-extension/semantic-metadata/" \
"headless[text() = 'true']") and return nil
Expand Down
14 changes: 7 additions & 7 deletions lib/metanorma/itu/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def makexml(node)
end

def doctype(node)
ret = node.attr("doctype")&.gsub(/\s+/, "-")&.downcase || "recommendation"
ret = node.attr("doctype")&.gsub(/\s+/, "-")&.downcase ||
"recommendation"
ret = "recommendation" if ret == "article"
ret
end
Expand Down Expand Up @@ -87,7 +88,7 @@ def sectiontype_streamline(ret)

def sectiontype(node, level = true)
ret = super
hdr = sectiontype_streamline(node&.attr("heading")&.downcase)
hdr = sectiontype_streamline(node.attr("heading")&.downcase)
return nil if ret == "terms and definitions" &&
hdr != "terms and definitions" && node.level > 1
return nil if ret == "symbols and abbreviated terms" &&
Expand Down Expand Up @@ -120,10 +121,8 @@ def clause_parse(attrs, xml, node)
node.option?("unnumbered") and attrs[:unnumbered] = true
case sectiontype1(node)
when "conventions" then attrs = attrs.merge(type: "conventions")
when "history"
attrs[:preface] and attrs = attrs.merge(type: "history")
when "source"
attrs[:preface] and attrs = attrs.merge(type: "source")
when "history", "source"
attrs[:preface] and attrs = attrs.merge(type: sectiontype1(node))
end
super
end
Expand All @@ -141,7 +140,8 @@ def doc_extract_attributes(node)
def presentation_xml_converter(node)
IsoDoc::ITU::PresentationXMLConvert
.new(html_extract_attributes(node)
.merge(output_formats: ::Metanorma::ITU::Processor.new.output_formats))
.merge(output_formats: ::Metanorma::ITU::Processor.new
.output_formats))
end

def html_converter(node)
Expand Down
1 change: 1 addition & 0 deletions metanorma-itu.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")

spec.add_dependency "metanorma-standoc", "~> 2.8.2"
spec.add_dependency "pubid-itu"
spec.add_dependency "twitter_cldr", ">= 3.0.0"
spec.add_dependency "tzinfo-data" # we need this for windows only

Expand Down
Loading
Loading