forked from pinout-xyz/Pinout.xyz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlmapper.py
executable file
·103 lines (68 loc) · 2.48 KB
/
urlmapper.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
import glob
import os
import re
import sys
import unicodedata
import markjaml
import pinout
reload(sys)
sys.setdefaultencoding('utf8')
def url_slugify(value):
"""
Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to hyphens.
"""
value = unicode(value)
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub('[^\w\s-]', '', value).strip().lower()
return re.sub('[-\s]+', '_', value)
def load_overlay_url(overlay, lang):
try:
data = markjaml.load('src/{}/overlay/{}.md'.format(lang, overlay))
loaded = data['data']
except IOError:
try:
data = markjaml.load('src/{}/translate/{}.md'.format(lang, overlay))
loaded = data['data']
except IOError:
return None
if not 'page_url' in loaded:
loaded['page_url'] = url_slugify(loaded['name'])
return overlay, loaded['page_url']
def get_pin_url(pin_num, pinout):
pin = pinout.pins[str(pin_num)]
pin_url = pin['name']
if pin_url == 'Ground':
return None
if 'scheme' in pin:
if 'bcm' in pin['scheme']:
bcm = pin['scheme']['bcm']
pin_url = 'gpio{}'.format(bcm)
return url_slugify('pin{}_{}'.format(pin_num, pin_url))
def generate_for_lang(lang="en"):
url_lookup = {}
pinout.load(lang)
overlays = pinout.settings['overlays']
base_url = pinout.get_setting('base_url', '/pinout/')
domain = pinout.get_setting('domain', 'pinout.xyz')
overlays_html = ''
overlays = map(lambda overlay: load_overlay_url(overlay, lang), overlays)
for pin in range(1, len(pinout.pins)+1):
pin_url = get_pin_url(pin, pinout)
if pin_url is None:
continue
url_lookup['pin{}'.format(pin)] = '//{domain}{base_url}{url}'.format(domain=domain, base_url=base_url, url=pin_url)
for url in overlays:
if url is None:
continue
url_lookup['{}'.format(url[0])] = '//{domain}{base_url}{url}'.format(domain=domain, base_url=base_url, url=url[1])
url_lookup['index'] = '//{}'.format(domain)
url_lookup['boards'] = '//{}/boards'.format(domain)
return url_lookup
def generate_urls(lang="en"):
languages = [l.replace('src/', '') for l in glob.glob('src/??') if os.path.isdir(l)] # if not l == 'src/'+lang
urls = {}
for lang in languages:
urls[lang] = generate_for_lang(lang)
return urls