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/i18n yaml inheritance #550

Merged
merged 4 commits into from
Nov 29, 2023
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
23 changes: 13 additions & 10 deletions lib/isodoc/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ module IsoDoc
class I18n
Hash.include Metanorma::Utils::Hash

def load_yaml1(lang, script)
def yaml_lang(lang, script)
case lang
when "en", "fr", "ru", "de", "es", "ar", "ja"
load_yaml2(lang)
lang
when "zh"
if script == "Hans" then load_yaml2("zh-Hans")
else load_yaml2("en")
end
else
load_yaml2("en")
"#{lang}-#{script}"
end
end

def load_yaml1(lang, script)
load_yaml2(yaml_lang(lang, script))
end

def load_yaml2(str)
YAML.load_file(File.join(File.dirname(__FILE__),
"../isodoc-yaml/i18n-#{str}.yaml"))
f = File.join(File.dirname(__FILE__),
"../isodoc-yaml/i18n-#{str}.yaml")
File.exist?(f) or
f = File.join(File.dirname(__FILE__),
"../isodoc-yaml/i18n-en.yaml")
YAML.load_file(f)
end
end
end

55 changes: 27 additions & 28 deletions lib/isodoc/presentation_function/bibdata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def bibdata(docxml)
"</localized-strings>"
end

def extension_insert(docxml, path = [])
ins = docxml.at(ns("//metanorma-extension")) ||
docxml.at(ns("//bibdata"))&.after("<metanorma-extension/>")&.next_element ||
docxml.root.elements.first.before("<metanorma-extension/>").previous_element
def extension_insert(xml, path = [])
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 extension_insert is too high. [<3, 16, 6> 17.35/15]
Cyclomatic complexity for extension_insert is too high. [7/6]

ins = xml.at(ns("//metanorma-extension")) ||
xml.at(ns("//bibdata"))&.after("<metanorma-extension/>")&.next_element ||
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]

xml.root.elements.first.before("<metanorma-extension/>").previous_element
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]

path.each do |n|
ins = ins.at(ns("./#{n}")) || ins.add_child("<#{n}/>").first
end
Expand All @@ -39,24 +39,24 @@ def passthrough_xslt
@output_formats.empty? and return nil
@output_formats.each_key.with_object([]) do |k, m|
m << <<~XSLT
<preprocess-xslt format="#{k}">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[local-name() = 'passthrough']">
<xsl:if test="contains(@formats,',#{k},')"> <!-- delimited -->
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
</preprocess-xslt>
<preprocess-xslt format="#{k}">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[local-name() = 'passthrough']">
<xsl:if test="contains(@formats,',#{k},')"> <!-- delimited -->
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
</preprocess-xslt>
XSLT
end.join("\n")
end
Expand All @@ -67,8 +67,7 @@ def preprocess_xslt_read
end

def toc_metadata(docxml)
return unless @tocfigures || @toctables || @tocrecommendations

@tocfigures || @toctables || @tocrecommendations or return
ins = extension_insert(docxml)
@tocfigures and
ins << "<toc type='figure'><title>#{@i18n.toc_figures}</title></toc>"
Expand All @@ -89,8 +88,7 @@ def address_precompose(bib)
end

def fonts_metadata(xmldoc)
return unless @fontist_fonts

@fontist_fonts or return
ins = xmldoc.at(ns("//presentation-metadata")) ||
xmldoc.at(ns("//metanorma-extension")) || xmldoc.at(ns("//bibdata"))
CSV.parse_line(@fontist_fonts, col_sep: ";").map(&:strip).each do |f|
Expand Down Expand Up @@ -144,9 +142,10 @@ def hash_translate(bibdata, hash, xpath, lang = @lang)
def edition_translate(bibdata)
x = bibdata.at(ns("./edition")) or return
/^\d+$/.match?(x.text) or return
@i18n.edition_ordinal or return
edn = edition_translate1(x.text.to_i) or return
tag_translate(x, @lang,
@i18n.edition_ordinal.sub(/%(Spellout|Ordinal)?/,
edition_translate1(x.text.to_i)))
@i18n.edition_ordinal.sub(/%(Spellout|Ordinal)?/, edn))
end

def edition_translate1(num)
Expand Down
13 changes: 8 additions & 5 deletions lib/isodoc/presentation_function/inline.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "metanorma-utils"
require "csv"

module IsoDoc
class PresentationXMLConvert < ::IsoDoc::Convert
Expand Down Expand Up @@ -126,16 +127,18 @@ def passthrough1(elem, formats)
elem["formats"] = ",#{elem['formats']},"
end

private

def extract_custom_charsets(docxml)
docxml.xpath(ns("//presentation-metadata/custom-charset-font")).
each_with_object({}) do |x, m|
kv = x.text.split(":", 2)
docxml.xpath(ns("//presentation-metadata/custom-charset-font"))
.each_with_object({}) do |line, m|
line.text.split(",").map(&:strip).each do |x|
kv = x.split(":", 2)
m[kv[0]] = kv[1]
end
end
end

private

def found_matching_variant_sibling(node)
prev = node.xpath("./preceding-sibling::xmlns:variant")
foll = node.xpath("./following-sibling::xmlns:variant")
Expand Down
6 changes: 2 additions & 4 deletions spec/isodoc/presentation_xml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1844,17 +1844,15 @@
it "realises custom charsets" do
input = <<~INPUT
<standard-document xmlns="https://www.metanorma.org/ns/standoc" type="semantic">
<presentation-metadata><custom-charset-font>weather:"OGC Weather Symbols"</custom-charset-font></presentation-metadata>
<presentation-metadata><custom-charset-font>conscript:"Code 2000"</custom-charset-font></presentation-metadata>
<presentation-metadata><custom-charset-font>weather:"OGC Weather Symbols",conscript:"Code 2000"</custom-charset-font></presentation-metadata>
<preface>
<foreword id="A">
<p id="_214f7090-c6d4-8fdc-5e6a-837ebb515871"><span custom-charset="weather">ﶀ</span></p>
</foreword></preface></standard-document>
INPUT
presxml = <<~OUTPUT
<standard-document xmlns="https://www.metanorma.org/ns/standoc" type="presentation">
<presentation-metadata><custom-charset-font>weather:"OGC Weather Symbols"</custom-charset-font></presentation-metadata>
<presentation-metadata><custom-charset-font>conscript:"Code 2000"</custom-charset-font></presentation-metadata>
<presentation-metadata><custom-charset-font>weather:"OGC Weather Symbols",conscript:"Code 2000"</custom-charset-font></presentation-metadata>
<preface><clause type="toc" id="_" displayorder="1"><title depth="1">Table of contents</title></clause>

<foreword id="A" displayorder="2">
Expand Down