Skip to content

Commit 0eb5cf9

Browse files
committed
Add a nagging message when the "mkdocs" executable is detected
Plugin authors can join in on this message by simply calling our special module. (Other than that, there's no way for us to cause it to be imported. So, without any participating plugins nothing happens.) Please add our warning to your MkDocs plugin in the following way: ``` import properdocs.replacement_warning properdocs.replacement_warning.setup() ```
1 parent f440858 commit 0eb5cf9

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

properdocs/replacement_warning.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""
2+
This module shows a warning to users that are still using the `mkdocs` executable, and doesn't show up for `properdocs`.
3+
4+
It kicks in as long as at least one plugin calls the `setup` function.
5+
Please add our warning to your plugin in the following way:
6+
7+
```
8+
import properdocs.replacement_warning
9+
10+
properdocs.replacement_warning.setup()
11+
```
12+
13+
This should be added somewhere in your module's topmost import (__init__.py) at the top level, to activate early.
14+
"""
15+
16+
import os
17+
import os.path
18+
import re
19+
import sys
20+
21+
_warning_message = '''\
22+
----------------------------------------------------------------
23+
24+
WARNING: MkDocs may break support for all existing plugins and themes soon!
25+
26+
The owner of MkDocs has completely abandoned maintenance of the project, and instead is planning \
27+
to publish a "version 2" which will not support any existing themes, plugins or even your \
28+
configuration files. This v2 may eventually replace what you download with `pip install mkdocs`, \
29+
suddenly breaking the build of your existing site.
30+
31+
To avoid these risks, switch to *ProperDocs*, a continuation of MkDocs 1.x and a drop-in replacement that supports your current MkDocs setup.
32+
Simply install it with `pip install properdocs` and build your site with `properdocs build` instead of the MkDocs equivalents.
33+
34+
For more info visit https://github.com/ProperDocs/properdocs/discussions/123 and https://properdocs.org/
35+
36+
(This warning was initiated by one of the plugins that you depend on.)
37+
38+
----------------------------------------------------------------'''
39+
40+
41+
def setup():
42+
global _warning_message
43+
if not _warning_message:
44+
return
45+
46+
if _is_running_from_mkdocs():
47+
# Allow to silence this warning with NO_MKDOCS_2_WARNING=true
48+
if os.environ.get('NO_MKDOCS_2_WARNING', '').lower() != 'true':
49+
message = _warning_message
50+
# Try to colorize the message, ignore all errors from this process just in case.
51+
try:
52+
import click
53+
54+
message = re.sub(r'\bWARNING\b', lambda m: click.style(m[0], fg='red', bold=True), message)
55+
message = re.sub(r'https://\S+', lambda m: click.style(m[0], bold=True), message)
56+
message = re.sub(r'\B`\b.+?\b`\B', lambda m: click.style(m[0], bold=True, fg='yellow'), message)
57+
message = re.sub(r'\B\*\b(.+?)\b\*\B', lambda m: click.style(m[1], bold=True), message)
58+
except Exception: # Ignore all errors prettifying the message.
59+
pass
60+
print(message, file=sys.stderr) # noqa: T201
61+
62+
# Prevent warnings from the theme that already uses this environment variable.
63+
# That warning does not apply to ProperDocs.
64+
os.environ['NO_MKDOCS_2_WARNING'] = 'true'
65+
66+
_warning_message = '' # Disable all activations other than the first one.
67+
68+
69+
def _is_running_from_mkdocs():
70+
if 'mkdocs' not in sys.modules:
71+
return False
72+
73+
dir, name = os.path.split(sys.argv[0])
74+
if name in ('mkdocs', 'mkdocs.exe'):
75+
return True
76+
elif name.endswith('.py'):
77+
dir, name = os.path.split(dir)
78+
if name == 'mkdocs':
79+
return True
80+
return False

0 commit comments

Comments
 (0)