|
| 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 sys |
| 19 | +import textwrap |
| 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 | + print(colorize_message(_warning_message), file=sys.stderr) # noqa: T201 |
| 50 | + |
| 51 | + # Prevent warnings from the theme that already uses this environment variable. |
| 52 | + # That warning does not apply to ProperDocs. |
| 53 | + os.environ['NO_MKDOCS_2_WARNING'] = 'true' |
| 54 | + |
| 55 | + _warning_message = '' # Disable all activations other than the first one. |
| 56 | + |
| 57 | + |
| 58 | +def is_running_from_mkdocs(): |
| 59 | + if 'mkdocs' not in sys.modules: |
| 60 | + return False |
| 61 | + |
| 62 | + dir, name = os.path.split(sys.argv[0]) |
| 63 | + if name in ('mkdocs', 'mkdocs.exe'): |
| 64 | + return True |
| 65 | + elif name.endswith('.py'): |
| 66 | + dir, name = os.path.split(dir) |
| 67 | + if name == 'mkdocs': |
| 68 | + return True |
| 69 | + return False |
| 70 | + |
| 71 | + |
| 72 | +def colorize_message(message: str, max_width: int = 145) -> str: |
| 73 | + try: # Try to colorize and rewrap the message, ignore all errors just in case. |
| 74 | + import re |
| 75 | + import shutil |
| 76 | + |
| 77 | + if terminal_width := shutil.get_terminal_size(fallback=(0, 0)).columns: |
| 78 | + lines = [] |
| 79 | + for line in message.split('\n'): |
| 80 | + lines.extend(textwrap.wrap(line, width=min(terminal_width, max_width)) or ['']) |
| 81 | + message = '\n'.join(lines) |
| 82 | + |
| 83 | + import click |
| 84 | + |
| 85 | + message = re.sub(r'\bWARNING\b', lambda m: click.style(m[0], fg='red', bold=True), message) |
| 86 | + message = re.sub(r'https://\S+', lambda m: click.style(m[0], bold=True), message) |
| 87 | + message = re.sub( |
| 88 | + r'\B`\b[\s\S]+?\b`\B', lambda m: click.style(m[0], bold=True, fg='yellow'), message |
| 89 | + ) |
| 90 | + message = re.sub(r'\B\*\b([\s\S]+?)\b\*\B', lambda m: click.style(m[1], bold=True), message) |
| 91 | + except Exception: |
| 92 | + pass |
| 93 | + return message |
0 commit comments