-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathfix_pythonfluente_urls.py
More file actions
executable file
·67 lines (46 loc) · 1.49 KB
/
Copy pathfix_pythonfluente_urls.py
File metadata and controls
executable file
·67 lines (46 loc) · 1.49 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
"""
In the volume 1 *.adoc files, find links in this form:
```
fpy.li/2p?{ref}[{text}] (vol. {vol}, cap. {ch_num})
```
Replace them with:
```
_{text} (vol.{vol}, cap.{ch_num}, fpy.li/{su})_
```
(vol.2, cap.13, fpy.li/q7)
Where {su} is the short URL for a URL to `pythonfluente.com`, such as:
```
https://pythonfluente.com/2/#{ref}
```
# Steps
1. Edited chapters with those links to isolate the few relevant links into
their own lines.
2. Used `grep fpy.li/2p 1/*.adoc > linkspyfl-web.txt` to follect those links.
3. Wrote list_pyfl_links(), use it to write `pyfl_refs.txt`
```
./fix_pythonfluente_urls.py > pyfl_refs.txt
```
4. Sent `pyfl_refs.txt` to `short.py` from the
[Fluent Python example code repo](https://github.com/fluentpython/example-code-2e/tree/cf996500070e0d712a3b088d29428f74ddc9fa1f/links)
"""
import re
ADOC_LINK_RE = re.compile(r'(.*?)\[(.*)]')
PYFL_BASE = 'https://pythonfluente.com/2/'
def list_pyfl_links():
"""Make list of links to PythonFluente.com"""
with open('links-pyfl-web.txt') as fp:
lines = fp.readlines()
for line in lines:
chapter_path, link = line.split(':', maxsplit=1)
res = ADOC_LINK_RE.match(link)
if res is None:
print('NO MATCH:', line)
continue
else:
url, text = res.groups()
_, ref = url.split('?')
#print(ref, text, sep='\t')
print(PYFL_BASE + '#' + ref)
if __name__ == '__main__':
list_pyfl_links()