Module: Asciidoctor::PDF::TextTransformer

Included in:
FormattedText::Transform, IndexCatalog, Asciidoctor::Prawn::Extensions
Defined in:
lib/asciidoctor/pdf/text_transformer.rb

Constant Summary collapse

XMLMarkupRx =
/&#?[a-z\d]+;|</
PCDATAFilterRx =
/(&#?[a-z\d]+;|<[^>]+>)|([^&<]+)/
TagFilterRx =
/(<[^>]+>)|([^<]+)/
ContiguousCharsRx =
/\p{Graph}+/
WordRx =
/\p{Word}+/
BareClassRx =
/ class="bare[" ]/
Hyphen =
'-'
SoftHyphen =
?\u00ad
LowerAlphaChars =
'a-z'
SmallCapsChars =

NOTE: use more widely-supported ғ instead of ꜰ as replacement for F NOTE: use more widely-supported ǫ instead of ꞯ as replacement for Q NOTE: use more widely-supported s (lowercase latin “s”) instead of ꜱ as replacement for S NOTE: in small caps, x (lowercase latin “x”) remains unchanged

'ᴀʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴoᴘǫʀsᴛᴜᴠᴡxʏᴢ'

Instance Method Summary collapse

Instance Method Details

#capitalize_words(string) ⇒ Object



29
30
31
# File 'lib/asciidoctor/pdf/text_transformer.rb', line 29

def capitalize_words string
  string.gsub(ContiguousCharsRx) { $&.capitalize }
end

#capitalize_words_pcdata(string) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/asciidoctor/pdf/text_transformer.rb', line 21

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

#hyphenate_words(string, hyphenator) ⇒ Object



49
50
51
# File 'lib/asciidoctor/pdf/text_transformer.rb', line 49

def hyphenate_words string, hyphenator
  string.gsub(WordRx) { hyphenator.visualize $&, SoftHyphen }
end

#hyphenate_words_pcdata(string, hyphenator) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/asciidoctor/pdf/text_transformer.rb', line 33

def hyphenate_words_pcdata string, hyphenator
  if XMLMarkupRx.match? string
    skipping = false
    string.gsub PCDATAFilterRx do
      if $2
        skipping ? $2 : (hyphenate_words $2, hyphenator)
      else
        skipping = skipping ? $1 != '</a>' : ($1.start_with? '<a ') && (BareClassRx.match? $1)
        $1
      end
    end
  else
    hyphenate_words string, hyphenator
  end
end

#lowercase_pcdata(string) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/asciidoctor/pdf/text_transformer.rb', line 53

def lowercase_pcdata string
  if string.include? '<'
    string.gsub(TagFilterRx) { $2 ? $2.downcase : $1 }
  else
    string.downcase
  end
end

#smallcaps(string) ⇒ Object



77
78
79
# File 'lib/asciidoctor/pdf/text_transformer.rb', line 77

def smallcaps string
  string.tr LowerAlphaChars, SmallCapsChars
end

#smallcaps_pcdata(string) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/asciidoctor/pdf/text_transformer.rb', line 69

def smallcaps_pcdata string
  if XMLMarkupRx.match? string
    string.gsub(PCDATAFilterRx) { $2 ? (smallcaps $2) : $1 }
  else
    smallcaps string
  end
end

#transform_text(text, transform) ⇒ Object

Apply the text transform to the specified text.

Supported transform values are “uppercase”, “lowercase”, or “none” (passed as either a String or a Symbol). When the uppercase transform is applied to the text, it correctly uppercases visible text while leaving markup and named character entities unchanged. The none transform returns the text unmodified.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/asciidoctor/pdf/text_transformer.rb', line 89

def transform_text text, transform
  case transform
  when :uppercase, 'uppercase'
    uppercase_pcdata text
  when :lowercase, 'lowercase'
    lowercase_pcdata text
  when :capitalize, 'capitalize'
    capitalize_words_pcdata text
  when :smallcaps, 'smallcaps'
    smallcaps_pcdata text
  else
    text
  end
end

#uppercase_pcdata(string) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/asciidoctor/pdf/text_transformer.rb', line 61

def uppercase_pcdata string
  if XMLMarkupRx.match? string
    string.gsub(PCDATAFilterRx) { $2 ? $2.upcase : $1 }
  else
    string.upcase
  end
end