|
| 1 | +from pathlib import Path |
| 2 | +import shutil |
| 3 | +from typing import List |
| 4 | +import subprocess |
| 5 | +import tempfile |
| 6 | + |
| 7 | +import chevron |
| 8 | +import click |
| 9 | +import toml |
| 10 | +from pydantic import BaseModel |
| 11 | +from jinja2 import Environment, PackageLoader, select_autoescape |
| 12 | +from jinja2 import Template |
| 13 | + |
| 14 | +def run_inkscape(input: str, output: str): |
| 15 | + click.echo(f"Running inkscape to produce {output}") |
| 16 | + subprocess.run(['inkscape', '--export-type=png', f'--export-filename={output}', input]) |
| 17 | + |
| 18 | + |
| 19 | +class DigitalMeetup(BaseModel): |
| 20 | + class Meta(BaseModel): |
| 21 | + template: str |
| 22 | + |
| 23 | + class Instance(BaseModel): |
| 24 | + year: str |
| 25 | + date: str |
| 26 | + time: str |
| 27 | + |
| 28 | + meta: Meta |
| 29 | + instances: List[Instance] |
| 30 | + |
| 31 | +class Posters(BaseModel): |
| 32 | + digital_meetup: DigitalMeetup |
| 33 | + |
| 34 | + |
| 35 | +class Output(BaseModel): |
| 36 | + title: str |
| 37 | + path: str |
| 38 | + |
| 39 | + |
| 40 | +def handle_digital_meetup(digital_meetup: DigitalMeetup) -> List[Output]: |
| 41 | + with open(digital_meetup.meta.template, 'r') as reader: |
| 42 | + template = reader.read() |
| 43 | + |
| 44 | + outputs = [] |
| 45 | + |
| 46 | + for instance in digital_meetup.instances: |
| 47 | + with tempfile.NamedTemporaryFile(suffix=".svg") as fp: |
| 48 | + rendered = chevron.render( |
| 49 | + template=template, |
| 50 | + data=instance |
| 51 | + ) |
| 52 | + parent_dir = f"./build/{instance.year}/digital-meetup/" |
| 53 | + Path(parent_dir).mkdir(parents=True, exist_ok=True) |
| 54 | + |
| 55 | + fp.write(rendered.encode('utf-8')) |
| 56 | + |
| 57 | + output_filename = f"{parent_dir}digital-meetup-{instance.year}-{instance.date}-{instance.time}.png" |
| 58 | + run_inkscape(fp.name, output_filename) |
| 59 | + |
| 60 | + outputs.append(Output( |
| 61 | + title=f"Digital Meetup ({instance.year}) {instance.date} @ {instance.time}", |
| 62 | + path=output_filename.lstrip("./build") |
| 63 | + )) |
| 64 | + |
| 65 | + return outputs |
| 66 | + |
| 67 | +@click.command() |
| 68 | +def socials(): |
| 69 | + """Generates social content""" |
| 70 | + click.echo("Generating socials content...") |
| 71 | + |
| 72 | + with open('posters.toml', 'r') as reader: |
| 73 | + posters_toml_string = reader.read() |
| 74 | + |
| 75 | + posters_toml = toml.loads(posters_toml_string) |
| 76 | + |
| 77 | + posters = Posters(**posters_toml) |
| 78 | + |
| 79 | + outputs = [ |
| 80 | + *handle_digital_meetup(posters.digital_meetup) |
| 81 | + ] |
| 82 | + |
| 83 | + template = Template(""" |
| 84 | +<!DOCTYPE html> |
| 85 | +<html> |
| 86 | +<head> |
| 87 | + <title>CTSNL Socials</title> |
| 88 | + <style> |
| 89 | +body { |
| 90 | + font-family: sans-serif; |
| 91 | + margin: 4rem; |
| 92 | +} |
| 93 | +.output { |
| 94 | + margin: 2rem 0; |
| 95 | +} |
| 96 | +.output img { |
| 97 | + max-width: 40rem; |
| 98 | +} |
| 99 | + </style> |
| 100 | +<head> |
| 101 | +<body> |
| 102 | + <h1>CTSNL Socials</h1> |
| 103 | + <p>Automagically generating social assets</p> |
| 104 | + <a href="https://github.com/CTS-NL/Socials">https://github.com/CTS-NL/Socials</a> |
| 105 | + {% for output in outputs %} |
| 106 | + <div class="output"> |
| 107 | + <h2>{{ output.title }}</h2> |
| 108 | + <img src="{{ output.path }}" /> |
| 109 | + </div> |
| 110 | + {% endfor %} |
| 111 | +</body> |
| 112 | +</html> |
| 113 | + """).render(outputs=outputs) |
| 114 | + |
| 115 | + Path("./build/index.html").write_text(template) |
| 116 | + Path("./build/posters.json").write_text(posters.json(indent=2)) |
| 117 | + shutil.copytree("./templates", "./build/templates", dirs_exist_ok=True) |
| 118 | + |
| 119 | + click.echo("Generating social content") |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + socials() |
0 commit comments