URL
# (absolute so it also works where cgit renders the readme on the summary page).
# cgit exports the repo context to about-filters via these environment vars; if
# they're absent we leave the src untouched.
import os, re
repo = os.environ.get("CGIT_REPO_URL", "").strip("/")
if repo:
branch = os.environ.get("CGIT_REPO_DEFBRANCH", "").strip()
plain = "/" + repo + "/plain/"
query = ("?h=" + branch) if branch else ""
# leave absolute, protocol-relative, root-relative, anchor and data: URLs alone
skip = re.compile(r"^(?:[a-z][a-z0-9+.\-]*:|//|/|#)", re.I)
def rewrite(m):
url = m.group(2)
if not url or skip.match(url):
return m.group(0)
return m.group(1) + plain + re.sub(r"^\./", "", url) + query + m.group(3)
html = re.sub(r'(
]*?\bsrc=")([^"]*)(")', rewrite, html, flags=re.I)
sys.stdout.write("")
sys.stdout.write(html)
sys.stdout.write("
")