home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import re
- __all__ = [
- 'titlecase']
- __version__ = '0.5'
- SMALL = 'a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v\\.?|via|vs\\.?'
- PUNCT = '!"#$%&\'\xe2\x80\x98()*+,\\-./:;?@[\\\\\\]_`{|}~'
- SMALL_WORDS = re.compile('^(%s)$' % SMALL, re.I)
- INLINE_PERIOD = re.compile('[a-z][.][a-z]', re.I)
- UC_ELSEWHERE = re.compile('[%s]*?[a-zA-Z]+[A-Z]+?' % PUNCT)
- CAPFIRST = re.compile('^[%s]*?([A-Za-z])' % PUNCT)
- SMALL_FIRST = re.compile('^([%s]*)(%s)\\b' % (PUNCT, SMALL), re.I)
- SMALL_LAST = re.compile('\\b(%s)[%s]?$' % (SMALL, PUNCT), re.I)
- SUBPHRASE = re.compile('([:.;?!][ ])(%s)' % SMALL)
- APOS_SECOND = re.compile("^[dol]{1}['\xe2\x80\x98]{1}[a-z]+$", re.I)
- ALL_CAPS = re.compile('^[A-Z\\s%s]+$' % PUNCT)
- UC_INITIALS = re.compile('^(?:[A-Z]{1}\\.{1}|[A-Z]{1}\\.{1}[A-Z]{1})+$')
- MAC_MC = re.compile('^([Mm]a?c)(\\w+)')
-
- def titlecase(text):
- all_caps = ALL_CAPS.match(text)
- words = re.split('\\s', text)
- line = []
- for word in words:
- if all_caps:
- if UC_INITIALS.match(word):
- line.append(word)
- continue
- else:
- word = word.lower()
-
- if APOS_SECOND.match(word):
- word = word.replace(word[0], word[0].upper())
- word = word.replace(word[2], word[2].upper())
- line.append(word)
- continue
-
- if INLINE_PERIOD.search(word) or UC_ELSEWHERE.match(word):
- line.append(word)
- continue
-
- if SMALL_WORDS.match(word):
- line.append(word.lower())
- continue
-
- match = MAC_MC.match(word)
- if match:
- line.append('%s%s' % (match.group(1).capitalize(), match.group(2).capitalize()))
- continue
-
- hyphenated = []
- for item in word.split('-'):
- hyphenated.append(CAPFIRST.sub((lambda m: m.group(0).upper()), item))
-
- line.append('-'.join(hyphenated))
-
- result = ' '.join(line)
- result = SMALL_FIRST.sub((lambda m: '%s%s' % (m.group(1), m.group(2).capitalize())), result)
- result = SMALL_LAST.sub((lambda m: m.group(0).capitalize()), result)
- result = SUBPHRASE.sub((lambda m: '%s%s' % (m.group(1), m.group(2).capitalize())), result)
- return result
-
-