Skip to content

Commit cdb93af

Browse files
Docs fixup (#69)
Linking fixes.
1 parent 20d1cff commit cdb93af

File tree

7 files changed

+163
-69
lines changed

7 files changed

+163
-69
lines changed

LICENSE.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

docs/index.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ Lets get to work...
4545
>>> with httpx.open_client() as client:
4646
... r = client.get('https://www.example.org/')
4747
...
48-
>>> print(r)
48+
>>> r
4949
<Response [200 OK]>
50-
>>> print(r.code)
50+
>>> r.code
5151
200
52-
>>> print(r.headers['content-type'])
52+
>>> r.headers['content-type']
5353
'text/html; charset=UTF-8'
54-
>>> print(r.text)
54+
>>> r.text
5555
'<!doctype html>\n<html>\n<head>\n<title>Example Domain</title>...'
5656
```
5757

@@ -65,9 +65,10 @@ We can also handle the server side...
6565
... return httpx.Response(code=200, content=content)
6666
...
6767
>>> with httpx.serve_http(hello_world) as server:
68-
... print(f"Serving on {server.url}")
68+
... print(f"Serving on {server.url} (Press CTRL+C to quit)")
6969
... server.wait()
7070
...
71+
Serving on http://127.0.0.1:8080/ (Press CTRL+C to quit)
7172
```
7273

7374
---
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<!-- saved from url=(0016)https://dab.eco/ -->
33
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4-
4+
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<link rel="icon" href="data:image/svg+xml,&lt;svg xmlns=&#39;http://www.w3.org/2000/svg&#39; viewBox=&#39;0 0 100 100&#39;&gt;&lt;text y=&#39;.9em&#39; font-size=&#39;90&#39;&gt;%F0%9F%8C%B1&lt;/text&gt;&lt;/svg&gt;">
77
<title>httpx</title>
@@ -107,7 +107,7 @@
107107
</head>
108108
<body>
109109
<main>
110-
{{ page.content }}
110+
{{ content }}
111111
</main>
112112

113113
</body></html>

mkdocs.yml

Lines changed: 0 additions & 17 deletions
This file was deleted.

requirements.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
-e .
22

3-
Test & build requirements...
3+
Build...
44
build==1.2.2
55
mypy==1.13.0
6+
7+
# Test...
68
pytest==8.3.4
79
pytest-cov==6.0.0
10+
11+
# Sync & Async mirroring...
812
unasync==0.6.0
9-
mkdocs==1.6.1
13+
14+
# Documentation...
15+
click==8.1.8
16+
jinja2==3.0.2
17+
markdown==3.8

scripts/docs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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()

scripts/docstrings

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)