Skip to content

Commit

Permalink
implement text-transform style: metanorma/metanorma-iec#157
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Feb 9, 2024
1 parent 166933e commit e52a0ee
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
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

0 comments on commit e52a0ee

Please sign in to comment.