Example #1
0
def organize_pages(directory: Path,
                   root: Path = Path(),
                   text: str = "") -> Optional[Tree[PaginatedAnchor]]:
    if not directory.is_dir():
        return None
    pages: list[Path] = []
    subdirectories: list[Path] = []
    children: list[Tree[PaginatedAnchor]] = []
    for path in directory.iterdir():
        if PaginatedImagesBuildJob.is_page(path):
            pages.append(path)
        elif path.is_dir():
            subdirectories.append(path)
    for subdirectory, subdirectory_text in sort_directories(subdirectories):
        tree = organize_pages(subdirectory, root, subdirectory_text)
        if tree is not None:
            children.append(tree)
    sorted_pages = sort_files(pages)
    if len(sorted_pages) > 0:
        href = sorted_pages[0].relative_to(root).with_suffix(".xhtml")
    elif len(sorted_pages) == 0 and len(children) > 0:
        href = children[0].value.href
    else:
        return None
    return Tree(PaginatedAnchor(text, href, sorted_pages), children)
Example #2
0
    def _write_navigation_document(
        self,
        epub_version: Optional[EPUBVersion] = None,
        add_to_spine: bool = True
    ) -> None:
        if epub_version is None:
            epub_version = self.epub_version

        navigation = Path(self._project.special_names.navigation_document)
        self._resource_manager.resources[navigation] = EPUBResource(
            navigation,
            manifest_properties={"nav"}
        )
        nav_anchor = TypedAnchor("Table of Contents", navigation, "toc")
        if add_to_spine:
            self._progression.append(navigation)
            self._nav_extras_front.append(Tree(nav_anchor, []))
            self._landmarks.append(nav_anchor)
        bodymatter_progression: list[Path] = []
        for nav_tree in self._project.nav_trees:
            for anchor in nav_tree:
                if len(anchor.href.name.split("#")) == 1:
                    bodymatter_progression.append(anchor.href)
        if len(bodymatter_progression) > 0:
            if epub_version is EPUBVersion.EPUB2:
                start_type = "text"
            elif epub_version is EPUBVersion.EPUB3:
                start_type = "bodymatter"
            self._landmarks.append(
                TypedAnchor(
                    "Begin Reading",
                    bodymatter_progression[0],
                    start_type
                )
            )
        self._progression.extend(bodymatter_progression)
        self._progression = list(dict.fromkeys(self._progression))

        nav_trees = [
            *self._nav_extras_front,
            *self._project.nav_trees,
            *self._nav_extras_back
        ]
        document = NavigationDocument(
            nav_trees,
            self._landmarks
        )
        document_path = Path(
            self._resource_manager.root,
            self._project.special_names.navigation_document
        )

        if epub_version is EPUBVersion.EPUB2:
            document.write_epub2(document_path)
        elif epub_version is EPUBVersion.EPUB3:
            document.write_epub3(document_path)
Example #3
0
def nav_trees_nonempty() -> list[Tree[Anchor]]:
    nav_trees: list[Tree[Anchor]] = []

    cover = Tree(Anchor("Cover", Path("_cover.xhtml")), [])
    nav_trees.append(cover)

    nav = Tree(Anchor("Table of Contents", Path("_nav.xhtml")), [])
    nav_trees.append(nav)

    chapter_1 = Tree(Anchor("Chapter 1", Path("chapter_1.xhtml")), [])
    nav_trees.append(chapter_1)

    chapter_2_1 = Tree(Anchor("Chapter 2.1", Path("chapter_2.xhtml#id-1")), [])
    chapter_2_2 = Tree(Anchor("Chapter 2.2", Path("chapter_2.xhtml#id-2")), [])
    chapter_2 = Tree(Anchor("Chapter 2", Path("chapter_2.xhtml")),
                     [chapter_2_1, chapter_2_2])
    nav_trees.append(chapter_2)

    chapter_3_1 = Tree(Anchor("Chapter 3.1", Path("chapter_3", "1.xhtml")), [])
    chapter_3 = Tree(Anchor("Chapter 3", Path("chapter_3.xhtml")),
                     [chapter_3_1])
    nav_trees.append(chapter_3)

    return nav_trees
Example #4
0
    def _write_cover(
        self,
        epub_version: Optional[EPUBVersion] = None,
        add_to_spine: bool = True
    ) -> None:
        if epub_version is None:
            epub_version = self.epub_version

        if not self._project.epub_metadata.cover:
            default_cover = Path("_cover.jpg")
            fill_blank_cover(Path(self._resource_manager.root, default_cover))
            self._project.epub_metadata.cover = default_cover
        self._resource_manager.resources[
            self._project.epub_metadata.cover
        ] = EPUBResource(
            self._project.epub_metadata.cover,
            manifest_properties={"cover-image"}
        )
        document = CoverXHTML(self._project.epub_metadata.cover)
        document_path = Path(
            self._resource_manager.root,
            self._project.special_names.cover_xhtml
        )

        if epub_version is EPUBVersion.EPUB2:
            document.write_epub2(document_path)
        elif epub_version is EPUBVersion.EPUB3:
            document.write_epub3(document_path)

        cover_xhtml_path = Path(self._project.special_names.cover_xhtml)
        self._resource_manager.resources[cover_xhtml_path] = EPUBResource(
            cover_xhtml_path
        )
        cover_xhtml_anchor = TypedAnchor("Cover", cover_xhtml_path, "cover")
        if add_to_spine:
            self._progression.append(cover_xhtml_path)
            self._nav_extras_front.append(Tree(cover_xhtml_anchor, []))
            self._landmarks.append(cover_xhtml_anchor)
Example #5
0
def tree_single() -> Tree[int]:
    return Tree(1, [])
Example #6
0
def tree_multi() -> Tree[int]:
    return Tree(1, [
        Tree(21, [Tree(31, []), Tree(32, [])]),
        Tree(22, [Tree(33, [])]),
        Tree(23, [Tree(34, []), Tree(35, [])])
    ])
Example #7
0
def nav_dict_to_tree(nav_dict: NavDict) -> Tree[Anchor]:
    return Tree(
        Anchor(nav_dict["text"], Path(nav_dict["href"])),
        [nav_dict_to_tree(child) for child in nav_dict["children"]]
    )