Commit fd3904be authored by Deimos's avatar Deimos

Add plugin to convert wiki links to Docs ones

This is to simplify moving pages over from the ~tildes.official wiki to
the Docs site - any links to pages on the wiki will be converted to ones
that will lead to the equivalent page on the Docs site instead,
including replacing underscores with hyphens to compensate for the
different slug-generation methods between the locations.
parent 2799d92f
PLUGIN_PATHS = ["plugins"]
PLUGINS = ["pelican_git_modified_plugin"]
PLUGINS = ["pelican_git_modified_plugin", "wiki_url_replacer_plugin"]
AUTHOR = 'Chad Birch (Deimos)'
SITENAME = 'Tildes'
from urllib.parse import urlparse, urlunparse
from bs4 import BeautifulSoup
from pelican.signals import content_written
def replace_wiki_urls(path, context):
"""Replace any urls pointing to the ~tildes.official wiki with Docs ones."""
with open(path, "r", encoding="utf-8") as f:
soup = BeautifulSoup(, features="html5lib")
for link in soup.find_all("a"):
if "href" not in link.attrs:
parsed = urlparse(link["href"])
if parsed.netloc != "":
prefix = "/~tildes.official/wiki/"
if not parsed.path.startswith(prefix):
# remove the prefix from the path and replace underscores with hyphens
new_path = parsed.path[len(prefix)-1:]
new_path = new_path.replace("_", "-")
# convert to, use modified path, and replace in the fragment too
parsed = parsed._replace(
fragment=parsed.fragment.replace("_", "-"),
link["href"] = urlunparse(parsed)
# write the file back out with the modified links, replacing original
with open(path, "w", encoding="utf-8") as f:
def register():
"""Register the method to run after writing each piece of content."""
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment