Improve cuddly #1

Merged
hannaeko merged 8 commits from improve-cuddly into main 2026-11-16 22:58:43 +01:00
2 changed files with 19 additions and 12 deletions
Showing only changes of commit fcc05b233c - Show all commits

add short options and possibility to use several --output

ariasuni 2026-11-08 02:15:45 +01:00
No known key found for this signature in database

View file

@ -26,6 +26,12 @@ Generating a HTML file
env/bin/cuddly --template samples/cv/cv.template.html --input samples/cv.kdl --output samples/cv.html env/bin/cuddly --template samples/cv/cv.template.html --input samples/cv.kdl --output samples/cv.html
``` ```
Generating both at the same time, with short options:
```
env/bin/cuddly -t samples/cv/cv.template.html -i samples/cv.kdl -o samples/cv.pdf -o samples/cv.html
```
### Template usage ### Template usage
The template language used is [Jinja](https://jinja.palletsprojects.com/en/3.0.x/), source template need to be a single HTML template file. The template language used is [Jinja](https://jinja.palletsprojects.com/en/3.0.x/), source template need to be a single HTML template file.

View file

@ -115,10 +115,10 @@ def stripscheme(url):
return url[scheme_len:] return url[scheme_len:]
@click.command() @click.command()
@click.option('--template', help="Path to a template file") @click.option('-t', '--template', help="Path to a template file")
@click.option('--input', help="Path to a KDL file") @click.option('-i', '--input', help="Path to a KDL file")
@click.option('--output', help="Output file") @click.option('-o', '--output', 'outputs', help="Output file", multiple=True)
def main(template, input, output): def main(template, input, outputs):
""" """
Template a file using a KDL input Template a file using a KDL input
""" """
@ -145,11 +145,12 @@ def main(template, input, output):
output_html = input_node.render(template) output_html = input_node.render(template)
if output.endswith('.pdf'): for output in outputs:
from weasyprint import HTML, default_url_fetcher if output.endswith('.pdf'):
base_dir = pathlib.Path(template_path).absolute().parent base_dir = pathlib.Path(template_path).absolute().parent
HTML(string=output_html, base_url=f'file://{base_dir}/').write_pdf(output, pdf_variant='pdf/ua-1') HTML(string=output_html, base_url=f'file://{base_dir}/').write_pdf(
output, pdf_variant='pdf/ua-1'
else: )
with open(output, 'w') as output_file: else:
output_file.write(output_html) with open(output, 'w') as output_file:
output_file.write(output_html)