Add support for other paper sizes than A4 (--size and --orientation), make custom css file optional, update README
This commit is contained in:
parent
010676b495
commit
26373ce09a
2 changed files with 39 additions and 7 deletions
10
README.md
10
README.md
|
@ -4,9 +4,17 @@
|
||||||
```html
|
```html
|
||||||
<div class="sheet">
|
<div class="sheet">
|
||||||
<h1>Hello</h1>
|
<h1>Hello</h1>
|
||||||
<p>World</p>
|
<p>{{ name }}</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="sheet">
|
<div class="sheet">
|
||||||
<h1>Second Page</h1>
|
<h1>Second Page</h1>
|
||||||
</div>
|
</div>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Data Example
|
||||||
|
|
||||||
|
```yml
|
||||||
|
title: Example Document
|
||||||
|
lang: en
|
||||||
|
name: World
|
||||||
|
```
|
||||||
|
|
32
generate.py
32
generate.py
|
@ -62,12 +62,13 @@ BASE_TEMPLATE = Template(
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>{{ title }}</title>
|
<title>{{ title }}</title>
|
||||||
</head>
|
</head>
|
||||||
<body class="A4">
|
<body class="{{ paper_size }} {{ orientation }}">
|
||||||
<style>
|
<style>
|
||||||
/* paper.css -> https://github.com/cognitom/paper-css */
|
/* paper.css -> https://github.com/cognitom/paper-css */
|
||||||
{{ paper_css }}
|
{{ paper_css }}
|
||||||
|
/* predefined */
|
||||||
|
@page { size: {{ paper_size }} {{ orientation }}; }
|
||||||
/* own rules */
|
/* own rules */
|
||||||
@page { size: A4; }
|
|
||||||
{{ css }}
|
{{ css }}
|
||||||
</style>
|
</style>
|
||||||
{{ content }}
|
{{ content }}
|
||||||
|
@ -86,17 +87,32 @@ if __name__ == "__main__":
|
||||||
"-s", "--css",
|
"-s", "--css",
|
||||||
help="The path to the css file",
|
help="The path to the css file",
|
||||||
metavar="PATH",
|
metavar="PATH",
|
||||||
type=Path, required=True)
|
type=Path)
|
||||||
argp.add_argument(
|
argp.add_argument(
|
||||||
"-d", "--data",
|
"-d", "--data",
|
||||||
help="The path to the yaml file containing the variables used in the templates",
|
help="The path to the yaml file containing the variables used in the templates",
|
||||||
metavar="PATH",
|
metavar="PATH",
|
||||||
type=Path, required=True)
|
type=Path, required=True)
|
||||||
|
argp.add_argument(
|
||||||
|
"--size",
|
||||||
|
help="The paper size (default: A4)",
|
||||||
|
choices=["A3", "A4", "A5", "letter", "legal"],
|
||||||
|
default="A4"
|
||||||
|
)
|
||||||
|
argp.add_argument(
|
||||||
|
"--orientation",
|
||||||
|
help="The page orientation (default: portrait)",
|
||||||
|
choices=["portrait", "landscape"],
|
||||||
|
default="portrait"
|
||||||
|
)
|
||||||
args = argp.parse_args()
|
args = argp.parse_args()
|
||||||
# load data
|
# load data
|
||||||
with args.data.open("r") as f:
|
with args.data.open("r") as f:
|
||||||
data = safe_load(f)
|
data = safe_load(f)
|
||||||
# load css
|
# load css
|
||||||
|
if args.css is None:
|
||||||
|
css = "/* empty */"
|
||||||
|
else:
|
||||||
with args.css.open("r") as f:
|
with args.css.open("r") as f:
|
||||||
css = f.read()
|
css = f.read()
|
||||||
# template content
|
# template content
|
||||||
|
@ -108,5 +124,13 @@ if __name__ == "__main__":
|
||||||
# render
|
# render
|
||||||
content = content_template.render(data)
|
content = content_template.render(data)
|
||||||
# template out complete html document
|
# template out complete html document
|
||||||
result = BASE_TEMPLATE.render(lang=data["lang"], title=data["title"], paper_css=PAPER_CSS, css=css, content=content)
|
result = BASE_TEMPLATE.render(
|
||||||
|
lang=data["lang"],
|
||||||
|
title=data["title"],
|
||||||
|
paper_size=args.size,
|
||||||
|
orientation=args.orientation,
|
||||||
|
paper_css=PAPER_CSS,
|
||||||
|
css=css,
|
||||||
|
content=content
|
||||||
|
)
|
||||||
print(result)
|
print(result)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue