11import logging
22from fnmatch import fnmatch
33from pathlib import Path
4- from typing import Dict , Iterable , List , NamedTuple , Optional , Tuple
54from tempfile import TemporaryDirectory
5+ from typing import Dict , Iterable , List , NamedTuple , Tuple
6+
67from mkdocs .structure .files import File
78
89log = logging .getLogger (__name__ )
@@ -34,7 +35,7 @@ class FilesGenerator:
3435 base_path : Path
3536 _temp_dir : TemporaryDirectory
3637
37- def __init__ (self , dest_dir : str , use_directory_urls : bool ):
38+ def __init__ (self , dest_dir : str , use_directory_urls : bool , init_section_index : bool ):
3839 """
3940 :param dest_dir: Configured mkdocs destination directory
4041 :param use_directory_urls: Configured mkdocs use_directory_urls
@@ -43,6 +44,7 @@ def __init__(self, dest_dir: str, use_directory_urls: bool):
4344 self ._temp_dir = TemporaryDirectory ()
4445 self .base_path = Path (self ._temp_dir .name )
4546
47+ self ._init_section_index = init_section_index
4648 self ._dest_dir = dest_dir
4749 self ._use_directory_urls = use_directory_urls
4850
@@ -73,9 +75,9 @@ def generate_page(self, source_base_path: Path, source_path: Path, page_template
7375 with open (target_path , "xt" , encoding = "utf8" ) as page :
7476 page .write (
7577 page_template .format (
76- module_name = module_id [ - 1 ] ,
77- printable_module_id = module_name (module_id ),
78- module_id = module_name (module_id ),
78+ module_name = self . module_name ( module_id , full_name = False , printable = True ) ,
79+ printable_module_id = self . module_name (module_id , full_name = True , printable = True ),
80+ module_id = self . module_name (module_id , full_name = True , printable = False ),
7981 )
8082 )
8183
@@ -114,10 +116,29 @@ def generate_pages_recursive(self, source: Source, template: str) -> List[FileEn
114116 return entries
115117
116118 def file_path_for_module_id (self , module_id : ModuleId ) -> Path :
117- if module_id [- 1 ] == "__init__" :
119+ """
120+ Format a .md file path for a file
121+ :param module_id: The module id
122+ :return: An absolute path
123+ """
124+ if self ._init_section_index and module_id [- 1 ] == "__init__" :
118125 module_id = module_id [:- 1 ] + ("README.md" ,)
119126 return Path (self .base_path , "_ref" , * module_id ).with_suffix (".md" )
120127
128+ def module_name (self , module_id : ModuleId , full_name : bool , printable : bool ) -> str :
129+ """
130+ Get the module name for a given module_id
131+
132+ :param module_id: Module ID generated by the function ``module_id()``
133+ :param full_name: If true, the full module name "foo.bar.baz" will be returned. Otherwise, just the last section
134+ "baz" will be returned.
135+ :param printable: Make adjustments for rendering this name. If false, the name must match something python
136+ can import. If True it will be modified to be more pleasing on the eye.
137+ """
138+ if module_id [- 1 ] == "__init__" and (not printable or self ._init_section_index ):
139+ module_id = module_id [:- 1 ]
140+ return "." .join (module_id ) if full_name else module_id [- 1 ]
141+
121142
122143def get_module_id (module_path : Path , base_path : Path ) -> ModuleId :
123144 """
@@ -129,16 +150,6 @@ def get_module_id(module_path: Path, base_path: Path) -> ModuleId:
129150 return module_path .relative_to (base_path ).with_suffix ("" ).parts
130151
131152
132- def module_name (module_id : ModuleId ) -> str :
133- """
134- Get the module name for a given module_id
135-
136- :param module_id: Module ID generated by the function ``module_id()``
137- """
138- if module_id [- 1 ] == "__init__" :
139- module_id = module_id [:- 1 ]
140- return "." .join (module_id )
141-
142153
143154def discover_python_files (source_dir : Path , ignore : List [str ]) -> Iterable [Path ]:
144155 """
0 commit comments