1
+ import re
1
2
import jinja2
2
3
import logging
3
4
@@ -21,7 +22,7 @@ class Renderer(object):
21
22
def __init__ (
22
23
self ,
23
24
plugin_config ,
24
- mkdocs_config = {} ,
25
+ mkdocs_config = None ,
25
26
cover_page_template_path = "" ,
26
27
banner_template_path = "" ,
27
28
print_page = None ,
@@ -30,7 +31,7 @@ def __init__(
30
31
Inits the class.
31
32
"""
32
33
self .plugin_config = plugin_config
33
- self .mkdocs_config = mkdocs_config
34
+ self .mkdocs_config = mkdocs_config or {}
34
35
self .cover_page_template_path = cover_page_template_path
35
36
self .banner_template_path = banner_template_path
36
37
self .print_page = print_page
@@ -82,26 +83,25 @@ def get_html_from_items(
82
83
if item .is_page :
83
84
# Do not include page in print page if excluded
84
85
if exclude (item .file .src_path , excluded_pages ):
85
- logging .debug ("Excluding page " + item .file .src_path )
86
+ logging .debug (f "Excluding page ' { item .file .src_path } '" )
86
87
continue
87
88
88
89
# If you specify the same page twice in your navigation, it is only rendered once
89
90
# so we need to check if the html attribute exists
90
91
if hasattr (item , "html" ):
91
92
if item .html == "" :
92
- logger .warning (
93
- "[mkdocs-print-site] %s is empty and will be ignored"
94
- % item .file .src_path
95
- )
93
+ logger .warning (f"[mkdocs-print-site] { item .file .src_path } is empty and will be ignored" )
96
94
continue
97
95
98
96
item_html = item .html
99
97
100
- # Add missing h1 tag if it doesn't exist
101
- if not item_html .startswith ("<h1" ):
102
- item_html = f"<h1 id=\" { to_snake_case (item .title )} \" >{ item .title } </h1>{ item_html } "
103
- logger .warning (f"[mkdocs-print-site] '{ item .file .src_path } ' file is missing a leading h1 tag. Added to the print-page with title '{ item .title } '" )
104
-
98
+ # Add missing h1 tag if the first heading is not a h1
99
+ match = re .search (r"\<h[0-6]" , item_html )
100
+ if match :
101
+ if not match .group () == "<h1" :
102
+ item_html = f"<h1 id=\" { to_snake_case (item .title )} \" >{ item .title } </h1>{ item_html } "
103
+ logger .warning (f"[mkdocs-print-site] '{ item .file .src_path } ' file is missing a leading h1 tag. Added to the print-page with title '{ item .title } '" )
104
+
105
105
# Support mkdocs-material tags
106
106
# See https://squidfunk.github.io/mkdocs-material/plugins/tags
107
107
if "tags" in item .meta :
0 commit comments