Skip to content

Commit debcfea

Browse files
committed
fix: some path was wrong
1 parent c27386c commit debcfea

File tree

3 files changed

+217
-192
lines changed

3 files changed

+217
-192
lines changed

mkdocs_embed_file_plugins/plugin.py

Lines changed: 140 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,26 @@
66
import frontmatter
77
import markdown
88
from bs4 import BeautifulSoup
9-
from mdx_wikilink_plus.mdx_wikilink_plus import WikiLinkPlusExtension
109
from mkdocs.config import config_options
1110
from mkdocs.plugins import BasePlugin
1211
from mkdocs_callouts.plugin import CalloutsPlugin
1312
from custom_attributes.plugin import convert_text_attributes
1413
import logging
1514

16-
from mkdocs_embed_file_plugins.src.links_correction import convert_links_if_markdown, mini_ez_links
17-
from mkdocs_embed_file_plugins.src.search_quote import search_file_in_documentation, search_in_file
15+
from mkdocs_embed_file_plugins.src.links_correction import (
16+
convert_links_if_markdown,
17+
mini_ez_links,
18+
)
19+
from mkdocs_embed_file_plugins.src.search_quote import (
20+
search_file_in_documentation,
21+
search_in_file,
22+
)
1823
from mkdocs_embed_file_plugins.src.utils import create_link, strip_comments
1924

2025

2126
def cite(
22-
md_link_path, link, soup, citation_part, config,
23-
callouts, custom_attr, msg
24-
) -> BeautifulSoup :
27+
md_link_path, link, soup, citation_part, config, callouts, custom_attr, msg
28+
) -> BeautifulSoup:
2529
"""Append the content of the founded file to the original file.
2630
2731
Args:
@@ -35,186 +39,188 @@ def cite(
3539
msg (str): Message to be displayed if the file is not found.
3640
Returns: updated HTML
3741
"""
38-
docs = config['docs_dir']
39-
url = config['site_url']
42+
docs = config["docs_dir"]
43+
url = config["site_url"]
4044

4145
md_config = {
42-
'mdx_wikilink_plus' : {
43-
'base_url' : (docs, url, md_link_path),
44-
'build_url' : mini_ez_links,
45-
'image_class' : 'wikilink',
46-
}
46+
"mdx_wikilink_plus": {
47+
"base_url": (docs, url, md_link_path),
48+
"build_url": mini_ez_links,
49+
"image_class": "wikilink",
4750
}
51+
}
4852
new_uri = str(md_link_path).replace(str(docs), str(url))
49-
new_uri = new_uri.replace('\\', '/')
50-
new_uri = new_uri.replace('.md', '/')
51-
new_uri = new_uri.replace('//', '/')
52-
new_uri = re.sub('https?:\\/', '\\g<0>/', new_uri)
53-
new_uri = new_uri.replace('/index/', '/')
54-
input_file = codecs.open(str(md_link_path), mode = 'r', encoding = 'utf-8')
53+
new_uri = new_uri.replace("\\", "/")
54+
new_uri = new_uri.replace(".md", "/")
55+
new_uri = new_uri.replace("//", "/")
56+
new_uri = re.sub("https?:\\/", "\\g<0>/", new_uri)
57+
new_uri = new_uri.replace("/index/", "/")
58+
input_file = codecs.open(str(md_link_path), mode="r", encoding="utf-8")
5559
text = input_file.read()
5660

5761
contents = frontmatter.loads(text).content
5862
quote = search_in_file(citation_part, contents)
5963
tooltip_template = (
60-
"<div class='citation'> <a href='"
61-
+ str(link['src'])
62-
+ "' class='link_citation'><i class='fas fa-link'></i> </a>"
63-
+ str(link['alt']) + ' not exists.'
64-
+ '</div>'
64+
"<div class='citation'> <a href='"
65+
+ str(link["src"])
66+
+ "' class='link_citation'><i class='fas fa-link'></i> </a>"
67+
+ str(link["alt"])
68+
+ " not exists."
69+
+ "</div>"
6570
)
66-
if len(quote) > 0 :
67-
if callouts :
71+
if len(quote) > 0:
72+
if callouts:
6873
quote = CalloutsPlugin().on_page_markdown(quote, None, None, None)
69-
if len(custom_attr) > 0 :
70-
config_attr = {
71-
'file' : custom_attr,
72-
'docs_dir' : docs
73-
}
74+
if len(custom_attr) > 0:
75+
config_attr = {"file": custom_attr, "docs_dir": docs}
7476
quote = convert_text_attributes(quote, config_attr)
7577
quote = convert_links_if_markdown(quote, (docs, url, md_link_path))
7678
quote = strip_comments(quote)
77-
md_extensions = config['markdown_extensions']
78-
md_extensions.append('mdx_wikilink_plus')
79+
md_extensions = config["markdown_extensions"]
80+
md_extensions.append("mdx_wikilink_plus")
7981
html = markdown.markdown(
80-
quote,
81-
extensions = md_extensions,
82-
extension_configs = md_config
83-
)
84-
link_soup = BeautifulSoup(html, 'html.parser')
85-
if link_soup :
82+
quote, extensions=md_extensions, extension_configs=md_config
83+
)
84+
link_soup = BeautifulSoup(html, "html.parser")
85+
if link_soup:
8686
tooltip_template = (
87-
"<div class='citation'> <a href='"
88-
+ str(new_uri)
89-
+ "' class='link_citation'><i class='fas fa-link'></i> </a>"
90-
+ str(link_soup).replace(
87+
"<div class='citation'> <a href='"
88+
+ str(new_uri)
89+
+ "' class='link_citation'><i class='fas fa-link'></i> </a>"
90+
+ str(link_soup).replace(
9191
'!<img class="wikilink', '<img class="wikilink'
92-
)
93-
+ '</div>'
92+
)
93+
+ "</div>"
9494
)
9595
new_soup = str(soup).replace(str(link), str(tooltip_template))
96-
soup = BeautifulSoup(new_soup, 'html.parser')
96+
soup = BeautifulSoup(new_soup, "html.parser")
9797
return soup
98-
else :
99-
log = logging.getLogger('mkdocs.plugins.' + __name__)
98+
else:
99+
log = logging.getLogger("mkdocs.plugins." + __name__)
100100
log.info(
101-
'[EMBED FILE PLUGIN] CITATION NOT FOUND : ' +
102-
unquote(citation_part) +
103-
'for : ' +
104-
str(md_link_path) +
105-
' with link: ' +
106-
str(link) +
107-
' and new_uri: ' +
108-
str(new_uri) +
109-
' and quote: ' +
110-
str(quote)
111-
)
101+
"[EMBED FILE PLUGIN] CITATION NOT FOUND : "
102+
+ unquote(citation_part)
103+
+ "for : "
104+
+ str(md_link_path)
105+
+ " with link: "
106+
+ str(link)
107+
+ " and new_uri: "
108+
+ str(new_uri)
109+
+ " and quote: "
110+
+ str(quote)
111+
)
112112
return tooltip_not_found(link, soup, msg)
113113

114114

115-
def tooltip_not_found(link, soup, msg) -> BeautifulSoup :
115+
def tooltip_not_found(link, soup, msg) -> BeautifulSoup:
116116
tooltip_template = (
117-
"<div class='citation'> <a class='link_citation'><i class='fas fa-link'></i> </a>"
118-
+ '<p style="text-align: center; display: block"><i class="not_found">' +
119-
str(link['alt']) + f'</i> {msg}</p>'
120-
+ '</div>'
117+
"<div class='citation'> <a class='link_citation'><i class='fas fa-link'></i> </a>"
118+
+ '<p style="text-align: center; display: block"><i class="not_found">'
119+
+ str(link["alt"])
120+
+ f"</i> {msg}</p>"
121+
+ "</div>"
121122
)
122123
new_soup = str(soup).replace(str(link), str(tooltip_template))
123-
soup = BeautifulSoup(new_soup, 'html.parser')
124+
soup = BeautifulSoup(new_soup, "html.parser")
124125
return soup
125126

126127

127-
class EmbedFile(BasePlugin) :
128+
class EmbedFile(BasePlugin):
128129
config_scheme = (
129-
('callouts', config_options.Type(bool, default = False)),
130-
('custom-attributes', config_options.Type(str, default = '')),
131-
('language_message', config_options.Type(str, default = 'file not exists')),
132-
)
130+
("callouts", config_options.Type(bool, default=False)),
131+
("custom-attributes", config_options.Type(str, default="")),
132+
("language_message", config_options.Type(str, default="file not exists")),
133+
)
133134

134-
def __init__(self) :
135+
def __init__(self):
135136
self.enabled = True
136137
self.total_time = 0
137138

138-
def on_post_page(self, output_content, page, config) -> str :
139-
soup = BeautifulSoup(output_content, 'html.parser')
140-
docs = Path(config['docs_dir'])
141-
md_link_path = ''
142-
callout = self.config['callouts']
143-
language_message = self.config['language_message']
139+
def on_post_page(self, output_content, page, config) -> str:
140+
soup = BeautifulSoup(output_content, "html.parser")
141+
docs = Path(config["docs_dir"])
142+
md_link_path = ""
143+
callout = self.config["callouts"]
144+
language_message = self.config["language_message"]
144145
for link in soup.findAll(
145-
'img',
146-
src = lambda src : src is not None
147-
and 'favicon' not in src
148-
and not src.endswith(('png', 'jpg', 'jpeg', 'gif', 'svg'))
149-
and not 'www' in src
150-
and not 'http' in src
151-
and not '://' in src,
152-
) :
153-
if len(link['src']) > 0 :
154-
if link['src'].startswith('./') :
146+
"img",
147+
src=lambda src: src is not None
148+
and "favicon" not in src
149+
and not src.endswith(("png", "jpg", "jpeg", "gif", "svg"))
150+
and "www" not in src
151+
and "http" not in src
152+
and "://" not in src,
153+
):
154+
if len(link["src"]) > 0:
155+
if link["src"].startswith("./"):
155156
md_link_path = page.file.abs_src_path
156-
elif link['src'][0] == '.' : # relative links
157-
md_src = create_link(unquote(link['src']))
157+
elif link["src"][0] == ".": # relative links
158+
md_src = create_link(unquote(link["src"]))
158159
md_link_path = Path(
159-
os.path.dirname(page.file.abs_src_path), md_src
160-
).resolve()
161-
md_link_path = re.sub(
162-
r'[\/\\]?#(.*)$', '', str(md_link_path)
163-
)
164-
if not os.path.isfile(md_link_path) :
160+
os.path.dirname(page.file.abs_src_path), md_src
161+
).resolve()
162+
md_link_path = re.sub(r"[\/\\]?#(.*)$", "", str(md_link_path))
163+
if not os.path.isfile(md_link_path):
165164
md_link_path = search_file_in_documentation(
166-
md_link_path, docs
167-
)
165+
md_link_path, docs, docs
166+
)
168167

169-
elif link['src'][0] == '/' :
170-
md_src_path = create_link(unquote(link['src']))
171-
md_link_path = os.path.join(
172-
config['docs_dir'], md_src_path
173-
)
168+
elif link["src"][0] == "/":
169+
md_src_path = create_link(unquote(link["src"]))
170+
md_link_path = os.path.join(config["docs_dir"], md_src_path)
174171
md_link_path = Path(unquote(md_link_path)).resolve()
175172

176-
elif link['src'][0] != '#':
177-
md_src_path = create_link(unquote(link['src']))
173+
elif link["src"][0] != "#":
174+
md_src_path = create_link(unquote(link["src"]))
178175
md_link_path = os.path.join(
179-
os.path.dirname(page.file.abs_src_path), md_src_path
180-
)
181-
md_link_path = re.sub(
182-
r'/#(.*).md$', '.md', str(md_link_path)
183-
)
176+
os.path.dirname(page.file.abs_src_path), md_src_path
177+
)
178+
md_link_path = re.sub(r"/#(.*).md$", ".md", str(md_link_path))
184179
md_link_path = Path(unquote(md_link_path)).resolve()
185180

186-
else :
187-
md_src_path = create_link(unquote(link['src']))
181+
else:
182+
md_src_path = create_link(unquote(link["src"]))
188183
md_link_path = os.path.join(
189-
os.path.dirname(page.file.abs_src_path), md_src_path
190-
)
184+
os.path.dirname(page.file.abs_src_path), md_src_path
185+
)
191186
md_link_path = Path(unquote(md_link_path)).resolve()
192-
if md_link_path == 0 :
187+
if md_link_path == 0:
193188
soup = tooltip_not_found(link, soup, language_message)
194-
if (md_link_path != '' or md_link_path ==
195-
0) and len(link['src']) > 0 :
196-
if '#' in link.get('alt', '') :
189+
if (md_link_path != "" or md_link_path == 0) and len(link["src"]) > 0:
190+
if "#" in link.get("alt", ""):
197191
# heading
198-
citation_part = re.sub('^(.*)#', '#', link['alt'])
199-
elif '#' in link.get('src', '') :
200-
citation_part = re.sub('^(.*)#', '#', unquote(link['src']))
201-
else :
202-
citation_part = link.get('alt', False)
203-
if citation_part :
192+
citation_part = re.sub("^(.*)#", "#", link["alt"])
193+
elif "#" in link.get("src", ""):
194+
citation_part = re.sub("^(.*)#", "#", unquote(link["src"]))
195+
else:
196+
citation_part = link.get("alt", False)
197+
if citation_part:
204198
md_link_path = Path(str(md_link_path))
205199

206-
if os.path.isfile(md_link_path) :
200+
if os.path.isfile(md_link_path):
207201
soup = cite(
208-
md_link_path, link, soup,
209-
citation_part, config, callout, self.config['custom-attributes'], language_message
210-
)
211-
else :
202+
md_link_path,
203+
link,
204+
soup,
205+
citation_part,
206+
config,
207+
callout,
208+
self.config["custom-attributes"],
209+
language_message,
210+
)
211+
else:
212212
link_found = search_file_in_documentation(
213-
md_link_path, docs
214-
)
215-
if link_found != 0 :
213+
md_link_path, docs, docs
214+
)
215+
if link_found != 0:
216216
soup = cite(
217-
link_found, link, soup,
218-
citation_part, config, callout, self.config['custom-attributes'], language_message
219-
)
217+
link_found,
218+
link,
219+
soup,
220+
citation_part,
221+
config,
222+
callout,
223+
self.config["custom-attributes"],
224+
language_message,
225+
)
220226
return str(soup)

0 commit comments

Comments
 (0)