verilog: add sv_maps iterators
[ghdl-vlg.git] / doc / helpers.py
blob5d099e72999f2f2db63fa5638be27891838a7934
1 from os.path import dirname, join
2 import json
3 import re
5 # Try to load JSON data from a file. If not found, use the argument as a tag name and retrieve the data from GitHub.
6 def getJSON(tag='all'):
7 f = tag
8 tag = '/'+tag
9 if f == 'all':
10 f = 'releases'
11 tag = ''
13 try:
14 d = json.loads(open(join(dirname(__file__), f+'.json'), 'r').read())
15 except:
16 from urllib.request import urlopen
17 d = json.loads(urlopen('https://api.github.com/repos/ghdl/ghdl/releases'+tag).read())
18 json.dump(d, open(f+'.json', 'w'), indent=4)
19 return d
22 # Print two versions of each shield. Onee for 'html' (`image::`) and one for 'latex' (`replace::`)
25 def printShieldSrc(label, alt, img, target, latex=False):
26 if latex:
27 if label[-6:] == '/total':
28 label = label[:-6]
29 idx = re.compile('[\W_]+').sub('', label)
30 print(f"""
31 .. |l{idx}| replace:: `{label}`_
32 .. _{label}: {target}
33 """)
34 else:
35 print(f'''
36 .. |{label}| image:: {img}
37 :target: {target}
38 :height: 22
39 :alt: {alt}
40 :class: shield
41 ''')
44 # Create shields/badges from JSON file
47 def createShields(file='shields'):
48 shields = getJSON(file)
49 for k, v in shields.items():
50 t = v['target']
51 if t[0:3] == 'gh:':
52 t = 'https://github.com/' + t[3:]
54 printShieldSrc(
55 'SHIELD:'+k,
56 v['alt'],
57 'https://img.shields.io/' + v['src'] + '&style=flat-square&longCache=true',
59 False