Beispiel #1
0
 def add_toc_level(self, elem, toc):
     for node in toc:
         block = element(elem, XHTML('div'),
                         attrib={'class': 'calibre_toc_block'})
         line = element(block, XHTML('a'),
                        attrib={'href': node.href,
                                'class': 'calibre_toc_line'})
         line.text = node.title
         self.add_toc_level(block, node)
 def add_toc_level(self, elem, toc):
     for node in toc:
         block = element(elem, XHTML('div'),
                         attrib={'class': 'calibre_toc_block'})
         line = element(block, XHTML('a'),
                        attrib={'href': node.href,
                                'class': 'calibre_toc_line'})
         line.text = node.title
         self.add_toc_level(block, node)
Beispiel #3
0
 def build_node(current_node, parent=None):
     if parent is None:
         parent = etree.Element('ul')
     elif len(current_node.nodes):
         parent = element(parent, ('ul'))
     for node in current_node.nodes:
         point = element(parent, 'li')
         href = relpath(abspath(unquote(node.href)), dirname(ref_url))
         link = element(point, 'a', href=href)
         title = node.title
         if title:
             title = re.sub(r'\s+', ' ', title)
         link.text=title
         build_node(node, point)
     return parent
Beispiel #4
0
 def build_node(current_node, parent=None):
     if parent is None:
         parent = etree.Element('ul')
     elif len(current_node.nodes):
         parent = element(parent, ('ul'))
     for node in current_node.nodes:
         point = element(parent, 'li')
         href = relpath(abspath(unquote(node.href)),
                        dirname(ref_url))
         link = element(point, 'a', href=href)
         title = node.title
         if title:
             title = re.sub(r'\s+', ' ', title)
         link.text = title
         build_node(node, point)
     return parent
Beispiel #5
0
 def build_node(current_node, parent=None):
     if parent is None:
         parent = etree.Element('ul')
     elif len(current_node.nodes):
         parent = element(parent, ('ul'))
     for node in current_node.nodes:
         point = element(parent, 'li')
         href = relpath(abspath(unquote(node.href)), dirname(ref_url))
         if isinstance(href, bytes):
             href = href.decode('utf-8')
         link = element(point, 'a', href=clean_xml_chars(href))
         title = node.title
         if isinstance(title, bytes):
             title = title.decode('utf-8')
         if title:
             title = re.sub(r'\s+', ' ', title)
         link.text = clean_xml_chars(title)
         build_node(node, point)
     return parent
 def build_node(current_node, parent=None):
     if parent is None:
         parent = etree.Element('ul')
     elif len(current_node.nodes):
         parent = element(parent, ('ul'))
     for node in current_node.nodes:
         point = element(parent, 'li')
         href = relpath(abspath(unquote(node.href)), dirname(ref_url))
         if isinstance(href, bytes):
             href = href.decode('utf-8')
         link = element(point, 'a', href=clean_xml_chars(href))
         title = node.title
         if isinstance(title, bytes):
             title = title.decode('utf-8')
         if title:
             title = re.sub(r'\s+', ' ', title)
         link.text = clean_xml_chars(title)
         build_node(node, point)
     return parent
Beispiel #7
0
    def __call__(self, oeb, context):
        has_toc = getattr(getattr(oeb, 'toc', False), 'nodes', False)

        if 'toc' in oeb.guide:
            # Ensure toc pointed to in <guide> is in spine
            from calibre.ebooks.oeb.base import urlnormalize
            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:
                        if self.position == 'end':
                            oeb.spine.add(item, linear=False)
                        else:
                            oeb.spine.insert(0, item, linear=True)
                    return
                elif has_toc:
                    oeb.guide.remove('toc')
            else:
                oeb.guide.remove('toc')
        if not has_toc:
            return
        oeb.logger.info('Generating in-line TOC...')
        title = self.title or oeb.translate(DEFAULT_TITLE)
        style = self.style
        if style not in STYLE_CSS:
            oeb.logger.error('Unknown TOC style %r' % style)
            style = 'nested'
        id, css_href = oeb.manifest.generate('tocstyle', 'tocstyle.css')
        oeb.manifest.add(id, css_href, CSS_MIME, data=STYLE_CSS[style])
        language = unicode_type(oeb.metadata.language[0])
        contents = element(None,
                           XHTML('html'),
                           nsmap={None: XHTML_NS},
                           attrib={XML('lang'): language})
        head = element(contents, XHTML('head'))
        htitle = element(head, XHTML('title'))
        htitle.text = title
        element(head,
                XHTML('link'),
                rel='stylesheet',
                type=CSS_MIME,
                href=css_href)
        body = element(contents,
                       XHTML('body'),
                       attrib={'class': 'calibre_toc'})
        h1 = element(body, XHTML('h2'), attrib={'class': 'calibre_toc_header'})
        h1.text = title
        self.add_toc_level(body, oeb.toc)
        id, href = oeb.manifest.generate('contents', 'contents.xhtml')
        item = oeb.manifest.add(id, href, XHTML_MIME, data=contents)
        if self.position == 'end':
            oeb.spine.add(item, linear=False)
        else:
            oeb.spine.insert(0, item, linear=True)
        oeb.guide.add('toc', 'Table of Contents', href)
Beispiel #8
0
    def __call__(self, oeb, context):
        has_toc = getattr(getattr(oeb, 'toc', False), 'nodes', False)

        if 'toc' in oeb.guide:
            # Ensure toc pointed to in <guide> is in spine
            from calibre.ebooks.oeb.base import urlnormalize
            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:
                        if self.position == 'end':
                            oeb.spine.add(item, linear=False)
                        else:
                            oeb.spine.insert(0, item, linear=True)
                    return
                elif has_toc:
                    oeb.guide.remove('toc')
            else:
                oeb.guide.remove('toc')
        if not has_toc:
            return
        oeb.logger.info('Generating in-line TOC...')
        title = self.title or oeb.translate(DEFAULT_TITLE)
        style = self.style
        if style not in STYLE_CSS:
            oeb.logger.error('Unknown TOC style %r' % style)
            style = 'nested'
        id, css_href = oeb.manifest.generate('tocstyle', 'tocstyle.css')
        oeb.manifest.add(id, css_href, CSS_MIME, data=STYLE_CSS[style])
        language = str(oeb.metadata.language[0])
        contents = element(None, XHTML('html'), nsmap={None: XHTML_NS},
                           attrib={XML('lang'): language})
        head = element(contents, XHTML('head'))
        htitle = element(head, XHTML('title'))
        htitle.text = title
        element(head, XHTML('link'), rel='stylesheet', type=CSS_MIME,
                href=css_href)
        body = element(contents, XHTML('body'),
                       attrib={'class': 'calibre_toc'})
        h1 = element(body, XHTML('h2'),
                     attrib={'class': 'calibre_toc_header'})
        h1.text = title
        self.add_toc_level(body, oeb.toc)
        id, href = oeb.manifest.generate('contents', 'contents.xhtml')
        item = oeb.manifest.add(id, href, XHTML_MIME, data=contents)
        if self.position == 'end':
            oeb.spine.add(item, linear=False)
        else:
            oeb.spine.insert(0, item, linear=True)
        oeb.guide.add('toc', 'Table of Contents', href)
Beispiel #9
0
    def __call__(self, oeb, context):
        has_toc = getattr(getattr(oeb, "toc", False), "nodes", False)

        if "toc" in oeb.guide:
            # Ensure toc pointed to in <guide> is in spine
            from calibre.ebooks.oeb.base import urlnormalize

            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:
                        if self.position == "end":
                            oeb.spine.add(item, linear=False)
                        else:
                            oeb.spine.insert(0, item, linear=True)
                    return
                elif has_toc:
                    oeb.guide.remove("toc")
            else:
                oeb.guide.remove("toc")
        if not has_toc:
            return
        oeb.logger.info("Generating in-line TOC...")
        title = self.title or oeb.translate(DEFAULT_TITLE)
        style = self.style
        if style not in STYLE_CSS:
            oeb.logger.error("Unknown TOC style %r" % style)
            style = "nested"
        id, css_href = oeb.manifest.generate("tocstyle", "tocstyle.css")
        oeb.manifest.add(id, css_href, CSS_MIME, data=STYLE_CSS[style])
        language = str(oeb.metadata.language[0])
        contents = element(None, XHTML("html"), nsmap={None: XHTML_NS}, attrib={XML("lang"): language})
        head = element(contents, XHTML("head"))
        htitle = element(head, XHTML("title"))
        htitle.text = title
        element(head, XHTML("link"), rel="stylesheet", type=CSS_MIME, href=css_href)
        body = element(contents, XHTML("body"), attrib={"class": "calibre_toc"})
        h1 = element(body, XHTML("h1"), attrib={"class": "calibre_toc_header"})
        h1.text = title
        self.add_toc_level(body, oeb.toc)
        id, href = oeb.manifest.generate("contents", "contents.xhtml")
        item = oeb.manifest.add(id, href, XHTML_MIME, data=contents)
        if self.position == "end":
            oeb.spine.add(item, linear=False)
        else:
            oeb.spine.insert(0, item, linear=True)
        oeb.guide.add("toc", "Table of Contents", href)
Beispiel #10
0
 def add_toc_level(self, elem, toc):
     for node in toc:
         block = element(elem, XHTML("div"), attrib={"class": "calibre_toc_block"})
         line = element(block, XHTML("a"), attrib={"href": node.href, "class": "calibre_toc_line"})
         line.text = node.title
         self.add_toc_level(block, node)