PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/django_countries/bin/titlecase.py

https://bitbucket.org/smileychris/django-countries/
Python | 74 lines | 52 code | 8 blank | 14 comment | 0 complexity | bbb7b784a83bb45f7e0f6ea8915d8aa8 MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. titlecase.py v0.3
  5. Original Perl version by: John Gruber http://daringfireball.net/ 10 May 2008
  6. Python version by Stuart Colville http://muffinresearch.co.uk
  7. License: http://www.opensource.org/licenses/mit-license.php
  8. """
  9. import sys
  10. import re
  11. __all__ = ['titlecase']
  12. SMALL = 'a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v\.?|via|vs\.?'
  13. PUNCT = "[!\"#$%&'‘()*+,-./:;?@[\\\\\\]_`{|}~]"
  14. SMALL_WORDS = re.compile(r'^(%s)$' % SMALL, re.I)
  15. INLINE_PERIOD = re.compile(r'[a-zA-Z][.][a-zA-Z]')
  16. UC_ELSEWHERE = re.compile(r'%s*?[a-zA-Z]+[A-Z]+?' % PUNCT)
  17. CAPFIRST = re.compile(r"^%s*?([A-Za-z])" % PUNCT)
  18. SMALL_FIRST = re.compile(r'^(%s*)(%s)\b' % (PUNCT, SMALL), re.I)
  19. SMALL_LAST = re.compile(r'\b(%s)%s?$' % (SMALL, PUNCT), re.I)
  20. SUBPHRASE = re.compile(r'([:.;?!][ ])(%s)' % SMALL)
  21. def titlecase(text):
  22. """
  23. Titlecases input text
  24. This filter changes all words to Title Caps, and attempts to be clever
  25. about *un*capitalizing SMALL words like a/an/the in the input.
  26. The list of "SMALL words" which are not capped comes from
  27. the New York Times Manual of Style, plus 'vs' and 'v'.
  28. """
  29. words = re.split('\s', text)
  30. line = []
  31. for word in words:
  32. if INLINE_PERIOD.search(word) or UC_ELSEWHERE.match(word):
  33. line.append(word)
  34. continue
  35. if SMALL_WORDS.match(word):
  36. line.append(word.lower())
  37. continue
  38. line.append(CAPFIRST.sub(lambda m: m.group(0).upper(), word))
  39. line = " ".join(line)
  40. line = SMALL_FIRST.sub(lambda m: '%s%s' % (
  41. m.group(1),
  42. m.group(2).capitalize()
  43. ), line)
  44. line = SMALL_LAST.sub(lambda m: m.group(0).capitalize(), line)
  45. line = SUBPHRASE.sub(lambda m: '%s%s' % (
  46. m.group(1),
  47. m.group(2).capitalize()
  48. ), line)
  49. return line
  50. if __name__ == '__main__':
  51. if not sys.stdin.isatty():
  52. for line in sys.stdin:
  53. print titlecase(line)