Skip to content

Commit 7c8334a

Browse files
committed
Resolve config variables against config values
No more special case for variant filename
1 parent 0bcd3f2 commit 7c8334a

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ wheels/
1818
# Backup files
1919
**.~
2020
*.swp
21+
22+
# Example build/dist directories
23+
examples/*/build/
24+
examples/*/dist/

src/pdfbaker/document.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ def _load_config(
6161

6262
def process(self) -> Path:
6363
"""Process the page from SVG template to PDF."""
64+
if "template" not in self.config:
65+
raise PDFBakeError(
66+
f'Page "{self.name}" in document "{self.document.name}" has no template'
67+
)
68+
6469
output_filename = f"{self.config['filename']}_{self.number:03}"
6570
svg_path = self.document.build_dir / f"{output_filename}.svg"
6671
pdf_path = self.document.build_dir / f"{output_filename}.pdf"
@@ -148,6 +153,13 @@ def process_document(self) -> None:
148153
else:
149154
self.process()
150155

156+
def _resolve_config(self, config: dict) -> dict:
157+
"""Resolve all template strings in config using its own values."""
158+
yaml_str = yaml.dump(config)
159+
template = Template(yaml_str)
160+
resolved_yaml = template.render(**config)
161+
return yaml.safe_load(resolved_yaml)
162+
151163
def process(self) -> None:
152164
"""Process document using standard processing."""
153165
doc_config = self.config.copy()
@@ -156,19 +168,13 @@ def process(self) -> None:
156168
# Multiple PDF documents
157169
for variant in self.config["variants"]:
158170
self.baker.info("Processing variant: %s", variant["name"])
159-
160-
# Customise config for variant
161171
variant_config = deep_merge(doc_config, variant)
162172
variant_config["variant"] = variant
163-
164-
# Render filename template
165-
template = Template(variant_config["filename"])
166-
filename = template.render(**variant_config)
167-
variant_config.update(filename=filename)
168-
173+
variant_config = self._resolve_config(variant_config)
169174
self._process_pages(variant_config)
170175
else:
171176
# Single PDF document
177+
doc_config = self._resolve_config(doc_config)
172178
self._process_pages(doc_config)
173179

174180
def _process_pages(self, config: dict[str, Any]) -> None:

0 commit comments

Comments
 (0)