Skip to content

Commit dd9e1ec

Browse files
committed
Create mkdocs webcontext plugin
1 parent c32f14f commit dd9e1ec

6 files changed

Lines changed: 127 additions & 0 deletions

File tree

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
venv
2+
build
3+
dist
4+
.idea
5+
.vscode
6+
build.bat
7+
/.vs

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include readme.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .plugin import Webcontext

mkdocs_webcontext_plugin/plugin.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import os
2+
import sys
3+
import re
4+
import logging
5+
from timeit import default_timer as timer
6+
from datetime import datetime, timedelta
7+
8+
from mkdocs import utils as mkdocs_utils
9+
from mkdocs.config import config_options, Config
10+
from mkdocs.plugins import BasePlugin
11+
from mkdocs.structure.pages import Page
12+
13+
from pathlib import Path
14+
15+
LOGGER = logging.getLogger(__name__)
16+
17+
class Webcontext(BasePlugin):
18+
19+
config_scheme = (
20+
('context', config_options.Type(str, default='/')),
21+
)
22+
23+
md_link = re.compile(r'''(\[[^]]*]\()(/[^)]*)(\))|(\[[^]]*]:\s)(/[^\n\r)]*)''', re.DOTALL | re.UNICODE)
24+
25+
def __init__(self):
26+
self.enabled = True
27+
self.total_time = 0
28+
29+
def on_page_markdown(self, markdown, page, config, files):
30+
context = self.config['context']
31+
new_md = self._absolute_to_webcontext(markdown, context)
32+
return new_md
33+
34+
def _absolute_to_webcontext(self, markdown, context: str):
35+
LOGGER.debug('webcontext: using defined context = %s', context)
36+
37+
def _check_link(link: str, context: str):
38+
contextLink = context +"/"+ link[1:]
39+
LOGGER.debug('webcontext: replace %s with %s', link, contextLink)
40+
link = link.replace(link[1:], contextLink)
41+
link = link.replace("\\", "/")
42+
43+
return link
44+
45+
def _to_webcontext(context:str):
46+
def re_write_link(matchobj):
47+
group1 = matchobj.group(1) # front stuff
48+
group2 = matchobj.group(2) # the actual link
49+
group3 = matchobj.group(3) # closing tag
50+
51+
if group1 and group2 and group3:
52+
link = _check_link(group2, context)
53+
return "{}{}{}".format(group1, link, group3)
54+
55+
group4 = matchobj.group(4) # front stuff
56+
group5 = matchobj.group(5) # the actual link
57+
if group4 and group5:
58+
link = _check_link(group5, context)
59+
return "{}{}".format(group4, link)
60+
61+
return re_write_link
62+
63+
file_data = re.sub(self.md_link, _to_webcontext(context), markdown)
64+
return file_data

readme.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Webcontext link converter plugin for mkdocs
2+
3+
MkDocs allows absulute paths assuming that the site is deployed to the root of the hosted website like "http://localhost/". When using the following absolute path "/assets/image1.jpg" it is actually using "http://localhost/assets/image1.jpg" which is correct if the MkDocs is deployed to the site root.
4+
5+
When the server root is not the same as the MkDocs root, the webcontext plugin can be used to define a webcontext to use instead of the defined root. The webcontext path which can be anyting like "/projectname/documents" is used where "/" is defined.
6+
7+
Some examples of site urls before and after using the webcontext plugin:
8+
Site Url | Context | Image before | Image after
9+
---------|----------|--------------|------------
10+
http://example.com/ | / | /images/img1.jpg | /images/img1.jpg
11+
http://example.com/foo | /foo | /images/img1.jpg | /foo/images/img1.jpg
12+
http://example.com/foo/bar | /foo/bar | /images/img1.jpg | /foo/bar/images/img1.jpg
13+
http://127.0.0.1:8000 | / | /images/img1.jpg | /images/img1.jpg
14+
http://127.0.0.1:8000/foo | /foo | /images/img1.jpg | /foo/images/img1.jpg
15+
16+
17+
## Quick start
18+
19+
1. Install the module using pip: `pip install mkdocs-webcontext`
20+
21+
2. In your project, add a plugin configuration to `mkdocs.yml`:
22+
23+
```yaml
24+
plugins:
25+
- webcontext:
26+
context: foo/bar
27+
```
28+
29+
Special thanks to the following repositories for guidance:
30+
31+
* [byrnereese/mkdocs-plugin-template](https://github.com/byrnereese/mkdocs-plugin-template)
32+
* [sander76/mkdocs-abs-rel-plugin](https://github.com/sander76/mkdocs-abs-rel-plugin)

setup.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import sys
2+
from shutil import rmtree
3+
from pathlib import Path
4+
from setuptools import setup, Command
5+
6+
setup(
7+
name="mkdocs-webcontext-plugin",
8+
version="0.1.0",
9+
packages=["mkdocs_webcontext_plugin"],
10+
url="https://github.com/darrelk/mkdocs-webcontext-plugin",
11+
license="MIT",
12+
author="darrelk",
13+
author_email="darrelkley@gmail.com",
14+
description="Mkdocs plugin to convert absolute paths to Webcontext aware paths.",
15+
long_description=README,
16+
long_description_content_type="text/markdown",
17+
entry_points={
18+
"mkdocs.plugins": [
19+
"webcontext = mkdocs_webcontext_plugin:Webcontext"
20+
]
21+
}
22+
)

0 commit comments

Comments
 (0)