Module: Titlify
- Defined in:
- lib/titlify/version.rb,
lib/titlify/titlecase.rb,
lib/titlify/data/word_lists.rb,
lib/titlify/string_extensions.rb,
lib/titlify/data/smart_format_rules.rb
Defined Under Namespace
Modules: StringExtensions Classes: Data
Constant Summary collapse
- VERSION =
"0.3.3"
Class Method Summary collapse
-
.titlify(string) ⇒ Object
(also: titlecase)
Format text appropriately for titles.
Class Method Details
.titlify(string) ⇒ Object Also known as: titlecase
Format text appropriately for titles. This method is much smarter than ActiveSupport’s titlecase. The algorithm is based on work done by John Gruber et al (daringfireball.net/2008/08/title_case_update)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/titlify/titlecase.rb', line 10 def titlify(string) lsquo = [8216].pack("U") rsquo = [8217].pack("U") ldquo = [8220].pack("U") rdquo = [8221].pack("U") ndash = [8211].pack("U") string.strip! string.gsub!(/\s+/, " ") string.downcase! unless string =~ /[[:lower:]]/ small_words = word_list.join("|") apos = / (?: ['#{rsquo}] [[:lower:]]* )? /xu string.gsub!( / \b ([_\*]*) (?: ([A-Z][\.]) # Acronyms with periods in them ){2,100} ([_\*]*) \b? /xi ) do |m| m.gsub(/\./, "").upcase end string.gsub!( / \b ([_\*]*) # [1] (?: ( # [2] entire matched URL (?:[A-Za-z0-9]+[\.]{1})+(?:ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|asia|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cu|cv|cw|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|info|int|io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sx|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|xn|xxx|ye|yt|za|zm|zw) | (?: (?: [a-z][\w-]+: (?: \/{1,3} | [a-z0-9%] ) | www\d{0,3}[.] | [a-z0-9.\-]+[.][a-z]{2,4}\/ ) (?: [^\s()<>]+ | \([^\s()<>]+|\([^\s()<>]+\)*\) )+ (?: \([^\s()<>]+|\([^\s()<>]+\)*\) | [^\s`!()\[\]{};:'".,<>?«»“”‘’] ) ) ) | ((?:[\w\.\-]+)@(?:[\w]+\.)+(?:[a-zA-Z]){2,4}) # [3] email | ( (?i: #{small_words} ) #{apos} ) # [4] or small word, case-insensitive | ( [[:alpha:]] [[:lower:]'#{rsquo}()\[\]{}]* #{apos} ) # [5] or word without internal caps | ( [[:alpha:]] [[:alpha:]'#{rsquo}()\[\]{}]* #{apos} ) # [6] or some other word ) ([_\*]*) # [7] \b /xu ) do ($1 ? $1 : "") + ($2 ? $2 : ($3 ? $3.downcase : ($4 ? $4.downcase : ($5 ? $5.downcase.capitalize : $6)))) + ($7 ? $7 : "") end if RUBY_VERSION < "1.9.0" string.gsub!( / \b ([:alpha:]+) (#{ndash}) ([:alpha:]+) \b /xu ) do $1.downcase.capitalize + $2 + $1.downcase.capitalize end end string.gsub!( / ( \A [[:punct:]]* # start of title | [:.;?!][ ]+ # or of subsentence | [ ]['"#{ldquo}#{lsquo}(\[][ ]* # or of inserted subphrase ) ( #{small_words} ) # followed by a small-word \b /xiu ) do $1 + $2.downcase.capitalize end string.gsub!( / \b ( #{small_words} ) # small-word (?= [[:punct:]]* \Z # at the end of the title | ['"#{rsquo}#{rdquo})\]] [ ] # or of an inserted subphrase ) /xu ) do $1.downcase.capitalize end string.gsub!( / ( \b [[:alpha:]] # single first letter [\-#{ndash}] # followed by a dash ) ( [[:alpha:]] ) # followed by a letter /xu ) do $1 + $2.downcase end string.gsub!(/q&a/i, 'Q&A') string end |