mk-content.py (3137B)
1 #!/usr/bin/env python3 2 3 import yaml 4 import urllib.request 5 import requests 6 import shutil 7 import os 8 9 # TODO flag -v for verbose printing 10 # TODO flag -d for destination 11 12 # TODO write in tisp 13 with open('data/projects.yaml') as d: 14 data = yaml.load(d, Loader=yaml.FullLoader) 15 if not os.path.exists('content/projects'): 16 os.mkdir('content/projects') 17 print(f'creating script pages') 18 for gist in data['scripts']: 19 gid = gist['id'] 20 r = requests.get('https://api.github.com/gists/' + gid) 21 desc = r.json()['description'] 22 title = list(r.json()['files'])[0].split('.')[0].lower() 23 with open(f'content/projects/{title}.md', 'w') as f: 24 f.write(f'---\ntitle: {title}\ndescription: {desc}\n---\n') 25 for file in iter(r.json()['files'].values()): 26 name = file['filename'] 27 lang = file['language'].lower() 28 content = file['content'] 29 f.write(f'##### {name}:\n') 30 f.write(f'```{lang}\n{content}\n```\n') 31 # write button 32 # TODO view raw file 33 # with open(f'content/projects/{name}', 'w') as fp: 34 # fp.write(content) 35 print(f'creating project pages') 36 for code in data['code']: 37 if 'link' in code: 38 continue 39 name = code['name'] 40 desc = code['desc'] 41 with open(f'content/projects/{name}.md', 'w') as f: 42 f.write(f'---\ntitle: {name}\ndescription: {desc}\n---\n') 43 url = f'https://raw.githubusercontent.com/edvb/{name}/master/README.md' 44 if name == 'tisp': 45 url = 'https://raw.githubusercontent.com/edvb/tisp/master/doc/tisp.7.md' 46 with urllib.request.urlopen(url) as response, open(f'content/projects/{name}.md', 'ab') as f: 47 for chunk in iter(lambda: response.read(1), ''): 48 if chunk == b'': 49 break 50 response.seek(0) 51 url = f'https://raw.githubusercontent.com/edvb/{name}/master/' + response.read() 52 with urllib.request.urlopen(url) as response: 53 shutil.copyfileobj(response, f) 54 if chunk == b'\n': 55 break 56 shutil.copyfileobj(response, f) 57 58 # TODO replace with parital in layout page 59 # https://gohugo.io/templates/lookup-order/ 60 print(f'creating photo pages') 61 with open('data/photos.yaml') as d: 62 data = yaml.load(d, Loader=yaml.FullLoader) 63 if not os.path.exists('content/photos'): 64 os.mkdir('content/photos') 65 with open('content/photos/_index.md', 'w') as f: 66 f.write('---\ntitle: photos\n---\n') 67 f.write(f'{{{{< photos limit="999" dir="best">}}}}\n') 68 for album in data['photos']: 69 f.write(f'{{{{< photos limit="5" dir="{album["name"]}">}}}}\n') 70 for album in data['photos']: 71 title = album['name'] 72 name = title.replace(' ', '-') 73 with open(f'content/photos/{name}.md', 'w') as f: 74 f.write(f'---\ntitle: {title}\n---\n') 75 f.write(f'{{{{< photos limit="999" dir="{title}">}}}}\n')