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

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2014-05-14 18:17:50 UTC
  • mfrom: (1.5.10)
  • Revision ID: package-import@ubuntu.com-20140514181750-xyrxqa47dbw0qfhu
Tags: 1.36.0+dfsg-1
* New upstream release:
  - Fixes editing of metadata (Closes: #741638)

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__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
 
8
 
 
9
from lxml import etree
 
10
 
 
11
from calibre.ebooks.oeb.polish.container import OPF_NAMESPACES
 
12
from calibre.utils.localization import canonicalize_lang
 
13
 
 
14
def get_book_language(container):
 
15
    for lang in container.opf_xpath('//dc:language'):
 
16
        raw = lang.text
 
17
        if raw:
 
18
            code = canonicalize_lang(raw.split(',')[0].strip())
 
19
            if code:
 
20
                return code
 
21
 
 
22
def set_guide_item(container, item_type, title, name, frag=None):
 
23
    ref_tag = '{%s}reference' % OPF_NAMESPACES['opf']
 
24
    href = None
 
25
    if name:
 
26
        href = container.name_to_href(name, container.opf_name)
 
27
        if frag:
 
28
            href += '#' + frag
 
29
 
 
30
    guides = container.opf_xpath('//opf:guide')
 
31
    if not guides and href:
 
32
        g = container.opf.makeelement('{%s}guide' % OPF_NAMESPACES['opf'], nsmap={'opf':OPF_NAMESPACES['opf']})
 
33
        container.insert_into_xml(container.opf, g)
 
34
        guides = [g]
 
35
 
 
36
    for guide in guides:
 
37
        matches = []
 
38
        for child in guide.iterchildren(etree.Element):
 
39
            if child.tag == ref_tag and child.get('type', '').lower() == item_type.lower():
 
40
                matches.append(child)
 
41
        if not matches and href:
 
42
            r = guide.makeelement(ref_tag, type=item_type, nsmap={'opf':OPF_NAMESPACES['opf']})
 
43
            container.insert_into_xml(guide, r)
 
44
            matches.append(r)
 
45
        for m in matches:
 
46
            if href:
 
47
                m.set('title', title), m.set('href', href), m.set('type', item_type)
 
48
            else:
 
49
                container.remove_from_xml(m)
 
50
    container.dirty(container.opf_name)
 
51