Skip to content

Commit

Permalink
resolves asciidoctor#1382 add support for capitalize text transform
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux committed Nov 15, 2019
1 parent 84b7b69 commit 3034f72
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[
== Unreleased

* hyphenate prose using the text-hyphen gem if the `hyphens` attribute is set (#20)
* add support for capitalize text transform (#1382)
* fix AsciiDoc table cell from overflowing bounds of table and creating extra page (#1369)
* don't double escape XML special characters in literal table cell (#1370)
* use same line height throughout abstract
Expand Down
7 changes: 3 additions & 4 deletions docs/theming-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ The following transforms are recognized:

* uppercase
* lowercase
* capitalize (each word, like CSS)
* none (clears an inherited value)

[CAUTION#transform-unicode-letters]
Expand All @@ -480,8 +481,6 @@ or
$ gem install unicode
====

// Additional transforms, such as capitalize, may be added in the future.

=== Colors

The theme language supports color values in three formats:
Expand Down Expand Up @@ -1502,7 +1501,7 @@ The keys in this category control the style of most headings, including part tit
|<<text-transforms,Text transform>> +
(default: _inherit_)
|heading:
text-transform: uppercase
text-transform: capitalize

|line-height
|<<values,Number>> +
Expand Down Expand Up @@ -1591,7 +1590,7 @@ The keys in this category control the style of most headings, including part tit
|<<text-transforms,Text transform>> +
(default: $heading-text-transform)
|heading:
h3-text-transform: lowercase
h3-text-transform: uppercase

|margin-top
|<<measurement-units,Measurement>> +
Expand Down
2 changes: 2 additions & 0 deletions lib/asciidoctor/pdf/ext/prawn/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ def transform_text text, transform
uppercase_pcdata text
when :lowercase, 'lowercase'
lowercase_mb text
when :capitalize, 'capitalize'
capitalize_words_pcdata text
else
text
end
Expand Down
28 changes: 28 additions & 0 deletions lib/asciidoctor/pdf/text_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ def uppercase_pcdata string
end
end

def capitalize_words_pcdata string
if XMLMarkupRx.match? string
string.gsub(PCDATAFilterRx) { $2 ? (capitalize_words_mb $2) : $1 }
else
captialize_words_mb string
end
end

def capitalize_words_mb string
string.gsub(WordRx) { capitalize_mb $& }
end

if RUBY_VERSION >= '2.4'
def uppercase_mb string
string.upcase
Expand All @@ -33,6 +45,10 @@ def uppercase_mb string
def lowercase_mb string
string.downcase
end

def capitalize_mb string
string.capitalize
end
# NOTE Unicode library is 4x as fast as ActiveSupport::MultiByte::Chars
elsif defined? ::Unicode
def uppercase_mb string
Expand All @@ -42,6 +58,10 @@ def uppercase_mb string
def lowercase_mb string
string.ascii_only? ? string.downcase : (::Unicode.downcase string)
end

def lowercase_mb string
string.ascii_only? ? string.capitalize : (::Unicode.capitalize string)
end
elsif defined? ::ActiveSupport::Multibyte
MultibyteChars = ::ActiveSupport::Multibyte::Chars

Expand All @@ -52,6 +72,10 @@ def uppercase_mb string
def lowercase_mb string
string.ascii_only? ? string.downcase : (MultibyteChars.new string).downcase.to_s
end

def capitalize_mb string
string.ascii_only? ? string.capitalize : (MultibyteChars.new string).capitalize.to_s
end
else
def uppercase_mb string
string.upcase
Expand All @@ -60,6 +84,10 @@ def uppercase_mb string
def lowercase_mb string
string.downcase
end

def capitalize_mb string
string.capitalize
end
end

def hyphenate_pcdata string, hyphenator
Expand Down
18 changes: 18 additions & 0 deletions spec/text_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@
(expect lines[0]).to eql %(\uf085 Heading)
(expect lines[1]).to eql %(See \uf085 Heading.)
end

it 'should apply text transform to text' do
[
['uppercase', 'here we go *again*', 'HERE WE GO AGAIN'],
['lowercase', 'Here We Go *Again*', 'here we go again'],
['capitalize', 'Here we go *again*', 'Here We Go Again'],
].each do |(transform, before, after)|
pdf = to_pdf <<~EOS, pdf_theme: { heading_text_transform: transform }, analyze: true
== #{before}
EOS

lines = pdf.lines
(expect lines).to have_size 1
(expect lines[0]).to eql after
formatted_word = (pdf.find_text %r/again/i)[0]
(expect formatted_word[:font_name]).to eql 'NotoSerif-Bold'
end
end
end

context 'Roles' do
Expand Down

0 comments on commit 3034f72

Please sign in to comment.