forked from gto76/python-cheatsheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_index.py
executable file
·51 lines (41 loc) · 1.12 KB
/
create_index.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
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
#
# Usage: .py
#
from collections import namedtuple
from dataclasses import make_dataclass
from enum import Enum
import re
import sys
from bs4 import BeautifulSoup
from collections import defaultdict
def main():
html = read_file('index.html')
doc = BeautifulSoup(''.join(html), 'html.parser')
hhh = defaultdict(lambda: defaultdict(list))
for i in range(2, 5):
for h in doc.find_all(f'h{i}'):
an_id = h.attrs['id']
text = h.text.lstrip('#')
first_letter = text[0]
hhh[first_letter][text].append(an_id)
print_hhh(hhh)
def print_hhh(hhh):
letters = hhh.keys()
for letter in sorted(letters):
hh = hhh[letter]
print(f'### {letter}')
commands = hh.keys()
for command in sorted(commands):
links = hh[command]
lll = ', '.join(f'[1](#{l})' for l in links)
print(f'**{command} {lll}** ')
print()
###
## UTIL
#
def read_file(filename):
with open(filename, encoding='utf-8') as file:
return file.readlines()
if __name__ == '__main__':
main()