equations.py
Go to the documentation of this file.
00001 #!/usr/bin/env python3
00002 from pathlib import Path
00003 import re
00004 import requests
00005 
00006 
00007 def clean(s):
00008     new_s = ''
00009     for c in s:
00010         if c in ' \\(){}':
00011             continue
00012         elif c.isalnum():
00013             new_s += c
00014         else:
00015             new_s += '_'
00016     return new_s
00017 
00018 
00019 EQ_PATTERN = re.compile(r'!eq\[([^\]]+)\]')
00020 doc_folder = Path('doc')
00021 src_files = [p for p in doc_folder.iterdir() if p.suffix == '.md']
00022 existing_gifs = [p for p in doc_folder.iterdir() if p.suffix == '.gif']
00023 gifs_to_remove = set(existing_gifs)
00024 
00025 for src_file in src_files:
00026     print(src_file.stem)
00027     with open(src_file) as f:
00028         s = f.read()
00029     m = EQ_PATTERN.search(s)
00030     while m:
00031         equation = m.group(1)
00032         img_filename = doc_folder / (clean(equation) + '.gif')
00033         if not img_filename.exists():
00034             url = 'http://latex.codecogs.com/gif.download?' + equation
00035             print('\t{} => {}'.format(m.group(0), img_filename))
00036             img = requests.get(url)
00037             with open(img_filename, 'wb') as f:
00038                 f.write(img.content)
00039         if img_filename in gifs_to_remove:
00040             gifs_to_remove.remove(img_filename)
00041         s = s.replace(m.group(0), '![%s](%s)' % (equation, img_filename))
00042         m = EQ_PATTERN.search(s)
00043 
00044     with open(src_file.name, 'w') as f:
00045         f.write(s)
00046 
00047 for gif_to_remove in gifs_to_remove:
00048     print('Deleting {}'.format(gif_to_remove))
00049     gif_to_remove.unlink()


dlux_global_planner
Author(s):
autogenerated on Wed Jun 26 2019 20:09:53