~ubuntu-branches/ubuntu/karmic/calibre/karmic

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/chardet/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: james.westby@ubuntu.com-20090730124941-kviipg9ypwgppulc
Tags: upstream-0.6.3+dfsg
ImportĀ upstreamĀ versionĀ 0.6.3+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
# modify it under the terms of the GNU Lesser General Public
4
4
# License as published by the Free Software Foundation; either
5
5
# version 2.1 of the License, or (at your option) any later version.
6
 
 
6
#
7
7
# This library is distributed in the hope that it will be useful,
8
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10
10
# Lesser General Public License for more details.
11
 
 
11
#
12
12
# You should have received a copy of the GNU Lesser General Public
13
13
# License along with this library; if not, write to the Free Software
14
14
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
30
30
 
31
31
# Added by Kovid
32
32
ENCODING_PATS = [
33
 
                 re.compile(r'<\?[^<>]+encoding=[\'"](.*?)[\'"][^<>]*>', 
 
33
                 re.compile(r'<\?[^<>]+encoding=[\'"](.*?)[\'"][^<>]*>',
34
34
                            re.IGNORECASE),
35
 
                 re.compile(r'<meta.*?content=[\'"].*?charset=([^\s\'"]+).*?[\'"].*?>', 
 
35
                 re.compile(r'<meta.*?content=[\'"].*?charset=([^\s\'"]+).*?[\'"].*?>',
36
36
                            re.IGNORECASE)
37
37
                 ]
38
38
ENTITY_PATTERN = re.compile(r'&(\S+?);')
51
51
 
52
52
_CHARSET_ALIASES = { "macintosh" : "mac-roman",
53
53
                        "x-sjis" : "shift-jis" }
54
 
    
 
54
 
55
55
 
56
56
def force_encoding(raw, verbose):
57
57
    from calibre.constants import preferred_encoding
70
70
    if encoding == 'ascii':
71
71
        encoding = 'utf-8'
72
72
    return encoding
73
 
        
74
 
 
75
 
def xml_to_unicode(raw, verbose=False, strip_encoding_pats=False, 
 
73
 
 
74
 
 
75
def xml_to_unicode(raw, verbose=False, strip_encoding_pats=False,
76
76
                   resolve_entities=False):
77
77
    '''
78
 
    Force conversion of byte string to unicode. Tries to look for XML/HTML 
 
78
    Force conversion of byte string to unicode. Tries to look for XML/HTML
79
79
    encoding declaration first, if not found uses the chardet library and
80
80
    prints a warning if detection confidence is < 100%
81
 
    @return: (unicode, encoding used) 
 
81
    @return: (unicode, encoding used)
82
82
    '''
83
83
    encoding = None
84
84
    if not raw:
85
 
        return u'', encoding    
 
85
        return u'', encoding
86
86
    if not isinstance(raw, unicode):
87
87
        if raw.startswith('\xff\xfe'):
88
88
            raw, encoding = raw.decode('utf-16-le')[1:], 'utf-16-le'
103
103
        except LookupError:
104
104
            encoding = 'utf-8'
105
105
            raw = raw.decode(encoding, 'replace')
106
 
    
 
106
 
107
107
    if strip_encoding_pats:
108
108
        raw = strip_encoding_declarations(raw)
109
109
    if resolve_entities:
110
110
        raw = substitute_entites(raw)
111
 
        
112
 
    return raw, encoding 
 
111
 
 
112
    return raw, encoding