Skip to content

Commit 58b4b76

Browse files
committed
Enumerate ToC sidebar in HTML version, closes #55
1 parent 11faed7 commit 58b4b76

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

mkdocs_print_site_plugin/renderer.py

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,26 +202,78 @@ def get_toc_sidebar(self) -> TableOfContents:
202202
"""
203203
toc = []
204204

205+
if self.plugin_config.get("enumerate_headings"):
206+
chapter_number = 0
207+
section_number = 0
208+
205209
for item in self._get_items():
206210
if item.is_page:
207211
page_key = get_page_key(item.url)
208212
# navigate to top of page if page is homepage
209213
if page_key == "index":
210214
page_key = ""
211-
toc.append(AnchorLink(title=item.title, id=f"{page_key}", level=0))
215+
216+
if self.plugin_config.get("enumerate_headings"):
217+
chapter_number += 1
218+
title = f"{chapter_number}. {item.title}"
219+
else:
220+
title = item.title
221+
toc.append(AnchorLink(title=title, id=f"{page_key}", level=0))
222+
212223
if item.is_section:
213224

225+
if self.plugin_config.get("enumerate_headings"):
226+
section_number += 1
227+
title = f"{int_to_roman(section_number)}. {item.title}"
228+
else:
229+
title = item.title
230+
214231
section_link = AnchorLink(
215-
title=item.title, id=f"section-{to_snake_case(item.title)}", level=0
232+
title=title, id=f"section-{to_snake_case(item.title)}", level=0
216233
)
217234

218235
subpages = [p for p in item.children if p.is_page]
219236
for page in subpages:
237+
if self.plugin_config.get("enumerate_headings"):
238+
chapter_number += 1
239+
title = f"{chapter_number}. {page.title}"
240+
else:
241+
title = page.title
242+
220243
page_key = get_page_key(page.url)
221244
section_link.children.append(
222-
AnchorLink(title=page.title, id=f"{page_key}", level=1)
245+
AnchorLink(title=title, id=f"{page_key}", level=1)
223246
)
224247

225248
toc.append(section_link)
226249

227250
return TableOfContents(toc)
251+
252+
253+
254+
def int_to_roman(num):
255+
"""
256+
Integer to roman number.
257+
258+
Copied from https://www.w3resource.com/python-exercises/class-exercises/python-class-exercise-1.php
259+
"""
260+
lookup = [
261+
(1000, 'M'),
262+
(900, 'CM'),
263+
(500, 'D'),
264+
(400, 'CD'),
265+
(100, 'C'),
266+
(90, 'XC'),
267+
(50, 'L'),
268+
(40, 'XL'),
269+
(10, 'X'),
270+
(9, 'IX'),
271+
(5, 'V'),
272+
(4, 'IV'),
273+
(1, 'I'),
274+
]
275+
res = ''
276+
for (n, roman) in lookup:
277+
(d, num) = divmod(num, n)
278+
res += roman * d
279+
return res

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup(
1111
name="mkdocs-print-site-plugin",
12-
version="2.3.1",
12+
version="2.3.2",
1313
description="MkDocs plugin that combines all pages into one, allowing for easy export to PDF and standalone HTML.",
1414
long_description=long_description,
1515
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)