123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- #!/usr/bin/env python3
- import chevron
- import datetime
- import os
- import markdown
- import shutil
- import sys
- import tempfile
- import yaml
- def main(projects_yaml, static_dir, template_dir, output_file=None):
- with open(projects_yaml) as f:
- project_data = yaml.safe_load(f)
- project_data['header'] = markdown.markdown(project_data['header'])
- for p in project_data['projects']:
- p['text'] = markdown.markdown(p['text'])
- for s in p.get('stuff', ()):
- s['text'] = markdown.markdown(s['text'])
- with open(os.path.join(static_dir, 'main.css')) as f:
- css = f.read()
- with open(os.path.join(static_dir, 'main.js')) as f:
- javascript = f.read()
- with open(os.path.join(template_dir, 'main.mustache')) as f:
- template = f.read()
- year = datetime.datetime.now().year
- index_html = chevron.render(
- template=template,
- data={
- 'css': css,
- 'javascript': javascript,
- 'project_data': project_data,
- 'copyright': f'© {year} Getty Ritter'
- },
- )
- if not output_file or output_file == '-':
- print(index_html)
- return
- with tempfile.TemporaryDirectory() as tmpdir:
- with open(os.path.join(tmpdir, 'index.html'), 'w') as f:
- print(index_html, file=f)
- shutil.copyfile(
- os.path.join(static_dir, 'raleway.ttf'),
- os.path.join(tmpdir, 'raleway.ttf'),
- )
- shutil.make_archive('output', 'zip', tmpdir)
- shutil.move('output.zip', output_file)
- if __name__ == '__main__':
- if len(sys.argv) not in (4, 5):
- print(
- f"{sys.argv[0]}: [project.yaml] [static/] [templates/] [output-zip]",
- file=sys.stderr,
- )
- sys.exit(1)
- main(*sys.argv[1:])
|