mk-content.py (4265B)
1 #!/usr/bin/env python3 2 3 import yaml 4 import urllib.request 5 import requests 6 import shutil 7 import os 8 import chevron 9 from pathlib import Path 10 11 # TODO flag -v for verbose printing 12 # TODO flag -d for destination 13 14 # TODO write in eevo 15 with open('data/projects.yaml') as d: 16 data = yaml.load(d, Loader=yaml.FullLoader) 17 if not os.path.exists('content/projects'): 18 os.mkdir('content/projects') 19 print(f'creating script pages') 20 for gist in data['scripts']: 21 gid = gist['id'] 22 r = requests.get('https://api.github.com/gists/' + gid) 23 desc = r.json()['description'] 24 title = list(r.json()['files'])[0].split('.')[0].lower() 25 with open(f'content/projects/{title}.md', 'w') as f: 26 f.write(f'---\ntitle: {title}\ndescription: {desc}\n---\n') 27 for file in iter(r.json()['files'].values()): 28 name = file['filename'] 29 lang = file['language'].lower() 30 content = file['content'] 31 f.write(f'##### {name}:\n') 32 f.write(f'```{lang}\n{content}\n```\n') 33 # write button 34 # TODO view raw file 35 # with open(f'content/projects/{name}', 'w') as fp: 36 # fp.write(content) 37 print(f'creating project pages') 38 for code in data['code']: 39 if 'link' in code: 40 continue 41 name = code['name'] 42 desc = code['desc'] 43 with open(f'content/projects/{name}.md', 'w') as f: 44 f.write(f'---\ntitle: {name}\ndescription: {desc}\n---\n') 45 url = f'https://raw.githubusercontent.com/edvb/{name}/master/README.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 def gen_galleries(input_file): 59 # Read the input YAML file 60 with open(input_file, 'r') as f: 61 data = yaml.safe_load(f) 62 63 galleries = [] 64 65 # Process each gallery 66 for gallery in data['galleries']: 67 title = gallery['title'] 68 date = gallery['date'] 69 folder = "photos/" + str(date) + "/" + title.lower().replace(' ', '-') 70 71 # Get all image files from the folder with the same name as title 72 folder_path = "content/" / Path(folder) 73 images = [folder + "/" + f.name for f in folder_path.iterdir() 74 # Only include images, not index pages 75 if f.is_file() and f.suffix != '.md'] 76 77 # Create new YAML structure 78 new_gallery = { 79 'title': title, 80 'date': date, 81 'link': folder, 82 # TODO cover or first image 83 'cover': folder + "/" + gallery['cover'], 84 'images': images 85 } 86 87 # Add to galleries index 88 galleries.append(new_gallery) 89 90 return galleries 91 92 def transform_galleries(input_file, templ_index, templ_gallery): 93 galleries = gen_galleries(input_file) 94 95 os.makedirs('content/photos', exist_ok=True) 96 print('creating content/photos/_index.md') 97 with open(templ_index, 'r') as templ, \ 98 open('content/photos/_index.md', 'w') as f: 99 f.write(chevron.render(templ, { 'galleries': galleries })) 100 101 for gallery in galleries: 102 with open(templ_gallery, 'r') as templ: 103 os.makedirs(f'content/photos/{gallery["date"]}', exist_ok=True) 104 # TODO make content/photos/{date}/_index.md for all photos that year, 105 # with list on galleries page for each year 106 print(f'creating content/{gallery["link"]}/_index.md') 107 with open(f'content/{gallery["link"]}/_index.md', 'w') as f: 108 f.write(chevron.render(templ, gallery)) 109 110 print('creating photo pages') 111 transform_galleries('data/galleries.yaml', 112 'templ/index.md', 'templ/gallery.md')