Class: String
Constant Summary collapse
- LATIN_MAP =
Map of latin chars and their representations as unicode chars.
{ "A" => %w[À Á  à ŠĄ Ā], "a" => %w[à á â ã å ą ả ã ạ ă ắ ằ ẳ ẵ ặ â ấ ầ ẩ ẫ ậ ā], "AE" => %w[Ä Æ Ǽ], "ae" => %w[ä æ ǽ], "C" => %w[Ç Č Ć Ĉ], "c" => %w[ç č ć ĉ], "D" => %w[Ð], "d" => %w[đ], "E" => %w[È É Ê Ẽ Ę Ė Ē Ë], "e" => %w[è é ę ë ė ẻ ẽ ẹ ê ế ề ể ễ ệ ē], "G" => %w[Ģ], "g" => %w[ģ], "I" => %w[Ì Í Î Ï Ĩ Į Ī], "i" => %w[ì í î ï ĩ į ỉ ị ī], "K" => %w[Ķ], "k" => %w[ķ], "L" => %w[Ļ], "l" => %w[ļ], "N" => %w[Ñ Ń Ņ], "n" => %w[ñ ń ņ], "O" => %w[Ò Ó Ô Õ Ø], "o" => %w[ò ó õ ỏ õ ọ ô ố ồ ổ ỗ ộ ơ ớ ờ ở ỡ ợ ø], "OE" => %w[Ö Œ], "oe" => %w[ö œ], "R" => %w[Ŗ], "r" => %w[ŗ], "S" => %w[Š], "s" => %w[š], "ss" => %w[ß], "U" => %w[Ù Ú Ũ Ű Ů Ũ Ų Ū Û], "u" => %w[ų ū û ú ù ű ů ủ ũ ụ ư ứ ừ ử ữ ự], "UE" => %w[Ü], "ue" => %w[ü], "x" => %w[×], "Y" => %w[Ý Ÿ Ŷ], "y" => %w[ý ÿ ŷ ỳ ỷ ỹ ỵ], "Z" => %w[Ž], "z" => %w[ž] }.freeze
Instance Method Summary collapse
-
#%(args) ⇒ Object
:nodoc:.
-
#_sprintf ⇒ Object
Extends Kernel::sprintf to accept “named arguments” given as hash.
-
#latinize ⇒ Object
Replaces non-latin chars, leaves some special ones.
-
#permalink ⇒ Object
Returns a string that may be used as permalink.
-
#snip(length, ellipsis = "…") ⇒ Object
Truncates string to given length while preserves whole words.
-
#sortable ⇒ Object
Converts string into a naturally sortable string.
-
#strip_tags ⇒ Object
Removes all html tags from given string.
-
#strip_tags! ⇒ Object
Removes all html tags on self.
-
#with_params(params = {}) ⇒ Object
Appends hash of query params to current string.
Instance Method Details
#%(args) ⇒ Object
:nodoc:
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/vidibus/core_extensions/string.rb', line 77 def %(args) if args.kind_of?(Hash) ret = dup args.each do |key, value| ret.gsub!(/\%\{#{key}\}/, value.to_s) end ret else ret = gsub(/%\{/, '%%{') begin ret._sprintf(args) rescue ArgumentError $stderr.puts " The string:#{ret}" $stderr.puts " args:#{args.inspect}" end end end |
#_sprintf ⇒ Object
Extends Kernel::sprintf to accept “named arguments” given as hash. This method was taken from Ruby-GetText. Thank you!
Examples:
# Normal sprintf behaviour:
"%s, %s" % ["Masao", "Mutoh"]
# Extended version with named arguments:
"%{firstname}, %{familyname}" % {:firstname => "Masao", :familyname => "Mutoh"}
76 |
# File 'lib/vidibus/core_extensions/string.rb', line 76 alias :_sprintf :% |
#latinize ⇒ Object
Replaces non-latin chars, leaves some special ones.
47 48 49 50 51 52 53 54 55 |
# File 'lib/vidibus/core_extensions/string.rb', line 47 def latinize c = dup for char, map in LATIN_MAP c.gsub!(/[#{map.join}]/mu, char) end c.gsub!(/[^a-zA-Z0-9\.\,\|\?\!\:;"'=\+\-_]+/mu, " ") c.gsub!(/\s+/, " ") c end |
#permalink ⇒ Object
Returns a string that may be used as permalink
58 59 60 61 62 63 |
# File 'lib/vidibus/core_extensions/string.rb', line 58 def permalink latinize. downcase. gsub(/[^a-z0-9]+/, "-"). gsub(/^-/, "").gsub(/-$/, "") end |
#snip(length, ellipsis = "…") ⇒ Object
Truncates string to given length while preserves whole words. If string exceeds given length, an ellipsis will be appended.
Example:
“O Brother, Where Art Thou?”.snip(13) # => “O Brother, Where…”
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/vidibus/core_extensions/string.rb', line 102 def snip(length, ellipsis = "…") return self if self.empty? str = dup str.strip! str.gsub(/^(.{#{length.to_i-1}})(.)([\w]*)(.*)/m) do if $2 == " " "#{$1}#{ellipsis}" elsif $3.empty? and $4.empty? str else "#{$1}#{$2}#{$3}#{ellipsis}" end end end |
#sortable ⇒ Object
Converts string into a naturally sortable string.
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/vidibus/core_extensions/string.rb', line 140 def sortable matches = self.scan(/(\D+)?(\d+(?:[\.]?\d+)?)(\D+)?/) return self.downcase.gsub(/\s/, '') if matches.none? ''.tap do |converted| matches.each do |s1, i, s2| converted << s1.downcase.gsub(/\s/, '') if s1 converted << '%030f' % i.to_f converted << s2.downcase.gsub(/\s/, '') if s2 end end end |
#strip_tags ⇒ Object
Removes all html tags from given string.
118 119 120 |
# File 'lib/vidibus/core_extensions/string.rb', line 118 def self.gsub(/<+\/?[^>]*>+/, '') end |
#strip_tags! ⇒ Object
Removes all html tags on self.
123 124 125 |
# File 'lib/vidibus/core_extensions/string.rb', line 123 def self.replace end |
#with_params(params = {}) ⇒ Object
Appends hash of query params to current string.
Example:
“vidibus.org”.with_params(:awesome => “yes”) # => “vidibus.org?awesome=yes”
134 135 136 137 |
# File 'lib/vidibus/core_extensions/string.rb', line 134 def with_params(params = {}) return self unless params and params.any? self + (self.match(/\?/) ? "&" : "?") + params.to_query end |