chapisha/helpers/pages.py
def create_toc_ncx(metadata: WorkMetadata, spine: list[Matter]) -> str:
    """
    Load the default `toc.ncx` file, and generate the required terms for the creative work. Return xhtml as a string.
    
    Parameters
    ----------
    metadata: WorkMetadata
        All the terms for updating the work, not all compulsory
    spine: list of Matter
        Spine and guide list of Matter, with `dedication` at 0, if present

    Returns
    -------
    str: xhtml response for `toc.ncx` as a str.
    """
    with open(DATA_PATH / "xhtml" / DEFAULT_TOC_NCX, "r+", encoding="utf-8") as toc_file:
        toc_xml = toc_file.read()
        ################################################################################################################
        # METADATA
        ################################################################################################################
        # dc:identifier
        toc_xml = toc_xml.replace('<meta content="" name="dtb:uid"/>', 
                                  F'<meta name="dtb:uid" content="{metadata.identifier}"/>')
        ################################################################################################################
        # NAVMAP
        ################################################################################################################
        navpoint = """\t\t<navPoint id="navpoint-{}" playOrder="{}">\n\t\t\t<navLabel>\n\t\t\t\t<text>{}</text>\n\t\t\t</navLabel>\n\t\t\t<content src="text/{}.xhtml"/>\n\t\t</navPoint>\n"""
        navmap_xml = ""
        navcount = 1
        chapter = 1
        # Add Titlepage
        navmap_xml += navpoint.format(navcount, navcount, "Title page", "titlepage")
        for matter in spine:
            navcount += 1
            if matter.content == FrontMatter.dedication:
                navmap_xml += navpoint.format(navcount, navcount, matter.title, "dedication")
            if matter.partition == MatterPartition.body:
                navmap_xml += navpoint.format(navcount, navcount, matter.title, F"chapter-{chapter}")
                chapter += 1
        # Add Colophon
        navmap_xml += navpoint.format(navcount+1, navcount+1, "Colophon", "colophon")
        toc_xml = toc_xml.replace(navpoint.format(1, 1, "Title page", "titlepage"),
                                  navmap_xml)
    return toc_xml

    Load the default `toc.ncx` file, and generate the required terms for the creative work. Return xhtml as a string.
    
    Parameters
    ----------
    metadata: WorkMetadata
        All the terms for updating the work, not all compulsory
    spine: list of Matter
        Spine and guide list of Matter, with `dedication` at 0, if present

    Returns
    -------
    str: xhtml response for `toc.ncx` as a str.
    
Load the default `toc.ncx` file, and generate the required terms for the creative work. Return xhtml as a string.
Parameters

WorkMetadata
All the terms for updating the work, not all compulsory
spine: list of Matter
Spine and guide list of Matter, with `dedication` at 0, if present

Returns

xhtml response for `toc.ncx` as a str.
Load the default ` toc . ncx ` file and generate the required terms for the creative work . Return xhtml as a string . Parameters WorkMetadata All the terms for updating the work not all compulsory spine : list of Matter Spine and guide list of Matter with ` dedication ` at 0 if present Returns xhtml response for ` toc . ncx ` as a str .
def create_toc_ncx(metadata: WorkMetadata, spine: list[Matter]) -> str:
    with open(DATA_PATH / "xhtml" / DEFAULT_TOC_NCX, "r+", encoding="utf-8") as toc_file:
        toc_xml = toc_file.read()
        toc_xml = toc_xml.replace('<meta content="" name="dtb:uid"/>', 
                                  F'<meta name="dtb:uid" content="{metadata.identifier}"/>')
        navpoint = """\t\t<navPoint id="navpoint-{}" playOrder="{}">\n\t\t\t<navLabel>\n\t\t\t\t<text>{}</text>\n\t\t\t</navLabel>\n\t\t\t<content src="text/{}.xhtml"/>\n\t\t</navPoint>\n"""
        navmap_xml = ""
        navcount = 1
        chapter = 1
        navmap_xml += navpoint.format(navcount, navcount, "Title page", "titlepage")
        for matter in spine:
            navcount += 1
            if matter.content == FrontMatter.dedication:
                navmap_xml += navpoint.format(navcount, navcount, matter.title, "dedication")
            if matter.partition == MatterPartition.body:
                navmap_xml += navpoint.format(navcount, navcount, matter.title, F"chapter-{chapter}")
                chapter += 1
        navmap_xml += navpoint.format(navcount+1, navcount+1, "Colophon", "colophon")
        toc_xml = toc_xml.replace(navpoint.format(1, 1, "Title page", "titlepage"),
                                  navmap_xml)
    return toc_xml
def create_toc_ncx ( metadata : WorkMetadata , spine : list [ Matter ] ) -> str : with open ( DATA_PATH / "xhtml" / DEFAULT_TOC_NCX , "r+" , encoding = "utf-8" ) as toc_file : toc_xml = toc_file . read ( ) toc_xml = toc_xml . replace ( '<meta content="" name="dtb:uid"/>' , F'<meta name="dtb:uid" content="{metadata.identifier}"/>' ) navpoint = """\t\t<navPoint id="navpoint-{}" playOrder="{}">\n\t\t\t<navLabel>\n\t\t\t\t<text>{}</text>\n\t\t\t</navLabel>\n\t\t\t<content src="text/{}.xhtml"/>\n\t\t</navPoint>\n""" navmap_xml = "" navcount = 1 chapter = 1 navmap_xml += navpoint . format ( navcount , navcount , "Title page" , "titlepage" ) for matter in spine : navcount += 1 if matter . content == FrontMatter . dedication : navmap_xml += navpoint . format ( navcount , navcount , matter . title , "dedication" ) if matter . partition == MatterPartition . body : navmap_xml += navpoint . format ( navcount , navcount , matter . title , F"chapter-{chapter}" ) chapter += 1 navmap_xml += navpoint . format ( navcount + 1 , navcount + 1 , "Colophon" , "colophon" ) toc_xml = toc_xml . replace ( navpoint . format ( 1 , 1 , "Title page" , "titlepage" ) , navmap_xml ) return toc_xml
Load the default `toc.ncx` file, and generate the required terms for the creative work.
Load the default ` toc . ncx ` file and generate the required terms for the creative work .
"""
    Load the default `toc.ncx` file, and generate the required terms for the creative work. Return xhtml as a string.
    
    Parameters
    ----------
    metadata: WorkMetadata
        All the terms for updating the work, not all compulsory
    spine: list of Matter
        Spine and guide list of Matter, with `dedication` at 0, if present

    Returns
    -------
    str: xhtml response for `toc.ncx` as a str.
    """ ################################################################################################################ # METADATA ################################################################################################################ # dc:identifier ################################################################################################################ # NAVMAP ################################################################################################################ # Add Titlepage # Add Colophon
metadata
WorkMetadata
spine
list[Matter]