generate.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 main(projects_yaml, static_dir, template_dir, output_file=None):
  11. with open(projects_yaml) as f:
  12. project_data = yaml.safe_load(f)
  13. project_data['header'] = markdown.markdown(project_data['header'])
  14. for p in project_data['projects']:
  15. p['text'] = markdown.markdown(p['text'])
  16. for s in p.get('stuff', ()):
  17. s['text'] = markdown.markdown(s['text'])
  18. with open(os.path.join(static_dir, 'main.css')) as f:
  19. css = f.read()
  20. with open(os.path.join(static_dir, 'main.js')) as f:
  21. javascript = f.read()
  22. with open(os.path.join(template_dir, 'main.mustache')) as f:
  23. template = f.read()
  24. year = datetime.datetime.now().year
  25. index_html = chevron.render(
  26. template=template,
  27. data={
  28. 'css': css,
  29. 'javascript': javascript,
  30. 'project_data': project_data,
  31. 'copyright': f'© {year} Getty Ritter'
  32. },
  33. )
  34. if not output_file or output_file == '-':
  35. print(index_html)
  36. return
  37. with tempfile.TemporaryDirectory() as tmpdir:
  38. with open(os.path.join(tmpdir, 'index.html'), 'w') as f:
  39. print(index_html, file=f)
  40. shutil.copyfile(
  41. os.path.join(static_dir, 'raleway.ttf'),
  42. os.path.join(tmpdir, 'raleway.ttf'),
  43. )
  44. shutil.make_archive('output', 'zip', tmpdir)
  45. shutil.move('output.zip', output_file)
  46. if __name__ == '__main__':
  47. if len(sys.argv) not in (4, 5):
  48. print(
  49. f"{sys.argv[0]}: [project.yaml] [static/] [templates/] [output-zip]",
  50. file=sys.stderr,
  51. )
  52. sys.exit(1)
  53. main(*sys.argv[1:])