示例#1
0
    def __init__(self, oeb, opts):
        self.oeb, self.opts, self.log = oeb, opts, oeb.log
        self.title = opts.toc_title or DEFAULT_TITLE
        self.at_start = opts.mobi_toc_at_start
        self.generated_item = None
        self.added_toc_guide_entry = False
        self.has_toc = oeb.toc and oeb.toc.count() > 1

        if 'toc' in oeb.guide:
            # Remove spurious toc entry from guide if it is not in spine or it
            # does not have any hyperlinks
            href = urlnormalize(oeb.guide['toc'].href.partition('#')[0])
            if href in oeb.manifest.hrefs:
                item = oeb.manifest.hrefs[href]
                if (hasattr(item.data, 'xpath')
                        and XPath('//h:a[@href]')(item.data)):
                    if oeb.spine.index(item) < 0:
                        oeb.spine.add(item, linear=False)
                    return
                elif self.has_toc:
                    oeb.guide.remove('toc')
            else:
                oeb.guide.remove('toc')

        if (not self.has_toc or 'toc' in oeb.guide or opts.no_inline_toc
                or getattr(opts, 'mobi_passthrough', False)):
            return

        self.log.info('\tGenerating in-line ToC')

        embed_css = ''
        s = getattr(oeb, 'store_embed_font_rules', None)
        if getattr(s, 'body_font_family', None):
            css = [x.cssText for x in s.rules
                   ] + ['body { font-family: %s }' % s.body_font_family]
            embed_css = '\n\n'.join(css)

        root = etree.fromstring(
            TEMPLATE.format(xhtmlns=XHTML_NS,
                            title=self.title,
                            embed_css=embed_css,
                            extra_css=(opts.extra_css or '')))
        parent = XPath('//h:ul')(root)[0]
        parent.text = '\n\t'
        for child in self.oeb.toc:
            self.process_toc_node(child, parent)

        id, href = oeb.manifest.generate('contents', 'contents.xhtml')
        item = self.generated_item = oeb.manifest.add(id,
                                                      href,
                                                      XHTML_MIME,
                                                      data=root)
        if self.at_start:
            oeb.spine.insert(0, item, linear=True)
        else:
            oeb.spine.add(item, linear=False)

        oeb.guide.add('toc', 'Table of Contents', href)
示例#2
0
文件: toc.py 项目: BobPyron/calibre
    def __init__(self, oeb, opts):
        self.oeb, self.opts, self.log = oeb, opts, oeb.log
        self.title = opts.toc_title or DEFAULT_TITLE
        self.at_start = opts.mobi_toc_at_start
        self.generated_item = None
        self.added_toc_guide_entry = False
        self.has_toc = oeb.toc and oeb.toc.count() > 1

        if 'toc' in oeb.guide:
            # Remove spurious toc entry from guide if it is not in spine or it
            # does not have any hyperlinks
            href = urlnormalize(oeb.guide['toc'].href.partition('#')[0])
            if href in oeb.manifest.hrefs:
                item = oeb.manifest.hrefs[href]
                if (hasattr(item.data, 'xpath') and
                    XPath('//h:a[@href]')(item.data)):
                    if oeb.spine.index(item) < 0:
                        oeb.spine.add(item, linear=False)
                    return
                elif self.has_toc:
                    oeb.guide.remove('toc')
            else:
                oeb.guide.remove('toc')

        if (not self.has_toc or 'toc' in oeb.guide or opts.no_inline_toc or
            getattr(opts, 'mobi_passthrough', False)):
            return

        self.log('\tGenerating in-line ToC')

        embed_css = ''
        s = getattr(oeb, 'store_embed_font_rules', None)
        if getattr(s, 'body_font_family', None):
            css = [x.cssText for x in s.rules] + [
                    'body { font-family: %s }'%s.body_font_family]
            embed_css = '\n\n'.join(css)

        root = etree.fromstring(TEMPLATE.format(xhtmlns=XHTML_NS,
            title=self.title, embed_css=embed_css,
            extra_css=(opts.extra_css or '')))
        parent = XPath('//h:ul')(root)[0]
        parent.text = '\n\t'
        for child in self.oeb.toc:
            self.process_toc_node(child, parent)

        id, href = oeb.manifest.generate('contents', 'contents.xhtml')
        item = self.generated_item = oeb.manifest.add(id, href, XHTML_MIME,
                data=root)
        if self.at_start:
            oeb.spine.insert(0, item, linear=True)
        else:
            oeb.spine.add(item, linear=False)

        oeb.guide.add('toc', 'Table of Contents', href)
示例#3
0
    def __init__(self, oeb, opts):
        self.oeb, self.opts, self.log = oeb, opts, oeb.log
        self.title = opts.toc_title or DEFAULT_TITLE
        self.at_start = opts.mobi_toc_at_start
        self.generated_item = None
        self.added_toc_guide_entry = False
        self.has_toc = oeb.toc and oeb.toc.count() > 1

        if 'toc' in oeb.guide:
            # Remove spurious toc entry from guide if it is not in spine or it
            # does not have any hyperlinks
            href = urlnormalize(oeb.guide['toc'].href)
            if href in oeb.manifest.hrefs:
                item = oeb.manifest.hrefs[href]
                if (hasattr(item.data, 'xpath')
                        and XPath('//h:a[@href]')(item.data)):
                    if oeb.spine.index(item) < 0:
                        oeb.spine.add(item, linear=False)
                    return
                elif self.has_toc:
                    oeb.guide.remove('toc')
            else:
                oeb.guide.remove('toc')

        if not self.has_toc or 'toc' in oeb.guide or opts.no_inline_toc:
            return

        self.log('\tGenerating in-line ToC')

        root = etree.fromstring(
            TEMPLATE.format(xhtmlns=XHTML_NS, title=self.title))
        parent = XPath('//h:ul')(root)[0]
        parent.text = '\n\t'
        for child in self.oeb.toc:
            self.process_toc_node(child, parent)

        id, href = oeb.manifest.generate('contents', 'contents.xhtml')
        item = self.generated_item = oeb.manifest.add(id,
                                                      href,
                                                      XHTML_MIME,
                                                      data=root)
        if self.at_start:
            oeb.spine.insert(0, item, linear=True)
        else:
            oeb.spine.add(item, linear=False)

        oeb.guide.add('toc', 'Table of Contents', href)
示例#4
0
文件: toc.py 项目: Eksmo/calibre
    def __init__(self, oeb, opts):
        self.oeb, self.opts, self.log = oeb, opts, oeb.log
        self.title = opts.toc_title or DEFAULT_TITLE
        self.at_start = opts.mobi_toc_at_start
        self.generated_item = None
        self.added_toc_guide_entry = False
        self.has_toc = oeb.toc and oeb.toc.count() > 1

        if 'toc' in oeb.guide:
            # Remove spurious toc entry from guide if it is not in spine or it
            # does not have any hyperlinks
            href = urlnormalize(oeb.guide['toc'].href)
            if href in oeb.manifest.hrefs:
                item = oeb.manifest.hrefs[href]
                if (hasattr(item.data, 'xpath') and
                    XPath('//h:a[@href]')(item.data)):
                    if oeb.spine.index(item) < 0:
                        oeb.spine.add(item, linear=False)
                    return
                elif self.has_toc:
                    oeb.guide.remove('toc')
            else:
                oeb.guide.remove('toc')

        if not self.has_toc or 'toc' in oeb.guide or opts.no_inline_toc:
            return

        self.log('\tGenerating in-line ToC')

        root = etree.fromstring(TEMPLATE.format(xhtmlns=XHTML_NS,
            title=self.title))
        parent = XPath('//h:ul')(root)[0]
        parent.text = '\n\t'
        for child in self.oeb.toc:
            self.process_toc_node(child, parent)

        id, href = oeb.manifest.generate('contents', 'contents.xhtml')
        item = self.generated_item = oeb.manifest.add(id, href, XHTML_MIME,
                data=root)
        if self.at_start:
            oeb.spine.insert(0, item, linear=True)
        else:
            oeb.spine.add(item, linear=False)

        oeb.guide.add('toc', 'Table of Contents', href)
示例#5
0
文件: toc.py 项目: JapaChin/calibre
    def __init__(self, oeb, opts, replace_previous_inline_toc=False, ignore_existing_toc=False):
        self.oeb, self.opts, self.log = oeb, opts, oeb.log
        self.title = opts.toc_title or DEFAULT_TITLE
        self.at_start = opts.mobi_toc_at_start
        self.generated_item = None
        self.added_toc_guide_entry = False
        self.has_toc = oeb.toc and oeb.toc.count() > 1

        self.tocitem = tocitem = None
        if find_previous_calibre_inline_toc:
            tocitem = self.tocitem = find_previous_calibre_inline_toc(oeb)
        if ignore_existing_toc and "toc" in oeb.guide:
            oeb.guide.remove("toc")

        if "toc" in oeb.guide:
            # Remove spurious toc entry from guide if it is not in spine or it
            # does not have any hyperlinks
            href = urlnormalize(oeb.guide["toc"].href.partition("#")[0])
            if href in oeb.manifest.hrefs:
                item = oeb.manifest.hrefs[href]
                if hasattr(item.data, "xpath") and XPath("//h:a[@href]")(item.data):
                    if oeb.spine.index(item) < 0:
                        oeb.spine.add(item, linear=False)
                    return
                elif self.has_toc:
                    oeb.guide.remove("toc")
            else:
                oeb.guide.remove("toc")

        if not self.has_toc or "toc" in oeb.guide or opts.no_inline_toc or getattr(opts, "mobi_passthrough", False):
            return

        self.log("\tGenerating in-line ToC")

        embed_css = ""
        s = getattr(oeb, "store_embed_font_rules", None)
        if getattr(s, "body_font_family", None):
            css = [x.cssText for x in s.rules] + ["body { font-family: %s }" % s.body_font_family]
            embed_css = "\n\n".join(css)

        root = etree.fromstring(
            TEMPLATE.format(xhtmlns=XHTML_NS, title=self.title, embed_css=embed_css, extra_css=(opts.extra_css or ""))
        )
        parent = XPath("//h:ul")(root)[0]
        parent.text = "\n\t"
        for child in self.oeb.toc:
            self.process_toc_node(child, parent)

        if tocitem is not None:
            href = tocitem.href
            if oeb.spine.index(tocitem) > -1:
                oeb.spine.remove(tocitem)
            tocitem.data = root
        else:
            id, href = oeb.manifest.generate("contents", "contents.xhtml")
            tocitem = self.generated_item = oeb.manifest.add(id, href, XHTML_MIME, data=root)
        if self.at_start:
            oeb.spine.insert(0, tocitem, linear=True)
        else:
            oeb.spine.add(tocitem, linear=False)

        oeb.guide.add("toc", "Table of Contents", href)