1
1
import codecs
2
- import os
2
+ import logging
3
3
import re
4
4
from pathlib import Path
5
5
from urllib .parse import unquote
6
+
6
7
import frontmatter
7
8
import markdown
8
9
from bs4 import BeautifulSoup
10
+ from custom_attributes .plugin import convert_text_attributes
9
11
from mkdocs .config import config_options
10
12
from mkdocs .plugins import BasePlugin
11
13
from mkdocs_callouts .plugin import CalloutsPlugin
12
- from custom_attributes .plugin import convert_text_attributes
13
- import logging
14
14
15
15
from mkdocs_embed_file_plugins .src .links_correction import (
16
16
MULTIMEDIA_EXTENSIONS ,
27
27
strip_comments ,
28
28
)
29
29
30
+ DEFAULT_MARKDOWN_EXTENSIONS_CONFIG = {
31
+ "pymdownx.arithmatex" : {"generic" : True },
32
+ "pymdownx.highlight" : {
33
+ "use_pygments" : True ,
34
+ "linenums" : True ,
35
+ },
36
+ "pymdownx.tasklist" : {"custom_checkbox" : True },
37
+ }
38
+
30
39
31
40
def cite (
32
- md_link_path , link , soup , citation_part , config , callouts , custom_attr , msg
41
+ md_link_path ,
42
+ link ,
43
+ soup ,
44
+ citation_part ,
45
+ config ,
46
+ callouts ,
47
+ custom_attr ,
48
+ msg ,
49
+ md_config ,
33
50
) -> BeautifulSoup :
34
51
"""Append the content of the founded file to the original file.
35
52
36
53
Args:
37
- md_link_path (str): Path of the file to be modified.
54
+ md_link_path (str|Path ): Path of the file to be modified.
38
55
link (str): Link to the file to be included.
39
56
soup (BeautifulSoup): BeautifulSoup object of the file to be modified.
40
57
citation_part (str): Part of the link to be included.
41
- config (dict): Configuration of the plugin.
58
+ config (dict|MkdocsConfig ): Configuration of the plugin.
42
59
callouts (CalloutsPlugin): Callouts plugin.
43
60
custom_attr (CustomAttributesPlugin): Custom attributes plugin.
44
61
msg (str): Message to be displayed if the file is not found.
62
+ md_config (dict): Configuration of the markdown extensions.
45
63
Returns: updated HTML
46
64
"""
65
+ log = logging .getLogger ("mkdocs.plugins." + __name__ )
47
66
docs = config ["docs_dir" ]
48
67
url = config ["site_url" ]
49
-
50
- md_config = {
68
+ mdx_wikilink_plus = {
51
69
"mdx_wikilink_plus" : {
52
70
"base_url" : (docs , url , md_link_path ),
53
71
"build_url" : mini_ez_links ,
54
72
"image_class" : "wikilink" ,
55
- }
73
+ },
56
74
}
75
+ md_config .update (mdx_wikilink_plus )
57
76
new_uri = str (md_link_path ).replace (str (docs ), str (url ))
58
77
new_uri = new_uri .replace ("\\ " , "/" )
59
78
new_uri = new_uri .replace (".md" , "/" )
@@ -83,9 +102,13 @@ def cite(
83
102
quote = strip_comments (quote )
84
103
md_extensions = config ["markdown_extensions" ]
85
104
md_extensions .append ("mdx_wikilink_plus" )
105
+ md_extensions = list (dict .fromkeys (md_extensions ))
106
+ # remove arithmatex from the list of extensions
107
+
86
108
html = markdown .markdown (
87
109
quote , extensions = md_extensions , extension_configs = md_config
88
110
)
111
+
89
112
link_soup = BeautifulSoup (html , "html.parser" )
90
113
if link_soup :
91
114
tooltip_template = (
@@ -101,8 +124,7 @@ def cite(
101
124
soup = BeautifulSoup (new_soup , "html.parser" )
102
125
return soup
103
126
else :
104
- log = logging .getLogger ("mkdocs.plugins." + __name__ )
105
- log .info (
127
+ log .warning (
106
128
"[EMBED FILE PLUGIN] CITATION NOT FOUND : "
107
129
+ unquote (citation_part )
108
130
+ "for : "
@@ -141,9 +163,20 @@ def __init__(self):
141
163
self .enabled = True
142
164
self .total_time = 0
143
165
166
+ def create_config (self , config ):
167
+ md_config = {}
168
+ mdx_config = config .get ("mdx_configs" )
169
+ if mdx_config :
170
+ for key , value in mdx_config .items ():
171
+ md_config [key ] = value
172
+ else :
173
+ md_config .update (DEFAULT_MARKDOWN_EXTENSIONS_CONFIG )
174
+ return md_config
175
+
144
176
def on_post_page (self , output_content , page , config ) -> str :
145
177
soup = BeautifulSoup (output_content , "html.parser" )
146
178
docs = Path (config ["docs_dir" ])
179
+ md_config = self .create_config (config )
147
180
md_link_path = ""
148
181
callout = self .config ["callouts" ]
149
182
language_message = self .config ["language_message" ]
@@ -162,32 +195,30 @@ def on_post_page(self, output_content, page, config) -> str:
162
195
elif link ["src" ][0 ] == "." : # relative links
163
196
md_src = create_link (unquote (link ["src" ]))
164
197
md_link_path = Path (
165
- os . path . dirname (page .file .abs_src_path ), md_src
198
+ Path (page .file .abs_src_path ). parent , md_src
166
199
).resolve ()
167
200
md_link_path = re .sub (r"[\/\\]?#(.*)$" , "" , str (md_link_path ))
168
- if not os . path . isfile (md_link_path ):
201
+ if not Path (md_link_path ). is_file ( ):
169
202
md_link_path = search_file_in_documentation (
170
203
md_link_path , docs , docs
171
204
)
172
205
173
206
elif link ["src" ][0 ] == "/" :
174
207
md_src_path = create_link (unquote (link ["src" ]))
175
- md_link_path = os . path . join (config ["docs_dir" ], md_src_path )
208
+ md_link_path = Path (config ["docs_dir" ]) / md_src_path
176
209
md_link_path = Path (unquote (md_link_path )).resolve ()
177
210
178
211
elif link ["src" ][0 ] != "#" :
179
212
md_src_path = create_link (unquote (link ["src" ]))
180
- md_link_path = os .path .join (
181
- os .path .dirname (page .file .abs_src_path ), md_src_path
182
- )
213
+ md_link_path = Path (page .file .abs_src_path ).parent / md_src_path
214
+
183
215
md_link_path = re .sub (r"/#(.*).md$" , ".md" , str (md_link_path ))
184
216
md_link_path = Path (unquote (md_link_path )).resolve ()
185
217
186
218
else :
187
219
md_src_path = create_link (unquote (link ["src" ]))
188
- md_link_path = os .path .join (
189
- os .path .dirname (page .file .abs_src_path ), md_src_path
190
- )
220
+ md_link_path = Path (page .file .abs_src_path ).parent / md_src_path
221
+
191
222
md_link_path = Path (unquote (md_link_path )).resolve ()
192
223
if md_link_path == 0 :
193
224
soup = tooltip_not_found (link , soup , language_message )
@@ -202,7 +233,7 @@ def on_post_page(self, output_content, page, config) -> str:
202
233
if citation_part :
203
234
md_link_path = Path (str (md_link_path ))
204
235
205
- if os . path . isfile (md_link_path ):
236
+ if Path (md_link_path ). is_file ( ):
206
237
soup = cite (
207
238
md_link_path ,
208
239
link ,
@@ -212,6 +243,7 @@ def on_post_page(self, output_content, page, config) -> str:
212
243
callout ,
213
244
self .config ["custom-attributes" ],
214
245
language_message ,
246
+ md_config ,
215
247
)
216
248
else :
217
249
link_found = search_file_in_documentation (
@@ -227,5 +259,6 @@ def on_post_page(self, output_content, page, config) -> str:
227
259
callout ,
228
260
self .config ["custom-attributes" ],
229
261
language_message ,
262
+ md_config ,
230
263
)
231
264
return add_not_found_class (str (soup ))
0 commit comments