Skip to content

Commit 0f6f181

Browse files
committed
fix(latex): preserve latex in citation
will take the config from mkdocs or create a custom config but it needs mathjax/katex support
1 parent 421aae7 commit 0f6f181

File tree

3 files changed

+57
-25
lines changed

3 files changed

+57
-25
lines changed

mkdocs_embed_file_plugins/plugin.py

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import codecs
2-
import os
2+
import logging
33
import re
44
from pathlib import Path
55
from urllib.parse import unquote
6+
67
import frontmatter
78
import markdown
89
from bs4 import BeautifulSoup
10+
from custom_attributes.plugin import convert_text_attributes
911
from mkdocs.config import config_options
1012
from mkdocs.plugins import BasePlugin
1113
from mkdocs_callouts.plugin import CalloutsPlugin
12-
from custom_attributes.plugin import convert_text_attributes
13-
import logging
1414

1515
from mkdocs_embed_file_plugins.src.links_correction import (
1616
MULTIMEDIA_EXTENSIONS,
@@ -27,33 +27,52 @@
2727
strip_comments,
2828
)
2929

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+
3039

3140
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,
3350
) -> BeautifulSoup:
3451
"""Append the content of the founded file to the original file.
3552
3653
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.
3855
link (str): Link to the file to be included.
3956
soup (BeautifulSoup): BeautifulSoup object of the file to be modified.
4057
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.
4259
callouts (CalloutsPlugin): Callouts plugin.
4360
custom_attr (CustomAttributesPlugin): Custom attributes plugin.
4461
msg (str): Message to be displayed if the file is not found.
62+
md_config (dict): Configuration of the markdown extensions.
4563
Returns: updated HTML
4664
"""
65+
log = logging.getLogger("mkdocs.plugins." + __name__)
4766
docs = config["docs_dir"]
4867
url = config["site_url"]
49-
50-
md_config = {
68+
mdx_wikilink_plus = {
5169
"mdx_wikilink_plus": {
5270
"base_url": (docs, url, md_link_path),
5371
"build_url": mini_ez_links,
5472
"image_class": "wikilink",
55-
}
73+
},
5674
}
75+
md_config.update(mdx_wikilink_plus)
5776
new_uri = str(md_link_path).replace(str(docs), str(url))
5877
new_uri = new_uri.replace("\\", "/")
5978
new_uri = new_uri.replace(".md", "/")
@@ -83,9 +102,13 @@ def cite(
83102
quote = strip_comments(quote)
84103
md_extensions = config["markdown_extensions"]
85104
md_extensions.append("mdx_wikilink_plus")
105+
md_extensions = list(dict.fromkeys(md_extensions))
106+
# remove arithmatex from the list of extensions
107+
86108
html = markdown.markdown(
87109
quote, extensions=md_extensions, extension_configs=md_config
88110
)
111+
89112
link_soup = BeautifulSoup(html, "html.parser")
90113
if link_soup:
91114
tooltip_template = (
@@ -101,8 +124,7 @@ def cite(
101124
soup = BeautifulSoup(new_soup, "html.parser")
102125
return soup
103126
else:
104-
log = logging.getLogger("mkdocs.plugins." + __name__)
105-
log.info(
127+
log.warning(
106128
"[EMBED FILE PLUGIN] CITATION NOT FOUND : "
107129
+ unquote(citation_part)
108130
+ "for : "
@@ -141,9 +163,20 @@ def __init__(self):
141163
self.enabled = True
142164
self.total_time = 0
143165

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+
144176
def on_post_page(self, output_content, page, config) -> str:
145177
soup = BeautifulSoup(output_content, "html.parser")
146178
docs = Path(config["docs_dir"])
179+
md_config = self.create_config(config)
147180
md_link_path = ""
148181
callout = self.config["callouts"]
149182
language_message = self.config["language_message"]
@@ -162,32 +195,30 @@ def on_post_page(self, output_content, page, config) -> str:
162195
elif link["src"][0] == ".": # relative links
163196
md_src = create_link(unquote(link["src"]))
164197
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
166199
).resolve()
167200
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():
169202
md_link_path = search_file_in_documentation(
170203
md_link_path, docs, docs
171204
)
172205

173206
elif link["src"][0] == "/":
174207
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
176209
md_link_path = Path(unquote(md_link_path)).resolve()
177210

178211
elif link["src"][0] != "#":
179212
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+
183215
md_link_path = re.sub(r"/#(.*).md$", ".md", str(md_link_path))
184216
md_link_path = Path(unquote(md_link_path)).resolve()
185217

186218
else:
187219
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+
191222
md_link_path = Path(unquote(md_link_path)).resolve()
192223
if md_link_path == 0:
193224
soup = tooltip_not_found(link, soup, language_message)
@@ -202,7 +233,7 @@ def on_post_page(self, output_content, page, config) -> str:
202233
if citation_part:
203234
md_link_path = Path(str(md_link_path))
204235

205-
if os.path.isfile(md_link_path):
236+
if Path(md_link_path).is_file():
206237
soup = cite(
207238
md_link_path,
208239
link,
@@ -212,6 +243,7 @@ def on_post_page(self, output_content, page, config) -> str:
212243
callout,
213244
self.config["custom-attributes"],
214245
language_message,
246+
md_config,
215247
)
216248
else:
217249
link_found = search_file_in_documentation(
@@ -227,5 +259,6 @@ def on_post_page(self, output_content, page, config) -> str:
227259
callout,
228260
self.config["custom-attributes"],
229261
language_message,
262+
md_config,
230263
)
231264
return add_not_found_class(str(soup))

mkdocs_embed_file_plugins/src/utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import re
22
from bs4 import BeautifulSoup
33

4-
54
def strip_comments(markdown):
65
file_content = markdown.split("\n")
76
markdown = ""
@@ -22,7 +21,6 @@ def create_link(link):
2221
else:
2322
return link + ".md"
2423

25-
2624
def add_not_found_class(html):
2725
soup = BeautifulSoup(html, "html.parser")
2826

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dependencies = [
1313
"mkdocs-callouts",
1414
"mkdocs-custom-tags-attributes",
1515
"pymdown-extensions",
16+
"ruamel-yaml"
1617
]
1718
authors = [
1819
{ name = "Mara-Li", email = "mara-li@outlook.fr" }
@@ -36,8 +37,8 @@ classifiers = [
3637
]
3738

3839
[tool.ruff.lint]
39-
select = ["PTH", "ANN", "N", "Q", "PL", "E", "F", "I"]
40-
ignore = ["E501"]
40+
select = ["PTH", "N", "Q", "PL", "E", "F", "I"]
41+
ignore = ["E501", "ANN", "PLR"]
4142
exclude = ["tests", "docs", "build", "dist", "venv", "venv3", "venv3.6", "venv3.7", "venv3.8", "venv3.9", "venv3.10", "__pycache__"]
4243

4344
[tool.ruff.lint.flake8-quotes]

0 commit comments

Comments
 (0)