@@ -202,26 +202,78 @@ def get_toc_sidebar(self) -> TableOfContents:
202
202
"""
203
203
toc = []
204
204
205
+ if self .plugin_config .get ("enumerate_headings" ):
206
+ chapter_number = 0
207
+ section_number = 0
208
+
205
209
for item in self ._get_items ():
206
210
if item .is_page :
207
211
page_key = get_page_key (item .url )
208
212
# navigate to top of page if page is homepage
209
213
if page_key == "index" :
210
214
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
+
212
223
if item .is_section :
213
224
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
+
214
231
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
216
233
)
217
234
218
235
subpages = [p for p in item .children if p .is_page ]
219
236
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
+
220
243
page_key = get_page_key (page .url )
221
244
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 )
223
246
)
224
247
225
248
toc .append (section_link )
226
249
227
250
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
0 commit comments