~ubuntu-branches/debian/sid/calibre/sid

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/oeb/polish/utils.py

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2014-02-02 10:46:11 UTC
  • mfrom: (1.5.7)
  • Revision ID: package-import@ubuntu.com-20140202104611-c2tlt43ldklc28k4
Tags: 1.22.0+dfsg-1
* New upstream release.
* debian/copyright: Update to 1.0 copyright standard. Thanks Felix Gruber!
  (LP: #737343)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# vim:fileencoding=utf-8
 
3
from __future__ import (unicode_literals, division, absolute_import,
 
4
                        print_function)
 
5
 
 
6
__license__ = 'GPL v3'
 
7
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
 
8
 
 
9
import re
 
10
from bisect import bisect
 
11
 
 
12
from calibre import guess_type as _guess_type
 
13
 
 
14
def guess_type(x):
 
15
    return _guess_type(x)[0] or 'application/octet-stream'
 
16
 
 
17
def setup_cssutils_serialization(tab_width=2):
 
18
    import cssutils
 
19
    prefs = cssutils.ser.prefs
 
20
    prefs.indent = tab_width * ' '
 
21
    prefs.indentClosingBrace = False
 
22
    prefs.omitLastSemicolon = False
 
23
 
 
24
class PositionFinder(object):
 
25
 
 
26
    def __init__(self, raw):
 
27
        pat = br'\n' if isinstance(raw, bytes) else r'\n'
 
28
        self.new_lines = tuple(m.start() + 1 for m in re.finditer(pat, raw))
 
29
 
 
30
    def __call__(self, pos):
 
31
        lnum = bisect(self.new_lines, pos)
 
32
        try:
 
33
            offset = abs(pos - self.new_lines[lnum - 1])
 
34
        except IndexError:
 
35
            offset = pos
 
36
        return (lnum + 1, offset)
 
37
 
 
38
class CommentFinder(object):
 
39
 
 
40
    def __init__(self, raw, pat=r'(?s)/\*.*?\*/'):
 
41
        self.starts, self.ends = [], []
 
42
        for m in re.finditer(pat, raw):
 
43
            start, end = m.span()
 
44
            self.starts.append(start), self.ends.append(end)
 
45
 
 
46
    def __call__(self, offset):
 
47
        if not self.starts:
 
48
            return False
 
49
        q = bisect(self.starts, offset) - 1
 
50
        return q >= 0 and self.starts[q] <= offset <= self.ends[q]
 
51
 
 
52
def link_stylesheets(container, names, sheets, remove=False, mtype='text/css'):
 
53
    from calibre.ebooks.oeb.base import XPath, XHTML
 
54
    changed_names = set()
 
55
    snames = set(sheets)
 
56
    lp = XPath('//h:link[@href]')
 
57
    hp = XPath('//h:head')
 
58
    for name in names:
 
59
        root = container.parsed(name)
 
60
        if remove:
 
61
            for link in lp(root):
 
62
                if (link.get('type', mtype) or mtype) == mtype:
 
63
                    container.remove_from_xml(link)
 
64
                    changed_names.add(name)
 
65
                    container.dirty(name)
 
66
        existing = {container.href_to_name(l.get('href'), name) for l in lp(root) if (l.get('type', mtype) or mtype) == mtype}
 
67
        extra = snames - existing
 
68
        if extra:
 
69
            changed_names.add(name)
 
70
            try:
 
71
                parent = hp(root)[0]
 
72
            except (TypeError, IndexError):
 
73
                parent = root.makeelement(XHTML('head'))
 
74
                container.insert_into_xml(root, parent, index=0)
 
75
            for sheet in sheets:
 
76
                if sheet in extra:
 
77
                    container.insert_into_xml(
 
78
                        parent, parent.makeelement(XHTML('link'), rel='stylesheet', type=mtype,
 
79
                                                   href=container.name_to_href(sheet, name)))
 
80
            container.dirty(name)
 
81
 
 
82
    return changed_names