Files
my-rook-config/.docs/macros/includes/main.py
T

33 lines
697 B
Python
Raw Normal View History

#!/usr/bin/env python3
2022-05-12 19:29:40 +02:00
from pygit2 import Repository
import re
"""
GitHub branch/tag URL replacer
"""
regex = r"(github\.com/.+/rook/.+)/master/"
subst = "\\1/%s/"
2022-06-07 13:49:14 +05:30
2022-05-12 19:29:40 +02:00
def define_env(env):
2024-05-14 22:28:21 +03:00
target = None
2022-06-07 13:49:14 +05:30
repo = Repository(".")
2022-05-12 19:29:40 +02:00
if repo is not None:
target = repo.head.shorthand
2022-06-07 13:49:14 +05:30
env.variables["current_branch"] = target
2022-05-12 19:29:40 +02:00
def on_post_page_macros(env):
"""
Replace the branch/tag in the rook GitHub file and directory links pointing to `master`
with the correct one that is currently active.
"""
2022-06-07 13:49:14 +05:30
target = env.variables["current_branch"]
if target == "master":
2022-05-12 19:29:40 +02:00
return
env.raw_markdown = re.sub(regex, subst % target, env.raw_markdown, 0)