@@ -71,27 +71,23 @@ def on_config(self, config, **kwargs):
71
71
# Check if 'hooks' is defined in the 'plugins' section
72
72
if isinstance (config .get ("hooks" ), dict ):
73
73
position_offset += len (config .get ("hooks" ))
74
-
74
+
75
75
if print_site_position != len (plugins ) - position_offset :
76
76
msg = "[mkdocs-print-site] 'print-site' should be defined as the *last* plugin,"
77
77
msg += "to ensure the print page has any changes other plugins make."
78
78
msg += "Please update the 'plugins:' section in your mkdocs.yml"
79
79
logger .warning (msg )
80
80
81
81
if "--dirtyreload" in sys .argv :
82
- msg = (
83
- "[mkdocs-print-site] Note the 'print-site' page does render all pages "
84
- )
82
+ msg = "[mkdocs-print-site] Note the 'print-site' page does render all pages "
85
83
msg += "when using the --dirtyreload option."
86
84
logger .warning (msg )
87
85
88
86
# Get abs path to cover_page_template
89
87
self .cover_page_template_path = ""
90
88
if self .config .get ("add_cover_page" ):
91
89
if self .config .get ("cover_page_template" ) == "" :
92
- self .cover_page_template_path = os .path .join (
93
- HERE , "templates" , "cover_page.tpl"
94
- )
90
+ self .cover_page_template_path = os .path .join (HERE , "templates" , "cover_page.tpl" )
95
91
else :
96
92
self .cover_page_template_path = os .path .join (
97
93
os .path .dirname (config .get ("config_file_path" )),
@@ -101,17 +97,13 @@ def on_config(self, config, **kwargs):
101
97
msg = "[print-site-plugin]: Path specified in 'cover_page_template' not found."
102
98
msg += "\n Make sure to use the URL relative to your mkdocs.yml file."
103
99
logger .warning (msg )
104
- raise FileNotFoundError (
105
- "File not found: %s" % self .cover_page_template_path
106
- )
100
+ raise FileNotFoundError ("File not found: %s" % self .cover_page_template_path )
107
101
108
102
# Get abs path to print_site_banner_template
109
103
self .banner_template_path = ""
110
104
if self .config .get ("add_print_site_banner" ):
111
105
if self .config .get ("print_site_banner_template" ) == "" :
112
- self .banner_template_path = os .path .join (
113
- HERE , "templates" , "print_site_banner.tpl"
114
- )
106
+ self .banner_template_path = os .path .join (HERE , "templates" , "print_site_banner.tpl" )
115
107
else :
116
108
self .banner_template_path = os .path .join (
117
109
os .path .dirname (config .get ("config_file_path" )),
@@ -121,9 +113,7 @@ def on_config(self, config, **kwargs):
121
113
msg = "[print-site-plugin]: Path specified in 'print_site_banner_template' not found."
122
114
msg += "\n Make sure to use the URL relative to your mkdocs.yml file."
123
115
logger .warning (msg )
124
- raise FileNotFoundError (
125
- "File not found: %s" % self .banner_template_path
126
- )
116
+ raise FileNotFoundError ("File not found: %s" % self .banner_template_path )
127
117
128
118
# Add pointer to print-site javascript
129
119
config ["extra_javascript" ] = ["js/print-site.js" ] + config ["extra_javascript" ]
@@ -146,7 +136,6 @@ def on_config(self, config, **kwargs):
146
136
147
137
config ["extra_css" ] = self .enum_css_files + config ["extra_css" ]
148
138
149
-
150
139
# Create MkDocs Page and File instances
151
140
self .print_file = File (
152
141
path = self .config .get ("print_page_basename" ) + ".md" ,
@@ -217,9 +206,7 @@ def on_page_content(self, html, page, config, files, **kwargs):
217
206
if is_external (pdf_url ):
218
207
page .url_to_pdf = pdf_url
219
208
else :
220
- page .url_to_pdf = get_relative_url (
221
- pdf_url , page .file .url
222
- )
209
+ page .url_to_pdf = get_relative_url (pdf_url , page .file .url )
223
210
224
211
return html
225
212
@@ -262,11 +249,14 @@ def on_template_context(self, context, template_name, config, **kwargs):
262
249
if template_name == "404.html" :
263
250
self .context = context
264
251
# Make sure paths are OK
265
- if config .get ('extra_css' ):
266
- self .context ['extra_css' ] = [get_relative_url (f , self .print_page .file .url ) for f in config .get ('extra_css' )]
267
- if config .get ('extra_javascript' ):
268
- self .context ['extra_javascript' ] = [get_relative_url (str (f ), self .print_page .file .url ) for f in config .get ('extra_javascript' )]
269
-
252
+ if config .get ("extra_css" ):
253
+ self .context ["extra_css" ] = [
254
+ get_relative_url (f , self .print_page .file .url ) for f in config .get ("extra_css" )
255
+ ]
256
+ if config .get ("extra_javascript" ):
257
+ self .context ["extra_javascript" ] = [
258
+ get_relative_url (str (f ), self .print_page .file .url ) for f in config .get ("extra_javascript" )
259
+ ]
270
260
271
261
def on_post_build (self , config , ** kwargs ):
272
262
"""
@@ -292,25 +282,19 @@ def on_post_build(self, config, **kwargs):
292
282
# Add print-site.css
293
283
css_output_base_path = os .path .join (config ["site_dir" ], "css" )
294
284
css_file_path = os .path .join (css_output_base_path , "print-site.css" )
295
- copy_file (
296
- os .path .join (os .path .join (HERE , "css" ), "print-site.css" ), css_file_path
297
- )
285
+ copy_file (os .path .join (os .path .join (HERE , "css" ), "print-site.css" ), css_file_path )
298
286
299
287
# Add enumeration css
300
288
for f in self .enum_css_files :
301
289
f = f .replace ("/" , os .sep )
302
290
css_file_path = os .path .join (config ["site_dir" ], f )
303
- copy_file (
304
- os .path .join (HERE , f ), css_file_path
305
- )
291
+ copy_file (os .path .join (HERE , f ), css_file_path )
306
292
307
293
# Add theme CSS file
308
294
css_file = "print-site-%s.css" % get_theme_name (config )
309
295
if css_file in os .listdir (os .path .join (HERE , "css" )):
310
296
css_file_path = os .path .join (css_output_base_path , css_file )
311
- copy_file (
312
- os .path .join (os .path .join (HERE , "css" ), css_file ), css_file_path
313
- )
297
+ copy_file (os .path .join (os .path .join (HERE , "css" ), css_file ), css_file_path )
314
298
315
299
# Combine the HTML of all pages present in the navigation
316
300
self .print_page .content , self .print_page .toc = self .renderer .write_combined ()
@@ -325,38 +309,30 @@ def on_post_build(self, config, **kwargs):
325
309
326
310
# Remove lazy loading attributes from images
327
311
# https://regex101.com/r/HVpKPs/1
328
- html = re .sub (r"(\<img.+)(loading=\"lazy\")" , r"\1" , html )
312
+ html = re .sub (r"(\<img.+)(loading=\"lazy\")" , r"\1" , html )
329
313
330
314
# Compatiblity with mkdocs-chart-plugin
331
315
# As this plugin adds some javascript to every page
332
316
# It should be included in the print site also
333
317
if config .get ("plugins" , {}).get ("charts" ):
334
- html = (
335
- config .get ("plugins" , {})
336
- .get ("charts" )
337
- .add_javascript_variables (html , self .print_page , config )
338
- )
318
+ html = config .get ("plugins" , {}).get ("charts" ).add_javascript_variables (html , self .print_page , config )
339
319
340
320
# Compatibility with mkdocs-drawio
341
321
# As this plugin adds renderer html for every drawio diagram
342
322
# referenced in your markdown files. This rendering happens
343
323
# in the on_post_page event, which is skipped by this plugin
344
- # therefore we need to manual execute the drawio plugin renderer here.
324
+ # therefore we need to manual execute the drawio plugin renderer here.
345
325
if config .get ("plugins" , {}).get ("drawio" ):
346
- html = (
347
- config .get ("plugins" , {})
348
- .get ("drawio" )
349
- .render_drawio_diagrams (html , self .print_page )
350
- )
326
+ html = config .get ("plugins" , {}).get ("drawio" ).render_drawio_diagrams (html , self .print_page )
351
327
352
328
# Compatibility with https://github.com/g-provost/lightgallery-markdown
353
329
# This plugin insert link hrefs with double dashes, f.e.
354
330
# <link href="//assets/css/somecss.css">
355
331
# Details https://github.com/timvink/mkdocs-print-site-plugin/issues/68
356
332
htmls = html .split ("</head>" )
357
333
base_url = "../" if config .get ("use_directory_urls" ) else ""
358
- htmls [0 ] = htmls [0 ].replace (" href=\ " //" , f" href=\ "{ base_url } " )
359
- htmls [0 ] = htmls [0 ].replace (" src=\ " //" , f" src=\ "{ base_url } " )
334
+ htmls [0 ] = htmls [0 ].replace (' href="//' , f' href="{ base_url } ' )
335
+ htmls [0 ] = htmls [0 ].replace (' src="//' , f' src="{ base_url } ' )
360
336
html = "</head>" .join (htmls )
361
337
362
338
# Determine calls to required javascript functions
@@ -379,7 +355,4 @@ def on_post_build(self, config, **kwargs):
379
355
html = html .replace ("</head>" , print_site_js + "</head>" )
380
356
381
357
# Write the print_page file to the output folder
382
- write_file (
383
- html .encode ("utf-8" , errors = "xmlcharrefreplace" ),
384
- self .print_page .file .abs_dest_path
385
- )
358
+ write_file (html .encode ("utf-8" , errors = "xmlcharrefreplace" ), self .print_page .file .abs_dest_path )
0 commit comments