generate.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. import chevron
  3. import datetime
  4. import os
  5. import markdown
  6. import shutil
  7. import sys
  8. import tempfile
  9. import yaml
  10. def find_asset(name, assets):
  11. for a in assets:
  12. if os.path.basename(a) == name:
  13. return a
  14. raise Exception(f"Unable to find {name} in assets")
  15. def main(output_file, projects_yaml, *assets):
  16. with open(projects_yaml) as f:
  17. project_data = yaml.safe_load(f)
  18. project_data['header'] = markdown.markdown(project_data['header'])
  19. for p in project_data['projects']:
  20. p['text'] = markdown.markdown(p['text'])
  21. for s in p.get('stuff', ()):
  22. s['text'] = markdown.markdown(s['text'])
  23. with open(find_asset('main.css', assets)) as f:
  24. css = f.read()
  25. with open(find_asset('main.js', assets)) as f:
  26. javascript = f.read()
  27. with open(find_asset('main.mustache', assets)) as f:
  28. template = f.read()
  29. year = datetime.datetime.now().year
  30. index_html = chevron.render(
  31. template=template,
  32. data={
  33. 'css': css,
  34. 'javascript': javascript,
  35. 'project_data': project_data,
  36. 'copyright': f'© {year} Getty Ritter'
  37. },
  38. )
  39. if not output_file or output_file == '-':
  40. print(index_html)
  41. return
  42. with tempfile.TemporaryDirectory() as tmpdir:
  43. with open(os.path.join(tmpdir, 'index.html'), 'w') as f:
  44. print(index_html, file=f)
  45. shutil.copyfile(
  46. find_asset('raleway.ttf', assets),
  47. os.path.join(tmpdir, 'raleway.ttf'),
  48. )
  49. shutil.make_archive('output', 'zip', tmpdir)
  50. shutil.move('output.zip', output_file)
  51. if __name__ == '__main__':
  52. main(*sys.argv[1:])