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

implement text-transform style: https://github.com/metanorma/metanorm… #558

Merged
merged 1 commit into from
Feb 9, 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
37 changes: 37 additions & 0 deletions lib/isodoc/presentation_function/inline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,43 @@ def date1(elem)

def inline_format(docxml)
custom_charset(docxml)
text_transform(docxml)
end

def text_transform(docxml)
docxml.xpath(ns("//span[contains(@style, 'text-transform')]")).each do |s|
text_transform1(s)
end
end

def text_transform1(span)
m = span["style"].split(/;\s*/)
i = m.index { |x| /^text-transform/.match?(x) }
value = m[i].sub(/^text-transform:/, "")
change_case(span, value, true)
m[i] = "text-transform:none"
span["style"] = m.join(";")
end

def change_case(span, value, seen_space)
span.traverse do |s|
s.text? or next
case value
when "uppercase" then s.replace s.text.upcase
when "lowercase" then s.replace s.text.downcase
when "capitalize"
s.replace conditional_capitalize(s.text, seen_space)
end
seen_space = /\s$/.match?(s.text)
end
end

def conditional_capitalize(text, seen_space)
m = text.split(/(?<=\s)/)
((seen_space ? 0 : 1)...m.size).each do |i|
m[i] = m[i].capitalize
end
m.join
end

def custom_charset(docxml)
Expand Down
35 changes: 35 additions & 0 deletions spec/isodoc/presentation_xml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,41 @@
.to be_equivalent_to (presxml)
end

it "realises text-transform" do
input = <<~INPUT
<standard-document xmlns="https://www.metanorma.org/ns/standoc" type="semantic">
<preface>
<foreword id="A">
<p id="_214f7090-c6d4-8fdc-5e6a-837ebb515871">
AB<span style="x:y">C</span>D<span style="x:y; text-transform:uppercase">aBc</span>
<span style="text-transform:uppercase">a<em>b</em>c</span>
<span style="text-transform:lowercase">A<em>B</em>C</span>
<span style="text-transform:capitalize">a<em>b</em>c abc</span>
<span style="text-transform:capitalize">a<em>b</em>c abc</span>
</p>
</foreword></preface></standard-document>
INPUT
presxml = <<~OUTPUT
<standard-document xmlns="https://www.metanorma.org/ns/standoc" type="presentation">
<preface><clause type="toc" id="_" displayorder="1"><title depth="1">Table of contents</title></clause>

<foreword id="A" displayorder="2">
<p id="_">
AB<span style="x:y">C</span>D<span style="x:y;text-transform:none">ABC</span>
<span style="text-transform:none">A<em>B</em>C</span>
<span style="text-transform:none">a<em>b</em>c</span>
<span style="text-transform:none">A<em>b</em>c Abc</span>
<span style="text-transform:none">A<em>b</em>c Abc</span>
</p>
</foreword></preface></standard-document>
OUTPUT
expect(strip_guid(IsoDoc::PresentationXMLConvert
.new(presxml_options)
.convert("test", input, true))
.sub(%r{<localized-strings>.*</localized-strings>}m, ""))
.to be_equivalent_to (presxml)
end

it "supplies formats in passthrough" do
input = <<~INPUT
<standard-document xmlns="https://www.metanorma.org/ns/standoc" type="semantic">
Expand Down
Loading