Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/zena/core_ext/string.rb

Direct Known Subclasses

Syntax::Token

Constant Summary collapse

ALLOWED_CHARS_IN_URL =
" a-zA-Z0-9\\."
ALLOWED_CHARS_IN_FILENAME =

in filename, allow ‘-’ and ‘_’ because it does not represent a space and we do not have the mode confusion thing.

"#{ALLOWED_CHARS_IN_URL}_\\-\\+$"
ALLOWED_CHARS_IN_FILEPATH =

Everything apart from a-zA-Z0-9_.-/$ are not allowed in template paths

"#{ALLOWED_CHARS_IN_FILENAME}/"
TO_FILENAME_REGEXP =
%r{([^ #{ALLOWED_CHARS_IN_FILENAME}]+)}n
TO_URL_NAME_REGEXP =
%r{([^ #{ALLOWED_CHARS_IN_URL}])}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_filename(str) ⇒ Object

Retrieve original title from filename



50
51
52
# File 'lib/zena/core_ext/string.rb', line 50

def self.from_filename(str)
  CGI.unescape(str.gsub('+', '%2B'))
end

.from_url_name(str) ⇒ Object

Retrieve original title from an url_name



38
39
40
# File 'lib/zena/core_ext/string.rb', line 38

def self.from_url_name(str)
  CGI.unescape(str.tr('-', ' '))
end

Instance Method Details

#abs_path(root) ⇒ Object

return an absolute path from a relative path and a root



87
88
89
90
91
92
93
94
95
# File 'lib/zena/core_ext/string.rb', line 87

def abs_path(root)
  root = root.split('/')
  path = split('/')
  while path[0] == '..'
    root.pop
    path.shift
  end
  (root + path).join('/')
end

#limit(size, readmore = '…') ⇒ Object

Limit the number of characters and append the ‘readmore’ argument or “…” by default if the string is longer then limit. If you limit to ‘20’ characters, the final size will max 20 + the size of the readmore argument.



101
102
103
104
105
106
107
108
# File 'lib/zena/core_ext/string.rb', line 101

def limit(size, readmore = '')
  if self.size > size
    # readmore can contain a link: <a...> but this is defined in the zafu template.
    self[0..(size-1)] + readmore
  else
    self
  end
end

#rel_path(root) ⇒ Object

return a relative path from an absolute path and a root



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/zena/core_ext/string.rb', line 65

def rel_path(root)
  root = root.split('/')
  path = split('/')
  i = 0
  ref  = []
  while true
    if root == []
      ref = path
      break
    elsif root[0] == path[0]
      root.shift
      path.shift
    else
      # for each root element left: '..'
      ref = root.map{'..'} + path
      break
    end
  end
  ref.join('/')
end

#to_filenameObject

Change a title into a valid filename



43
44
45
46
47
# File 'lib/zena/core_ext/string.rb', line 43

def to_filename
  gsub(TO_FILENAME_REGEXP) do
    '%' + $1.unpack('H2' * $1.size).join('%').upcase
  end
end

#to_filename!Object



59
60
61
62
# File 'lib/zena/core_ext/string.rb', line 59

def to_filename!
  replace(to_filename)
  self
end

#url_nameObject

Change a title into a valid url name



26
27
28
29
30
# File 'lib/zena/core_ext/string.rb', line 26

def url_name
  gsub(TO_URL_NAME_REGEXP) do
    '%' + $1.unpack('H2' * $1.size).join('%').upcase
  end.tr(' ', '-')
end

#url_name!Object



54
55
56
57
# File 'lib/zena/core_ext/string.rb', line 54

def url_name!
  replace(url_name)
  self
end

#urlencodeObject

Change a string into something that can be inserted in an url.



33
34
35
# File 'lib/zena/core_ext/string.rb', line 33

def urlencode
  URI.escape(self)
end