equations.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 from pathlib import Path
3 import re
4 import requests
5 
6 
7 def clean(s):
8  new_s = ''
9  for c in s:
10  if c in ' \\(){}':
11  continue
12  elif c.isalnum():
13  new_s += c
14  else:
15  new_s += '_'
16  return new_s
17 
18 
19 EQ_PATTERN = re.compile(r'!eq\[([^\]]+)\]')
20 doc_folder = Path('doc')
21 src_files = [p for p in doc_folder.iterdir() if p.suffix == '.md']
22 existing_gifs = [p for p in doc_folder.iterdir() if p.suffix == '.gif']
23 gifs_to_remove = set(existing_gifs)
24 
25 for src_file in src_files:
26  print(src_file.stem)
27  with open(src_file) as f:
28  s = f.read()
29  m = EQ_PATTERN.search(s)
30  while m:
31  equation = m.group(1)
32  img_filename = doc_folder / (clean(equation) + '.gif')
33  if not img_filename.exists():
34  url = 'http://latex.codecogs.com/gif.download?' + equation
35  print('\t{} => {}'.format(m.group(0), img_filename))
36  img = requests.get(url)
37  with open(img_filename, 'wb') as f:
38  f.write(img.content)
39  if img_filename in gifs_to_remove:
40  gifs_to_remove.remove(img_filename)
41  s = s.replace(m.group(0), '![%s](%s)' % (equation, img_filename))
42  m = EQ_PATTERN.search(s)
43 
44  with open(src_file.name, 'w') as f:
45  f.write(s)
46 
47 for gif_to_remove in gifs_to_remove:
48  print('Deleting {}'.format(gif_to_remove))
49  gif_to_remove.unlink()
def clean(s)
Definition: equations.py:7


dlux_global_planner
Author(s):
autogenerated on Wed Jun 26 2019 20:06:30