Skip to content

Commit c23fd84

Browse files
authored
Merge pull request #2 from manics/dev
Bundle custom plugins in this repo
2 parents 7cb8c94 + f028e85 commit c23fd84

9 files changed

Lines changed: 750 additions & 2 deletions

File tree

plugins/mkdocs-uktre-glossary-plugin/.gitignore

Lines changed: 521 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Tim Vink
4+
Copyright (c) 2025 UKTRE
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# mkdocs-uktre-glossary-plugin
2+
3+
An [MkDocs](https://www.mkdocs.org/) plugin based on [mkdocs-table-reader-plugin](https://github.com/timvink/mkdocs-table-reader-plugin) to convert the UK TRE glossary from YAML to HTML.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[build-system]
2+
requires = ["setuptools>=70.0", "setuptools-scm>=8.0"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project.entry-points."mkdocs.plugins"]
6+
"uktre-glossary" = "mkdocs_uktre_glossary_plugin.plugin:GlossaryPlugin"
7+
8+
[project]
9+
name="mkdocs_uktre_glossary_plugin"
10+
keywords = ["mkdocs", "plugin"]
11+
authors = [
12+
{name = "UKTRE"},
13+
]
14+
license = { text = "MIT" }
15+
16+
description="MkDocs plugin to generate UKTRE glossary"
17+
readme = { file = "README.md", content-type = "text/markdown" }
18+
19+
requires-python=">=3.11"
20+
21+
classifiers=[
22+
"Operating System :: OS Independent",
23+
"Programming Language :: Python",
24+
"Programming Language :: Python :: 3",
25+
]
26+
27+
dynamic = ["version"]
28+
dependencies = [
29+
"mkdocs>=1.6.1",
30+
"pandas>=2.2.3",
31+
"pyyaml>=6.0.2",
32+
"tabulate>=0.9.0",
33+
# We're hooking in to the internals of this plugin, so pin exactly
34+
"mkdocs-table-reader-plugin==3.1.0",
35+
]
36+
37+
[project.urls]
38+
"Homepage" = "https://github.com/TODO/TODO"
39+
40+
[tool.setuptools.dynamic]
41+
version = {attr = "mkdocs_uktre_glossary_plugin.__version__"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = "3.1.0"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from mkdocs.config import config_options
2+
from mkdocs.plugins import get_plugin_logger
3+
from mkdocs_table_reader_plugin.plugin import TableReaderPlugin
4+
5+
from .readers import READERS
6+
7+
logger = get_plugin_logger("uktre-glossary")
8+
9+
10+
class GlossaryPlugin(TableReaderPlugin):
11+
config_scheme = (
12+
("data_path", config_options.Type(str, default=".")),
13+
("allow_missing_files", config_options.Type(bool, default=False)),
14+
(
15+
"select_readers",
16+
config_options.ListOfItems(
17+
config_options.Choice(list(READERS.keys())),
18+
default=list(READERS.keys()),
19+
),
20+
),
21+
)
22+
23+
def on_config(self, config, **kwargs):
24+
"""
25+
See https://www.mkdocs.org/user-guide/plugins/#on_config.
26+
27+
Args:
28+
config
29+
30+
Returns:
31+
Config
32+
"""
33+
34+
self.readers = {
35+
reader: READERS[reader].set_config_context(
36+
mkdocs_config=config, plugin_config=self.config
37+
)
38+
for reader in self.config.get("select_readers")
39+
if reader in self.config.get("select_readers", [])
40+
}
41+
self.external_jinja_engine = False
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import logging
2+
3+
from html import escape
4+
from markdown import markdown
5+
import pandas as pd
6+
from textwrap import dedent
7+
import re
8+
import yaml
9+
10+
from mkdocs_table_reader_plugin.markdown import convert_to_md_table
11+
from mkdocs_table_reader_plugin.readers import ParseArgs
12+
from mkdocs_table_reader_plugin.utils import kwargs_in_func, kwargs_not_in_func
13+
14+
logger = logging.getLogger("mkdocs.plugins")
15+
16+
17+
18+
def _slugify(s: str):
19+
return re.sub(r"\W", "-", s.lower())
20+
21+
def link_urls(s: str):
22+
trailing_punctuation = re.escape(".,!?)]}>'\"")
23+
s = re.sub(rf"(https?://[\S]+[^{trailing_punctuation}])", r"[\1](\1)", s)
24+
return s
25+
26+
def _crossref_terms(text):
27+
# Find [...] but not [...](...)
28+
matches = re.findall(r"(\[[^]]+\])([^(]|$)", text)
29+
# Get the first capture group
30+
crossrefs = set(m[0] for m in matches)
31+
32+
for crossref in crossrefs:
33+
target_term = crossref[1:-1]
34+
link_target = "#term-" + _slugify(target_term)
35+
link_md = f"[{target_term}]({link_target})"
36+
text = text.replace(crossref, link_md)
37+
return text
38+
39+
40+
def to_glossary_html(df, **kwargs):
41+
"""
42+
df.to_markdown() escapes some HTML, so create a HTML table ourselves
43+
"""
44+
if kwargs:
45+
raise ValueError(f"Unsupported kwargs: {kwargs}")
46+
47+
out = """
48+
<table>
49+
<tr>
50+
<th>Term</th>
51+
<th>Tags</th>
52+
<th>Definition</th>
53+
</tr>
54+
"""
55+
for row in df.itertuples(index=False):
56+
anchor = "term-" + _slugify(row.term)
57+
crossreferenced = _crossref_terms(link_urls(row.definition))
58+
definition = markdown(escape(crossreferenced))
59+
60+
row = f"""
61+
<tr>
62+
<td id="{anchor}"><a href="#{anchor}">{escape(row.term)}</a></td>
63+
<td>{escape(", ".join(row.tags))}</td>
64+
<td>{markdown(definition)}</td>
65+
</tr>
66+
"""
67+
out += row
68+
69+
out += """
70+
</table>
71+
"""
72+
73+
stripped = "\n".join(line.strip() for line in out.splitlines() if line.strip())
74+
return stripped
75+
76+
77+
@ParseArgs
78+
def read_csv(*args, **kwargs) -> str:
79+
read_kwargs = kwargs_in_func(kwargs, pd.read_csv)
80+
df = pd.read_csv(*args, **read_kwargs)
81+
82+
markdown_kwargs = kwargs_not_in_func(kwargs, pd.read_csv)
83+
return to_glossary_html(df, **markdown_kwargs)
84+
85+
86+
@ParseArgs
87+
def read_json(*args, **kwargs) -> str:
88+
read_kwargs = kwargs_in_func(kwargs, pd.read_json)
89+
df = pd.read_json(*args, **read_kwargs)
90+
91+
markdown_kwargs = kwargs_not_in_func(kwargs, pd.read_json)
92+
return to_glossary_html(df, **markdown_kwargs)
93+
94+
95+
@ParseArgs
96+
def read_excel(*args, **kwargs) -> str:
97+
read_kwargs = kwargs_in_func(kwargs, pd.read_excel)
98+
df = pd.read_excel(*args, **read_kwargs)
99+
100+
markdown_kwargs = kwargs_not_in_func(kwargs, pd.read_excel)
101+
return to_glossary_html(df, **markdown_kwargs)
102+
103+
104+
@ParseArgs
105+
def read_yaml(*args, **kwargs) -> str:
106+
json_kwargs = kwargs_in_func(kwargs, pd.json_normalize)
107+
with open(args[0]) as f:
108+
df = pd.json_normalize(yaml.safe_load(f), **json_kwargs)
109+
110+
markdown_kwargs = kwargs_not_in_func(kwargs, pd.json_normalize)
111+
return to_glossary_html(df, **markdown_kwargs)
112+
113+
114+
READERS = {
115+
"read_csv": read_csv,
116+
"read_excel": read_excel,
117+
"read_yaml": read_yaml,
118+
"read_json": read_json,
119+
}

requirements.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
git+https://github.com/manics/mkdocs-uktre-glossary-plugin.git@a2dec8e5045c0b51d7ce44a1f302f5fc9564e687
1+
file:plugins/mkdocs-uktre-glossary-plugin#egg=mkdocs-uktre-glossary-plugin
22
mkdocs
33
mkdocs-material

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ mkdocs-material-extensions==1.3.1
5151
# via mkdocs-material
5252
mkdocs-table-reader-plugin==3.1.0
5353
# via mkdocs-uktre-glossary-plugin
54-
mkdocs-uktre-glossary-plugin @ git+https://github.com/manics/mkdocs-uktre-glossary-plugin.git@a2dec8e5045c0b51d7ce44a1f302f5fc9564e687
54+
file:plugins/mkdocs-uktre-glossary-plugin#egg=mkdocs-uktre-glossary-plugin
5555
# via -r requirements.in
5656
numpy==2.2.4
5757
# via pandas

0 commit comments

Comments
 (0)