1
+ import shutil
2
+
1
3
import marko
2
- from marko .md_renderer import MarkdownRenderer
3
4
from marko .block import Heading
5
+ from marko .md_renderer import MarkdownRenderer
6
+ from mkdocs import plugins
7
+ from mkdocs .config .defaults import MkDocsConfig
4
8
from mkdocs .structure .files import Files
5
9
from mkdocs .structure .nav import Link
6
10
from mkdocs .structure .nav import Navigation
7
11
from mkdocs .structure .pages import Page
8
- from mkdocs .config .defaults import MkDocsConfig
9
- import shutil
10
12
11
13
MAN_INDEXES = ["man1/index.md" , "man3/index.md" , "man5/index.md" , "man7/index.md" ]
12
14
SKIP_FILES = ["index.md" , "fips.md" ]
@@ -24,7 +26,7 @@ def get_names_paragraph(content: str) -> str:
24
26
if append :
25
27
paragraph_lines .append (line )
26
28
return " " .join (paragraph_lines )
27
-
29
+
28
30
29
31
def get_names (content : str ) -> list [str ]:
30
32
names_paragraph = get_names_paragraph (content )
@@ -41,7 +43,9 @@ def on_pre_build(config: MkDocsConfig) -> None:
41
43
shutil .copytree ("scaffold" , "docs" , dirs_exist_ok = True )
42
44
43
45
44
- def populate_index_content (source_md : str , page : Page , files : Files ) -> str :
46
+ def populate_index_content (source_md : str , page : Page , config : MkDocsConfig , files : Files ) -> str :
47
+ if page .file .src_uri not in MAN_INDEXES :
48
+ return source_md
45
49
current_man_dir = page .parent .title .lower ()
46
50
rows = []
47
51
for man_file in files .documentation_pages ():
@@ -60,7 +64,9 @@ def populate_index_content(source_md: str, page: Page, files: Files) -> str:
60
64
return source_md + "\n " .join (sorted (rows ))
61
65
62
66
63
- def fix_headings (source_md : str , page : Page ) -> str :
67
+ def fix_headings (source_md : str , page : Page , config : MkDocsConfig , files : Files ) -> str :
68
+ if page .file .src_uri in SKIP_FILES + MAN_INDEXES :
69
+ return source_md
64
70
parser = marko .Markdown (renderer = MarkdownRenderer )
65
71
new_children = []
66
72
h1_parsed = parser .parse (f"# { page .file .name } " )
@@ -78,12 +84,13 @@ def fix_headings(source_md: str, page: Page) -> str:
78
84
return parser .render (parsed )
79
85
80
86
81
- def on_page_markdown (source_md : str , page : Page , config : MkDocsConfig , files : Files ) -> str :
82
- if page .file .src_uri in SKIP_FILES :
87
+ def fix_img_links (source_md : str , page : Page , config : MkDocsConfig , files : Files ) -> str :
88
+ if not page .file .name . startswith ( "life_cycle-" ) :
83
89
return source_md
84
- if page .file .src_uri in MAN_INDEXES :
85
- return populate_index_content (source_md , page , files )
86
- return fix_headings (source_md , page )
90
+ return source_md .replace ('<img src="' , '<img src="../' )
91
+
92
+
93
+ on_page_markdown = plugins .CombinedEvent (fix_headings , fix_img_links , populate_index_content )
87
94
88
95
89
96
def populate_nav (files : Files ) -> dict [str , list [Link ]]:
@@ -115,7 +122,7 @@ def on_nav(nav: Navigation, config: MkDocsConfig, files: Files) -> Navigation:
115
122
"man1" : "Commands" ,
116
123
"man3" : "Libraries" ,
117
124
"man5" : "File Formats" ,
118
- "man7" : "Overviews"
125
+ "man7" : "Overviews" ,
119
126
}
120
127
nav_children = populate_nav (files )
121
128
for item in nav .items :
0 commit comments