1
1
import codecs
2
- from typing import Union , List , Tuple , Dict , Any
2
+ from typing import Union
3
3
import os
4
4
import re
5
5
from glob import iglob
6
6
from pathlib import Path
7
7
from urllib .parse import unquote , quote
8
- import ast
9
8
import frontmatter
10
9
import markdown
11
10
from bs4 import BeautifulSoup
14
13
from mkdocs .plugins import BasePlugin
15
14
from mkdocs_callouts .plugin import CalloutsPlugin
16
15
from custom_attributes .plugin import convert_text_attributes
17
- from mkdocs .exceptions import PluginError
16
+ import logging
17
+
18
18
19
19
def search_in_file (citation_part : str , contents : str ) -> str :
20
20
"""
@@ -31,7 +31,8 @@ def search_in_file(citation_part: str, contents: str) -> str:
31
31
elif '#' in citation_part and not '^' in citation_part :
32
32
# cite from title
33
33
sub_section = []
34
- citation_part = citation_part .replace ('-' , ' ' ).replace ('#' , '# ' ).upper ()
34
+ citation_part = citation_part .replace (
35
+ '-' , ' ' ).replace ('#' , '# ' ).upper ()
35
36
heading = 0
36
37
37
38
for i in data :
@@ -52,7 +53,7 @@ def search_in_file(citation_part: str, contents: str) -> str:
52
53
# cite from block
53
54
citation_part = citation_part .replace ('#' , '' )
54
55
for i in data :
55
- if re .search (re .escape (citation_part ) + "$" , i ):
56
+ if re .search (re .escape (citation_part ) + '$' , i ):
56
57
return i .replace (citation_part , '' )
57
58
return ''
58
59
@@ -66,8 +67,12 @@ def mini_ez_links(urlo, base, end, url_whitespace, url_case):
66
67
internal_link = str (internal_link ).replace (base , '' )
67
68
else : # fallback to searching
68
69
if urlo [2 ].endswith ('.md' ):
69
- internal_link = str (search_file_in_documentation (Path (urlo [2 ]).resolve (), Path (md_link_path ).parent ))
70
- if not os .path .isfile (internal_link ): # manual search
70
+ internal_link = str (
71
+ search_file_in_documentation (
72
+ Path (
73
+ urlo [2 ]).resolve (),
74
+ Path (md_link_path ).parent ))
75
+ if not os .path .isfile (internal_link ): # manual search
71
76
file_name = urlo [2 ].replace ('index' , '' )
72
77
file_name = file_name .replace ('../' , '' )
73
78
file_name = file_name .replace ('./' , '' )
@@ -91,22 +96,25 @@ def mini_ez_links(urlo, base, end, url_whitespace, url_case):
91
96
url = re .sub (r'\/$' , '' , str (url_blog )) + '/' + quote (url )
92
97
if not url .startswith ('http' ):
93
98
url = 'https://' + url
94
- if not url .endswith ('/' ) and not url .endswith (('png' , 'jpg' , 'jpeg' , 'gif' , 'webm' )):
99
+ if not url .endswith ('/' ) and not url .endswith (('png' ,
100
+ 'jpg' , 'jpeg' , 'gif' , 'webm' )):
95
101
url = url + '/'
96
102
return url
97
103
104
+
98
105
def strip_comments (markdown ):
99
106
file_content = markdown .split ('\n ' )
100
107
markdown = ''
101
108
for line in file_content :
102
- if not re .search (r'%%(.*)%%' , line ) or not line .startswith ('%%' ) or not line .endswith ('%%' ):
109
+ if not re .search (
110
+ r'%%(.*)%%' , line ) or not line .startswith ('%%' ) or not line .endswith ('%%' ):
103
111
markdown += line + '\n '
104
112
markdown = re .sub (r'%%(.*)%%' , '' , markdown , flags = re .DOTALL )
105
113
return markdown
106
114
107
115
108
-
109
- def cite ( md_link_path , link , soup , citation_part , config , callouts , custom_attr , msg ) -> BeautifulSoup :
116
+ def cite ( md_link_path , link , soup , citation_part , config ,
117
+ callouts , custom_attr , msg ) -> BeautifulSoup :
110
118
"""Append the content of the founded file to the original file.
111
119
112
120
Args:
@@ -131,19 +139,19 @@ def cite(md_link_path, link, soup, citation_part, config, callouts, custom_attr,
131
139
new_uri = new_uri .replace ('\\ ' , '/' )
132
140
new_uri = new_uri .replace ('.md' , '/' )
133
141
new_uri = new_uri .replace ('//' , '/' )
134
- new_uri = re .sub ('https?:\/' , '\g<0>/' , new_uri )
142
+ new_uri = re .sub ('https?:\\ /' , '\ \ g<0>/' , new_uri )
135
143
new_uri = new_uri .replace ('/index/' , '/' )
136
144
input_file = codecs .open (str (md_link_path ), mode = 'r' , encoding = 'utf-8' )
137
145
text = input_file .read ()
138
146
139
147
contents = frontmatter .loads (text ).content
140
148
quote = search_in_file (citation_part , contents )
141
149
tooltip_template = (
142
- "<div class='citation'> <a href='"
143
- + str (link ['src' ])
144
- + "' class='link_citation'><i class='fas fa-link'></i> </a>"
145
- + str (link ['alt' ]) + ' not exists.'
146
- + '</div>'
150
+ "<div class='citation'> <a href='"
151
+ + str (link ['src' ])
152
+ + "' class='link_citation'><i class='fas fa-link'></i> </a>"
153
+ + str (link ['alt' ]) + ' not exists.'
154
+ + '</div>'
147
155
)
148
156
if len (quote ) > 0 :
149
157
if callouts :
@@ -156,6 +164,8 @@ def cite(md_link_path, link, soup, citation_part, config, callouts, custom_attr,
156
164
quote = convert_text_attributes (quote , config_attr )
157
165
quote = strip_comments (quote )
158
166
md_extensions = config ['markdown_extensions' ]
167
+ wiki = WikiLinkPlusExtension (md_config ['mdx_wikilink_plus' ])
168
+ md_extensions .append (wiki )
159
169
html = markdown .markdown (
160
170
quote ,
161
171
extensions = md_extensions ,
@@ -177,30 +187,44 @@ def cite(md_link_path, link, soup, citation_part, config, callouts, custom_attr,
177
187
return soup
178
188
else :
179
189
log = logging .getLogger ('mkdocs.plugins.' + __name__ )
190
+ log .info ('[EMBED FILE PLUGIN] CITATION NOT FOUND : ' +
191
+ unquote (citation_part ) +
192
+ 'for : ' +
193
+ str (md_link_path ) +
194
+ ' with link: ' +
195
+ str (link ) +
196
+ ' and new_uri: ' +
197
+ str (new_uri ) +
198
+ ' and quote: ' +
199
+ str (quote ))
180
200
return tooltip_not_found (link , soup , msg )
181
201
182
202
183
203
def tooltip_not_found (link , soup , msg ) -> BeautifulSoup :
184
204
tooltip_template = (
185
- "<div class='citation'> <a class='link_citation'><i class='fas fa-link'></i> </a>"
186
- + '<p style="text-align: center; display: block"><i class="not_found">' + str (link ['alt' ]) + f'</i> { msg } </p>'
187
- + '</div>'
205
+ "<div class='citation'> <a class='link_citation'><i class='fas fa-link'></i> </a>"
206
+ + '<p style="text-align: center; display: block"><i class="not_found">' +
207
+ str (link ['alt' ]) + f'</i> { msg } </p>'
208
+ + '</div>'
188
209
)
189
210
new_soup = str (soup ).replace (str (link ), str (tooltip_template ))
190
211
soup = BeautifulSoup (new_soup , 'html.parser' )
191
212
return soup
192
213
214
+
193
215
def create_link (link ):
194
216
if link .endswith ('/' ):
195
217
return link [:- 1 ] + '.md'
196
218
else :
197
219
return link + '.md'
198
220
199
- def search_file_in_documentation (link : Union [Path ,str ], config_dir : Path ) -> Union [Path , int ]:
221
+
222
+ def search_file_in_documentation (
223
+ link : Union [Path , str ], config_dir : Path ) -> Union [Path , int ]:
200
224
file_name = os .path .basename (link )
201
225
if not file_name .endswith ('.md' ):
202
226
file_name = file_name + '.md'
203
- for p in config_dir .rglob (f" *{ file_name } " ):
227
+ for p in config_dir .rglob (f' *{ file_name } ' ):
204
228
return p
205
229
return 0
206
230
@@ -224,12 +248,12 @@ def on_post_page(self, output_content, page, config) -> str:
224
248
language_message = self .config ['language_message' ]
225
249
for link in soup .findAll (
226
250
'img' ,
227
- src = lambda src : src is not None
228
- and 'favicon' not in src
229
- and not src .endswith (('png' , 'jpg' , 'jpeg' , 'gif' , 'svg' ))
230
- and not 'www' in src
231
- and not 'http' in src
232
- and not '://' in src ,
251
+ src = lambda src : src is not None
252
+ and 'favicon' not in src
253
+ and not src .endswith (('png' , 'jpg' , 'jpeg' , 'gif' , 'svg' ))
254
+ and not 'www' in src
255
+ and not 'http' in src
256
+ and not '://' in src ,
233
257
):
234
258
if len (link ['src' ]) > 0 :
235
259
if link ['src' ].startswith ('./' ):
@@ -238,9 +262,11 @@ def on_post_page(self, output_content, page, config) -> str:
238
262
md_src = create_link (unquote (link ['src' ]))
239
263
md_link_path = Path (
240
264
os .path .dirname (page .file .abs_src_path ), md_src ).resolve ()
241
- md_link_path = re .sub (r'[\/\\]?#(.*)$' , '' , str (md_link_path ))
265
+ md_link_path = re .sub (
266
+ r'[\/\\]?#(.*)$' , '' , str (md_link_path ))
242
267
if not os .path .isfile (md_link_path ):
243
- md_link_path = search_file_in_documentation (md_link_path , docs )
268
+ md_link_path = search_file_in_documentation (
269
+ md_link_path , docs )
244
270
245
271
elif link ['src' ][0 ] == '/' :
246
272
md_src_path = create_link (unquote (link ['src' ]))
@@ -255,7 +281,8 @@ def on_post_page(self, output_content, page, config) -> str:
255
281
md_link_path = os .path .join (
256
282
os .path .dirname (page .file .abs_src_path ), md_src_path
257
283
)
258
- md_link_path = re .sub (r'/#(.*).md$' , '.md' , str (md_link_path ))
284
+ md_link_path = re .sub (
285
+ r'/#(.*).md$' , '.md' , str (md_link_path ))
259
286
md_link_path = Path (unquote (md_link_path )).resolve ()
260
287
261
288
else :
@@ -266,7 +293,8 @@ def on_post_page(self, output_content, page, config) -> str:
266
293
md_link_path = Path (unquote (md_link_path )).resolve ()
267
294
if (md_link_path == 0 ):
268
295
soup = tooltip_not_found (link , soup , language_message )
269
- if (md_link_path != '' or md_link_path == 0 ) and len (link ['src' ]) > 0 :
296
+ if (md_link_path != '' or md_link_path ==
297
+ 0 ) and len (link ['src' ]) > 0 :
270
298
if '#' in link .get ('alt' , '' ):
271
299
# heading
272
300
citation_part = re .sub ('^(.*)#' , '#' , link ['alt' ])
@@ -281,7 +309,8 @@ def on_post_page(self, output_content, page, config) -> str:
281
309
soup = cite (md_link_path , link , soup ,
282
310
citation_part , config , callout , self .config ['custom-attributes' ], language_message )
283
311
else :
284
- link_found = search_file_in_documentation (md_link_path , docs )
312
+ link_found = search_file_in_documentation (
313
+ md_link_path , docs )
285
314
if link_found != 0 :
286
315
soup = cite (link_found , link , soup ,
287
316
citation_part , config , callout , self .config ['custom-attributes' ], language_message )
0 commit comments