|
| 1 | +#!venv/bin/python |
| 2 | +import pathlib |
| 3 | +import posixpath |
| 4 | + |
| 5 | +import click |
| 6 | +import ghp_import |
| 7 | +import httpx |
| 8 | +import jinja2 |
| 9 | +import markdown |
| 10 | + |
| 11 | +import xml.etree.ElementTree as etree |
| 12 | + |
| 13 | + |
| 14 | +pages = { |
| 15 | + '/': 'docs/index.md', |
| 16 | + '/quickstart': 'docs/quickstart.md', |
| 17 | + '/clients': 'docs/clients.md', |
| 18 | + '/requests': 'docs/requests.md', |
| 19 | + '/responses': 'docs/responses.md', |
| 20 | + '/urls': 'docs/urls.md', |
| 21 | + '/headers': 'docs/headers.md', |
| 22 | + '/content-types': 'docs/content-types.md', |
| 23 | + '/connections': 'docs/connections.md', |
| 24 | + '/networking': 'docs/networking.md', |
| 25 | + '/about': 'docs/about.md', |
| 26 | +} |
| 27 | + |
| 28 | +def path_to_url(path): |
| 29 | + if path == "index.md": |
| 30 | + return "/" |
| 31 | + return f"/{path[:-3]}" |
| 32 | + |
| 33 | + |
| 34 | +class URLsProcessor(markdown.treeprocessors.Treeprocessor): |
| 35 | + def __init__(self, state): |
| 36 | + self.state = state |
| 37 | + |
| 38 | + def run(self, root: etree.Element) -> etree.Element: |
| 39 | + for element in root.iter(): |
| 40 | + if element.tag == 'a': |
| 41 | + key = 'href' |
| 42 | + elif element.tag == 'img': |
| 43 | + key = 'src' |
| 44 | + else: |
| 45 | + continue |
| 46 | + |
| 47 | + url_or_path = element.get(key) |
| 48 | + if url_or_path is not None: |
| 49 | + output_url = self.rewrite_url(url_or_path) |
| 50 | + element.set(key, output_url) |
| 51 | + |
| 52 | + return root |
| 53 | + |
| 54 | + def rewrite_url(self, href: str) -> str: |
| 55 | + if not href.endswith('.md'): |
| 56 | + return href |
| 57 | + |
| 58 | + current_url = path_to_url(self.state.file) |
| 59 | + linked_url = path_to_url(href) |
| 60 | + return posixpath.relpath(linked_url, start=current_url) |
| 61 | + |
| 62 | + |
| 63 | +class BuildState: |
| 64 | + def __init__(self): |
| 65 | + self.file = '' |
| 66 | + |
| 67 | + |
| 68 | +state = BuildState() |
| 69 | +env = jinja2.Environment( |
| 70 | + loader=jinja2.FileSystemLoader('docs/templates'), |
| 71 | + autoescape=False |
| 72 | +) |
| 73 | +template = env.get_template('base.html') |
| 74 | +md = markdown.Markdown(extensions=['fenced_code']) |
| 75 | +md.treeprocessors.register( |
| 76 | + item=URLsProcessor(state), |
| 77 | + name='urls', |
| 78 | + priority=10, |
| 79 | +) |
| 80 | + |
| 81 | + |
| 82 | +def not_found(): |
| 83 | + headers = {'Content-Type': 'text/plain'} |
| 84 | + content = b'Not Found' |
| 85 | + return httpx.Response(404, headers=headers, content=content) |
| 86 | + |
| 87 | + |
| 88 | +def web_server(request): |
| 89 | + if request.url.path not in pages: |
| 90 | + return not_found() |
| 91 | + |
| 92 | + file = pages[request.url.path] |
| 93 | + text = pathlib.Path(file).read_text() |
| 94 | + |
| 95 | + state.file = file |
| 96 | + content = md.convert(text) |
| 97 | + html = template.render(content=content).encode('utf-8') |
| 98 | + headers = {'Content-Type': 'text/html; charset=utf-8'} |
| 99 | + return httpx.Response(200, headers=headers, content=html) |
| 100 | + |
| 101 | + |
| 102 | +@click.group() |
| 103 | +def main(): |
| 104 | + pass |
| 105 | + |
| 106 | + |
| 107 | +@main.command() |
| 108 | +def build(): |
| 109 | + pathlib.Path("build").mkdir(exist_ok=True) |
| 110 | + |
| 111 | + for url, path in pages.items(): |
| 112 | + basename = url.lstrip("/") |
| 113 | + output = f"build/{basename}.html" if basename else "build/index.html" |
| 114 | + text = pathlib.Path(path).read_text() |
| 115 | + content = md.convert(text) |
| 116 | + html = template.render(content=content) |
| 117 | + pathlib.Path(output).write_text(html) |
| 118 | + print(f"Built {output}") |
| 119 | + |
| 120 | + |
| 121 | +@main.command() |
| 122 | +def serve(): |
| 123 | + with httpx.serve_http(web_server) as server: |
| 124 | + print(f"Serving on {server.url}") |
| 125 | + server.wait() |
| 126 | + |
| 127 | + |
| 128 | +@main.command() |
| 129 | +def deploy(): |
| 130 | + ghp_import.ghp_import( |
| 131 | + "build", |
| 132 | + mesg="Documentation deploy", |
| 133 | + remote="origin", |
| 134 | + branch="gh-pages", |
| 135 | + push=True, |
| 136 | + force=False, |
| 137 | + use_shell=False, |
| 138 | + no_history=False, |
| 139 | + nojekyll=True, |
| 140 | + ) |
| 141 | + print(f"Deployed to GitHub") |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + main() |
0 commit comments