~ubuntu-branches/ubuntu/karmic/quodlibet/karmic

« back to all changes in this revision

Viewing changes to quodlibet/util/titlecase.py

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2009-01-30 23:55:34 UTC
  • mfrom: (1.1.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20090130235534-l4e72ulw0vqfo17w
Tags: 2.0-1ubuntu1
* Merge from Debian experimental (LP: #276856), remaining Ubuntu changes:
  + debian/patches/40-use-music-profile.patch:
    - Use the "Music and Movies" pipeline per default.
* Refresh the above patch for new upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2007 Javier Kohen
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License version 2 as
 
5
# published by the Free Software Foundation
 
6
#
 
7
# $Id: titlecase.py 4330 2008-09-14 03:19:26Z piman $
 
8
 
 
9
import unicodedata
 
10
 
 
11
def iswbound(char):
 
12
    """Returns whether the given character is a word boundary."""
 
13
    category = unicodedata.category(char)
 
14
    # If it's a space separator or punctuation
 
15
    return 'Zs' == category or 'Sk' == category or 'P' == category[0]
 
16
 
 
17
def utitle(string):
 
18
    """Title-case a string using a less destructive method than str.title."""
 
19
    new_string = string[0].capitalize()
 
20
    cap = False
 
21
    for i in xrange(1, len(string)):
 
22
        s = string[i]
 
23
        # Special case apostrophe in the middle of a word.
 
24
        if u"'" == s and string[i-1].isalpha(): cap = False
 
25
        elif iswbound(s): cap = True
 
26
        elif cap and s.isalpha():
 
27
            cap = False
 
28
            s = s.capitalize()
 
29
        else: cap = False
 
30
        new_string += s
 
31
    return new_string
 
32
 
 
33
def title(string, locale="utf-8"):
 
34
    """Title-case a string using a less destructive method than str.title."""
 
35
    if not string: return u""
 
36
    # if the string is all uppercase, lowercase it - Erich/Javier
 
37
    #   Lots of Japanese songs use entirely upper-case English titles,
 
38
    #   so I don't like this change... - JoeW
 
39
    #if string == string.upper(): string = string.lower()
 
40
    if not isinstance(string, unicode):
 
41
        string = string.decode(locale)
 
42
    return utitle(string)