-
Notifications
You must be signed in to change notification settings - Fork 16
/
generate_refs.py
32 lines (27 loc) · 1.02 KB
/
generate_refs.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
import re
from collections import defaultdict
from pathlib import Path
base_url = 'https://developer.mozilla.org/en-US/docs/Web/API/'
doc_base_url = 'https://pkg.go.dev/github.com/life4/gweb/{package}#{obj}'
link = re.escape(f'// {base_url}')
rex = re.compile(rf'(?:{link}([a-zA-Z/-]+))+\nfunc \([a-z]+ \*?([a-zA-Z]+)\) ([a-zA-Z]+)')
refs: dict = defaultdict(list)
for path in Path().glob('*/*.go'):
content = path.read_text()
for match in rex.findall(content):
*links, struct, func = match
for link in links:
refs[link].append((path.parent.name, f'{struct}.{func}'))
print("""
# Reference
Below is the mapping of web API to gweb functions.
This file is autogenerated, so some references may be missed.
| Web API | gweb |
| ------- | ---- |
""".strip())
for ref, objects in sorted(refs.items()):
url = base_url + ref
ref = ref.replace('/', '.')
for package, obj in objects:
doc_url = doc_base_url.format(package=package, obj=obj)
print(f'| [{ref}]({url}) | [{obj}]({doc_url}) |')