-
-
Notifications
You must be signed in to change notification settings - Fork 782
/
Copy pathmain.py
41 lines (28 loc) · 1.53 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from __future__ import annotations as _annotations
import re
import subprocess
from mkdocs.config import Config
from mkdocs.structure.files import Files
from mkdocs.structure.pages import Page
def on_page_content(html: str, page: Page, config: Config, files: Files) -> str:
"""Called on each page after the markdown is converted to HTML."""
html = add_hyperlink_to_pull_request(html, page, config, files)
return html
def add_hyperlink_to_pull_request(html: str, page: Page, config: Config, files: Files) -> str:
"""Add hyperlink on PRs mentioned on the release notes page.
If we find "(#\\d+)" it will be added an hyperlink to https://github.com/encode/uvicorn/pull/$1.
"""
if not page.file.name == "release-notes":
return html
return re.sub(r"\(#(\d+)\)", r"(<a href='https://github.com/encode/uvicorn/pull/\1'>#\1</a>)", html)
def on_page_markdown(markdown: str, page: Page, config: Config, files: Files) -> str:
"""Called on each file after it is read and before it is converted to HTML."""
markdown = uvicorn_print_help(markdown, page)
return markdown
def uvicorn_print_help(markdown: str, page: Page) -> str:
# if you don't filter to the specific route that needs this substitution, things will be very slow
if page.file.src_uri not in ("index.md", "deployment.md"):
return markdown
output = subprocess.run(["uvicorn", "--help"], capture_output=True, check=True)
logfire_help = output.stdout.decode()
return re.sub(r"{{ *uvicorn_help *}}", logfire_help, markdown)