|
| 1 | +# Copyright (c) 2016-2025 Martin Donath <[email protected]> |
| 2 | + |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to |
| 5 | +# deal in the Software without restriction, including without limitation the |
| 6 | +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 | +# sell copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | + |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | + |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 | +# IN THE SOFTWARE. |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import posixpath |
| 24 | +import re |
| 25 | + |
| 26 | +from mkdocs.config.defaults import MkDocsConfig |
| 27 | +from mkdocs.structure.files import File, Files |
| 28 | +from mkdocs.structure.pages import Page |
| 29 | +from re import Match |
| 30 | + |
| 31 | +# ----------------------------------------------------------------------------- |
| 32 | +# Hooks |
| 33 | +# ----------------------------------------------------------------------------- |
| 34 | + |
| 35 | +def on_page_markdown( |
| 36 | + markdown: str, *, page: Page, config: MkDocsConfig, files: Files |
| 37 | +): |
| 38 | + |
| 39 | + # Replace callback |
| 40 | + def replace(match: Match): |
| 41 | + type, args = match.groups() |
| 42 | + args = args.strip() |
| 43 | + if type == "version": |
| 44 | + return _badge_for_version(args, page, files) |
| 45 | + |
| 46 | + raise RuntimeError(f"Unknown shortcode: {type}") |
| 47 | + |
| 48 | + # Find and replace all external asset URLs in current page |
| 49 | + return re.sub( |
| 50 | + r"<!-- md:(\w+)(.*?) -->", |
| 51 | + replace, markdown, flags = re.I | re.M |
| 52 | + ) |
| 53 | + |
| 54 | +def _badge(icon: str, text: str = "", type: str = ""): |
| 55 | + print("makin") |
| 56 | + classes = f"mdx-badge mdx-badge--{type}" if type else "mdx-badge" |
| 57 | + return "".join([ |
| 58 | + f"<span class=\"{classes}\">", |
| 59 | + *([f"<span class=\"mdx-badge__icon\">{icon}</span>"] if icon else []), |
| 60 | + *([f"<span class=\"mdx-badge__text\">{text}</span>"] if text else []), |
| 61 | + f"</span>", |
| 62 | + ]) |
| 63 | + |
| 64 | +def _badge_for_version(text: str, page: Page, files: Files): |
| 65 | + spec = text |
| 66 | + |
| 67 | + repo_url = "https://github.com/SnotyMe/snoty-backend" |
| 68 | + |
| 69 | + if spec == "next": |
| 70 | + url = f"{repo_url}/tree/main" |
| 71 | + else: |
| 72 | + url = f"{repo_url}/releases/tag/{spec}" |
| 73 | + |
| 74 | + text = f"[{spec}]({url})" + '{: target="_blank"}' if spec else "" |
| 75 | + # dev is on a feature branch => not guaranteed to land in the next version => no link (could be anywhere) |
| 76 | + if spec == "dev": |
| 77 | + text = spec |
| 78 | + |
| 79 | + icon = "material-tag-outline" |
| 80 | + return _badge( |
| 81 | + icon = f"[:{icon}:]('Available from version {spec}')", |
| 82 | + text = text |
| 83 | + ) |
0 commit comments