home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1429 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  2.7 KB  |  65 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. __all__ = [
  6.     'titlecase']
  7. __version__ = '0.5'
  8. SMALL = 'a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v\\.?|via|vs\\.?'
  9. PUNCT = '!"#$%&\'\xe2\x80\x98()*+,\\-./:;?@[\\\\\\]_`{|}~'
  10. SMALL_WORDS = re.compile('^(%s)$' % SMALL, re.I)
  11. INLINE_PERIOD = re.compile('[a-z][.][a-z]', re.I)
  12. UC_ELSEWHERE = re.compile('[%s]*?[a-zA-Z]+[A-Z]+?' % PUNCT)
  13. CAPFIRST = re.compile('^[%s]*?([A-Za-z])' % PUNCT)
  14. SMALL_FIRST = re.compile('^([%s]*)(%s)\\b' % (PUNCT, SMALL), re.I)
  15. SMALL_LAST = re.compile('\\b(%s)[%s]?$' % (SMALL, PUNCT), re.I)
  16. SUBPHRASE = re.compile('([:.;?!][ ])(%s)' % SMALL)
  17. APOS_SECOND = re.compile("^[dol]{1}['\xe2\x80\x98]{1}[a-z]+$", re.I)
  18. ALL_CAPS = re.compile('^[A-Z\\s%s]+$' % PUNCT)
  19. UC_INITIALS = re.compile('^(?:[A-Z]{1}\\.{1}|[A-Z]{1}\\.{1}[A-Z]{1})+$')
  20. MAC_MC = re.compile('^([Mm]a?c)(\\w+)')
  21.  
  22. def titlecase(text):
  23.     all_caps = ALL_CAPS.match(text)
  24.     words = re.split('\\s', text)
  25.     line = []
  26.     for word in words:
  27.         if all_caps:
  28.             if UC_INITIALS.match(word):
  29.                 line.append(word)
  30.                 continue
  31.             else:
  32.                 word = word.lower()
  33.         
  34.         if APOS_SECOND.match(word):
  35.             word = word.replace(word[0], word[0].upper())
  36.             word = word.replace(word[2], word[2].upper())
  37.             line.append(word)
  38.             continue
  39.         
  40.         if INLINE_PERIOD.search(word) or UC_ELSEWHERE.match(word):
  41.             line.append(word)
  42.             continue
  43.         
  44.         if SMALL_WORDS.match(word):
  45.             line.append(word.lower())
  46.             continue
  47.         
  48.         match = MAC_MC.match(word)
  49.         if match:
  50.             line.append('%s%s' % (match.group(1).capitalize(), match.group(2).capitalize()))
  51.             continue
  52.         
  53.         hyphenated = []
  54.         for item in word.split('-'):
  55.             hyphenated.append(CAPFIRST.sub((lambda m: m.group(0).upper()), item))
  56.         
  57.         line.append('-'.join(hyphenated))
  58.     
  59.     result = ' '.join(line)
  60.     result = SMALL_FIRST.sub((lambda m: '%s%s' % (m.group(1), m.group(2).capitalize())), result)
  61.     result = SMALL_LAST.sub((lambda m: m.group(0).capitalize()), result)
  62.     result = SUBPHRASE.sub((lambda m: '%s%s' % (m.group(1), m.group(2).capitalize())), result)
  63.     return result
  64.  
  65.