Skip to content

Commit 3a913ef

Browse files
committed
🎉 Initial commit
0 parents  commit 3a913ef

File tree

10 files changed

+258
-0
lines changed

10 files changed

+258
-0
lines changed

.editorconfig

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This file is for unifying the coding style for different editors and IDEs
2+
# editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
indent_size = 4
8+
end_of_line = lf
9+
charset = utf-8
10+
insert_final_newline = true
11+
trim_trailing_whitespace = true
12+
indent_style = space
13+
max_line_length = 90
14+
15+
[*.{yaml,yml}]
16+
indent_size = 2

.github/workflows/build.yml

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: Build
2+
on:
3+
push:
4+
pull_request:
5+
release:
6+
types: [published]
7+
workflow_dispatch:
8+
9+
jobs:
10+
build:
11+
name: 🔨 Build distribution
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: 🏗 Set up Python 3.7
16+
uses: actions/setup-python@v4
17+
with:
18+
python-version: "3.7"
19+
- name: 🏗 Install build dependencies
20+
run: |
21+
python -m pip install wheel --user
22+
- name: 🔨 Build a binary wheel and a source tarball
23+
run: |
24+
python setup.py sdist bdist_wheel
25+
- name: ⬆ Upload build result
26+
uses: actions/upload-artifact@v3
27+
with:
28+
name: dist
29+
path: dist
30+
31+
pre-commit:
32+
name: 🧹 Pre-commit
33+
runs-on: ubuntu-latest
34+
steps:
35+
- uses: actions/checkout@v3
36+
- name: 🏗 Set up Python 3.11
37+
uses: actions/setup-python@v4
38+
with:
39+
python-version: "3.11"
40+
- name: 🏗 Set up dev dependencies
41+
run: |
42+
pip install pre-commit
43+
- name: 🚀 Run pre-commit
44+
run: |
45+
pre-commit run --all-files --show-diff-on-failure
46+
47+
publish-on-pypi:
48+
name: 📦 Publish tagged releases to PyPI
49+
if: github.event_name == 'release' && github.repository == 'OctoPrint/mkdocs-site-urls'
50+
needs:
51+
- build
52+
- pre-commit
53+
runs-on: ubuntu-latest
54+
environment: release
55+
permissions:
56+
id-token: write
57+
steps:
58+
- name: ⬇ Download build result
59+
uses: actions/download-artifact@v3
60+
with:
61+
name: dist
62+
path: dist
63+
- name: 📦 Publish to index
64+
uses: pypa/gh-action-pypi-publish@release/v1
65+
with:
66+
password: ${{ secrets.pypi_token }}

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
build
2+
dist
3+
*.egg-info
4+
*.egg
5+
__pycache__
6+
*.pyc
7+
*.pyo
8+
*.pyd

.pre-commit-config.yaml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
default_language_version:
2+
python: python3.11
3+
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v4.4.0
6+
hooks:
7+
- id: end-of-file-fixer
8+
- id: trailing-whitespace
9+
- id: check-case-conflict
10+
- id: check-merge-conflict
11+
- id: fix-encoding-pragma
12+
args: ["--remove"]
13+
- repo: https://github.com/asottile/pyupgrade
14+
rev: v3.9.0
15+
hooks:
16+
- id: pyupgrade
17+
args: ["--py37-plus"]
18+
- repo: https://github.com/pre-commit/mirrors-isort
19+
rev: v5.10.1
20+
hooks:
21+
- id: isort
22+
- repo: https://github.com/psf/black
23+
rev: 23.7.0
24+
hooks:
25+
- id: black
26+
- repo: https://github.com/pycqa/flake8
27+
rev: 6.0.0
28+
hooks:
29+
- id: flake8
30+
args: [--config=setup.cfg]

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Gina Häußge
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# MkDocs Site URLs
2+
3+
A MkDocs plugin that adds support for site-relative `site:` URLs.
4+
5+
Example:
6+
7+
| URL | site_url | resulting URL |
8+
| --- | -------- | ------------- |
9+
| `site:images/foo.png` | `https://example.com/` | `/images/foo.png` |
10+
| `site:images/foo.png` | `https://example.com/bar/` | `/bar/images/foo.png` |
11+
12+
## Usage
13+
14+
1. Install the plugin from PyPI
15+
```bash
16+
pip install mkdocs-site-urls
17+
```
18+
2. Add the `site-urls` plugin to your `mkdocs.yml` plugins section:
19+
```yaml
20+
plugins:
21+
- site-urls
22+
```
23+
There are no configuration options.
24+
3. Start using site-relative URLs in your Markdown files by prefixing them with `site:`:
25+
```markdown
26+
[Link to another page](site:another-page/relative/to/the/site/root)
27+
28+
![Image](site:images/foo.png)
29+
```
30+
31+
## How it works
32+
33+
The plugin hooks into the [`on_page_content` event](https://www.mkdocs.org/dev-guide/plugins/#on_page_content)
34+
and replaces all URLs in `href` or `src` attributes in the rendered HTML with the corresponding site-relative URLs.
35+
36+
## License
37+
38+
This project is licensed under the MIT license, see the [LICENSE](https://github.com/OctoPrint/mkdocs-site-urls/blob/main/LICENSE) file for details.

mkdocs_site_urls/__init__.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import re
2+
import urllib.parse
3+
4+
import mkdocs.plugins
5+
6+
SITE_URLS_REGEX = re.compile(r'(href|src)="site:([^"]+)"', re.IGNORECASE)
7+
8+
logger = mkdocs.plugins.get_plugin_logger(__name__)
9+
10+
11+
class SiteUrlsPlugin(mkdocs.plugins.BasePlugin):
12+
@mkdocs.plugins.event_priority(50)
13+
def on_page_content(self, html, page, config, files):
14+
site_url = config["site_url"]
15+
path = urllib.parse.urlparse(site_url).path
16+
17+
if not path:
18+
path = "/"
19+
if not path.endswith("/"):
20+
path += "/"
21+
22+
def _replace(match):
23+
param = match.group(1)
24+
url = match.group(2)
25+
if url.startswith("/"):
26+
url = url[1:]
27+
28+
logger.info(f"Replacing site:{match.group(2)} with {path}{url}...")
29+
return f'{param}="{path}{url}"'
30+
31+
return SITE_URLS_REGEX.sub(_replace, html)

pyproject.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[build-system]
2+
requires = ["setuptools>=40.8.0", "wheel"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[tool.black]
6+
line-length = 90

setup.cfg

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[metadata]
2+
license_file = LICENSE
3+
4+
[bdist_wheel]
5+
universal = 1
6+
7+
[flake8]
8+
max-line-length = 90
9+
extend-ignore = E203, E231, E265, E266, E402, E501, E731, B023, B903, B904, B907, B950, W503
10+
select = B,C,E,F,W,T4,B9
11+
12+
[isort]
13+
multi_line_output = 3
14+
include_trailing_comma = True
15+
force_grid_wrap = 0
16+
use_parentheses = True
17+
ensure_newline_before_comments = True
18+
line_length = 90
19+
known_first_party =
20+
mkdocs_site_urls

setup.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from setuptools import find_packages, setup
2+
3+
with open("README.md", encoding="utf-8") as f:
4+
long_description = f.read()
5+
6+
setup(
7+
name="mkdocs-site-urls",
8+
version="0.1.0",
9+
author="Gina Häußge",
10+
author_email="[email protected]",
11+
url="https://github.com/OctoPrint/mkdocs-site-urls",
12+
project_urls={"Source": "https://github.com/OctoPrint/mkdocs-site-urls"},
13+
keywords=["mkdocs", "plugin"],
14+
packages=find_packages(),
15+
license="MIT",
16+
description="A MkDocs plugin that adds support for site-relative URLs",
17+
long_description=long_description,
18+
long_description_content_type="text/markdown",
19+
include_package_data=True,
20+
python_requires=">=3.7",
21+
entry_points={"mkdocs.plugins": ["site-urls = mkdocs_site_urls:SiteUrlsPlugin"]},
22+
)

0 commit comments

Comments
 (0)