Skip to content

Commit b38dcc1

Browse files
committed
style: formating
1 parent 44f609e commit b38dcc1

File tree

1 file changed

+62
-33
lines changed

1 file changed

+62
-33
lines changed

mkdocs_embed_file_plugins/plugin.py

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import codecs
2-
from typing import Union, List, Tuple, Dict, Any
2+
from typing import Union
33
import os
44
import re
55
from glob import iglob
66
from pathlib import Path
77
from urllib.parse import unquote, quote
8-
import ast
98
import frontmatter
109
import markdown
1110
from bs4 import BeautifulSoup
@@ -14,7 +13,8 @@
1413
from mkdocs.plugins import BasePlugin
1514
from mkdocs_callouts.plugin import CalloutsPlugin
1615
from custom_attributes.plugin import convert_text_attributes
17-
from mkdocs.exceptions import PluginError
16+
import logging
17+
1818

1919
def search_in_file(citation_part: str, contents: str) -> str:
2020
"""
@@ -31,7 +31,8 @@ def search_in_file(citation_part: str, contents: str) -> str:
3131
elif '#' in citation_part and not '^' in citation_part:
3232
# cite from title
3333
sub_section = []
34-
citation_part = citation_part.replace('-', ' ').replace('#', '# ').upper()
34+
citation_part = citation_part.replace(
35+
'-', ' ').replace('#', '# ').upper()
3536
heading = 0
3637

3738
for i in data:
@@ -52,7 +53,7 @@ def search_in_file(citation_part: str, contents: str) -> str:
5253
# cite from block
5354
citation_part = citation_part.replace('#', '')
5455
for i in data:
55-
if re.search(re.escape(citation_part) + "$", i):
56+
if re.search(re.escape(citation_part) + '$', i):
5657
return i.replace(citation_part, '')
5758
return ''
5859

@@ -66,8 +67,12 @@ def mini_ez_links(urlo, base, end, url_whitespace, url_case):
6667
internal_link = str(internal_link).replace(base, '')
6768
else: # fallback to searching
6869
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
7176
file_name = urlo[2].replace('index', '')
7277
file_name = file_name.replace('../', '')
7378
file_name = file_name.replace('./', '')
@@ -91,22 +96,25 @@ def mini_ez_links(urlo, base, end, url_whitespace, url_case):
9196
url = re.sub(r'\/$', '', str(url_blog)) + '/' + quote(url)
9297
if not url.startswith('http'):
9398
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')):
95101
url = url + '/'
96102
return url
97103

104+
98105
def strip_comments(markdown):
99106
file_content = markdown.split('\n')
100107
markdown = ''
101108
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('%%'):
103111
markdown += line + '\n'
104112
markdown = re.sub(r'%%(.*)%%', '', markdown, flags=re.DOTALL)
105113
return markdown
106114

107115

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:
110118
"""Append the content of the founded file to the original file.
111119
112120
Args:
@@ -131,19 +139,19 @@ def cite(md_link_path, link, soup, citation_part, config, callouts, custom_attr,
131139
new_uri = new_uri.replace('\\', '/')
132140
new_uri = new_uri.replace('.md', '/')
133141
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)
135143
new_uri = new_uri.replace('/index/', '/')
136144
input_file = codecs.open(str(md_link_path), mode='r', encoding='utf-8')
137145
text = input_file.read()
138146

139147
contents = frontmatter.loads(text).content
140148
quote = search_in_file(citation_part, contents)
141149
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>'
147155
)
148156
if len(quote) > 0:
149157
if callouts:
@@ -156,6 +164,8 @@ def cite(md_link_path, link, soup, citation_part, config, callouts, custom_attr,
156164
quote = convert_text_attributes(quote, config_attr)
157165
quote = strip_comments(quote)
158166
md_extensions = config['markdown_extensions']
167+
wiki = WikiLinkPlusExtension(md_config['mdx_wikilink_plus'])
168+
md_extensions.append(wiki)
159169
html = markdown.markdown(
160170
quote,
161171
extensions=md_extensions,
@@ -177,30 +187,44 @@ def cite(md_link_path, link, soup, citation_part, config, callouts, custom_attr,
177187
return soup
178188
else:
179189
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))
180200
return tooltip_not_found(link, soup, msg)
181201

182202

183203
def tooltip_not_found(link, soup, msg) -> BeautifulSoup:
184204
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>'
188209
)
189210
new_soup = str(soup).replace(str(link), str(tooltip_template))
190211
soup = BeautifulSoup(new_soup, 'html.parser')
191212
return soup
192213

214+
193215
def create_link(link):
194216
if link.endswith('/'):
195217
return link[:-1] + '.md'
196218
else:
197219
return link + '.md'
198220

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]:
200224
file_name = os.path.basename(link)
201225
if not file_name.endswith('.md'):
202226
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}'):
204228
return p
205229
return 0
206230

@@ -224,12 +248,12 @@ def on_post_page(self, output_content, page, config) -> str:
224248
language_message = self.config['language_message']
225249
for link in soup.findAll(
226250
'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,
233257
):
234258
if len(link['src']) > 0:
235259
if link['src'].startswith('./'):
@@ -238,9 +262,11 @@ def on_post_page(self, output_content, page, config) -> str:
238262
md_src = create_link(unquote(link['src']))
239263
md_link_path = Path(
240264
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))
242267
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)
244270

245271
elif link['src'][0] == '/':
246272
md_src_path = create_link(unquote(link['src']))
@@ -255,7 +281,8 @@ def on_post_page(self, output_content, page, config) -> str:
255281
md_link_path = os.path.join(
256282
os.path.dirname(page.file.abs_src_path), md_src_path
257283
)
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))
259286
md_link_path = Path(unquote(md_link_path)).resolve()
260287

261288
else:
@@ -266,7 +293,8 @@ def on_post_page(self, output_content, page, config) -> str:
266293
md_link_path = Path(unquote(md_link_path)).resolve()
267294
if (md_link_path == 0):
268295
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:
270298
if '#' in link.get('alt', ''):
271299
# heading
272300
citation_part = re.sub('^(.*)#', '#', link['alt'])
@@ -281,7 +309,8 @@ def on_post_page(self, output_content, page, config) -> str:
281309
soup = cite(md_link_path, link, soup,
282310
citation_part, config, callout, self.config['custom-attributes'], language_message)
283311
else:
284-
link_found=search_file_in_documentation(md_link_path, docs)
312+
link_found = search_file_in_documentation(
313+
md_link_path, docs)
285314
if link_found != 0:
286315
soup = cite(link_found, link, soup,
287316
citation_part, config, callout, self.config['custom-attributes'], language_message)

0 commit comments

Comments
 (0)