PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/python/lib/django-0.96/django/utils/text.py

https://gitlab.com/gregtyka/frankenserver
Python | 204 lines | 196 code | 3 blank | 5 comment | 10 complexity | e2f3d5fc01742ae5df69770756f54381 MD5 | raw file
  1. import re
  2. from django.conf import settings
  3. # Capitalizes the first letter of a string.
  4. capfirst = lambda x: x and x[0].upper() + x[1:]
  5. def wrap(text, width):
  6. """
  7. A word-wrap function that preserves existing line breaks and most spaces in
  8. the text. Expects that existing line breaks are posix newlines.
  9. """
  10. def _generator():
  11. it = iter(text.split(' '))
  12. word = it.next()
  13. yield word
  14. pos = len(word) - word.rfind('\n') - 1
  15. for word in it:
  16. if "\n" in word:
  17. lines = word.split('\n')
  18. else:
  19. lines = (word,)
  20. pos += len(lines[0]) + 1
  21. if pos > width:
  22. yield '\n'
  23. pos = len(lines[-1])
  24. else:
  25. yield ' '
  26. if len(lines) > 1:
  27. pos = len(lines[-1])
  28. yield word
  29. return "".join(_generator())
  30. def truncate_words(s, num):
  31. "Truncates a string after a certain number of words."
  32. length = int(num)
  33. words = s.split()
  34. if len(words) > length:
  35. words = words[:length]
  36. if not words[-1].endswith('...'):
  37. words.append('...')
  38. return ' '.join(words)
  39. def truncate_html_words(s, num):
  40. """
  41. Truncates html to a certain number of words (not counting tags and comments).
  42. Closes opened tags if they were correctly closed in the given html.
  43. """
  44. length = int(num)
  45. if length <= 0:
  46. return ''
  47. html4_singlets = ('br', 'col', 'link', 'base', 'img', 'param', 'area', 'hr', 'input')
  48. # Set up regular expressions
  49. re_words = re.compile(r'&.*?;|<.*?>|([A-Za-z0-9][\w-]*)')
  50. re_tag = re.compile(r'<(/)?([^ ]+?)(?: (/)| .*?)?>')
  51. # Count non-HTML words and keep note of open tags
  52. pos = 0
  53. ellipsis_pos = 0
  54. words = 0
  55. open_tags = []
  56. while words <= length:
  57. m = re_words.search(s, pos)
  58. if not m:
  59. # Checked through whole string
  60. break
  61. pos = m.end(0)
  62. if m.group(1):
  63. # It's an actual non-HTML word
  64. words += 1
  65. if words == length:
  66. ellipsis_pos = pos
  67. continue
  68. # Check for tag
  69. tag = re_tag.match(m.group(0))
  70. if not tag or ellipsis_pos:
  71. # Don't worry about non tags or tags after our truncate point
  72. continue
  73. closing_tag, tagname, self_closing = tag.groups()
  74. tagname = tagname.lower() # Element names are always case-insensitive
  75. if self_closing or tagname in html4_singlets:
  76. pass
  77. elif closing_tag:
  78. # Check for match in open tags list
  79. try:
  80. i = open_tags.index(tagname)
  81. except ValueError:
  82. pass
  83. else:
  84. # SGML: An end tag closes, back to the matching start tag, all unclosed intervening start tags with omitted end tags
  85. open_tags = open_tags[i+1:]
  86. else:
  87. # Add it to the start of the open tags list
  88. open_tags.insert(0, tagname)
  89. if words <= length:
  90. # Don't try to close tags if we don't need to truncate
  91. return s
  92. out = s[:ellipsis_pos] + ' ...'
  93. # Close any tags still open
  94. for tag in open_tags:
  95. out += '</%s>' % tag
  96. # Return string
  97. return out
  98. def get_valid_filename(s):
  99. """
  100. Returns the given string converted to a string that can be used for a clean
  101. filename. Specifically, leading and trailing spaces are removed; other
  102. spaces are converted to underscores; and all non-filename-safe characters
  103. are removed.
  104. >>> get_valid_filename("john's portrait in 2004.jpg")
  105. 'johns_portrait_in_2004.jpg'
  106. """
  107. s = s.strip().replace(' ', '_')
  108. return re.sub(r'[^-A-Za-z0-9_.]', '', s)
  109. def get_text_list(list_, last_word='or'):
  110. """
  111. >>> get_text_list(['a', 'b', 'c', 'd'])
  112. 'a, b, c or d'
  113. >>> get_text_list(['a', 'b', 'c'], 'and')
  114. 'a, b and c'
  115. >>> get_text_list(['a', 'b'], 'and')
  116. 'a and b'
  117. >>> get_text_list(['a'])
  118. 'a'
  119. >>> get_text_list([])
  120. ''
  121. """
  122. if len(list_) == 0: return ''
  123. if len(list_) == 1: return list_[0]
  124. return '%s %s %s' % (', '.join([str(i) for i in list_][:-1]), last_word, list_[-1])
  125. def normalize_newlines(text):
  126. return re.sub(r'\r\n|\r|\n', '\n', text)
  127. def recapitalize(text):
  128. "Recapitalizes text, placing caps after end-of-sentence punctuation."
  129. # capwords = ()
  130. text = text.lower()
  131. capsRE = re.compile(r'(?:^|(?<=[\.\?\!] ))([a-z])')
  132. text = capsRE.sub(lambda x: x.group(1).upper(), text)
  133. # for capword in capwords:
  134. # capwordRE = re.compile(r'\b%s\b' % capword, re.I)
  135. # text = capwordRE.sub(capword, text)
  136. return text
  137. def phone2numeric(phone):
  138. "Converts a phone number with letters into its numeric equivalent."
  139. letters = re.compile(r'[A-PR-Y]', re.I)
  140. char2number = lambda m: {'a': '2', 'c': '2', 'b': '2', 'e': '3',
  141. 'd': '3', 'g': '4', 'f': '3', 'i': '4', 'h': '4', 'k': '5',
  142. 'j': '5', 'm': '6', 'l': '5', 'o': '6', 'n': '6', 'p': '7',
  143. 's': '7', 'r': '7', 'u': '8', 't': '8', 'w': '9', 'v': '8',
  144. 'y': '9', 'x': '9'}.get(m.group(0).lower())
  145. return letters.sub(char2number, phone)
  146. # From http://www.xhaus.com/alan/python/httpcomp.html#gzip
  147. # Used with permission.
  148. def compress_string(s):
  149. import cStringIO, gzip
  150. zbuf = cStringIO.StringIO()
  151. zfile = gzip.GzipFile(mode='wb', compresslevel=6, fileobj=zbuf)
  152. zfile.write(s)
  153. zfile.close()
  154. return zbuf.getvalue()
  155. ustring_re = re.compile(u"([\u0080-\uffff])")
  156. def javascript_quote(s, quote_double_quotes=False):
  157. def fix(match):
  158. return r"\u%04x" % ord(match.group(1))
  159. if type(s) == str:
  160. s = s.decode(settings.DEFAULT_CHARSET)
  161. elif type(s) != unicode:
  162. raise TypeError, s
  163. s = s.replace('\\', '\\\\')
  164. s = s.replace('\r', '\\r')
  165. s = s.replace('\n', '\\n')
  166. s = s.replace('\t', '\\t')
  167. s = s.replace("'", "\\'")
  168. if quote_double_quotes:
  169. s = s.replace('"', '&quot;')
  170. return str(ustring_re.sub(fix, s))
  171. smart_split_re = re.compile('("(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'|[^\\s]+)')
  172. def smart_split(text):
  173. """
  174. Generator that splits a string by spaces, leaving quoted phrases together.
  175. Supports both single and double quotes, and supports escaping quotes with
  176. backslashes. In the output, strings will keep their initial and trailing
  177. quote marks.
  178. >>> list(smart_split('This is "a person\'s" test.'))
  179. ['This', 'is', '"a person\'s"', 'test.']
  180. """
  181. for bit in smart_split_re.finditer(text):
  182. bit = bit.group(0)
  183. if bit[0] == '"':
  184. yield '"' + bit[1:-1].replace('\\"', '"').replace('\\\\', '\\') + '"'
  185. elif bit[0] == "'":
  186. yield "'" + bit[1:-1].replace("\\'", "'").replace("\\\\", "\\") + "'"
  187. else:
  188. yield bit