@@ -80,25 +80,47 @@ def __init__(
8080 self .build_dir = self ["directories" ]["build" ] / self .name
8181 self .dist_dir = self ["directories" ]["dist" ] / self .name
8282
83+ # The "pages" may be defined in the variants rather than
84+ # the document itself (when different variants have different pages)
8385 if "pages" not in self :
86+ if "variants" in self :
87+ # A variant not defining pages will fail to process
88+ self .document .log_debug (
89+ 'Pages of document "%s" will be determined per variant' ,
90+ self .name ,
91+ )
92+ else :
93+ self .document .log_warning (
94+ f'Document "{ self .name } " has neither "pages" nor "variants"'
95+ )
96+ raise ConfigurationError (
97+ f'Cannot determine pages of document "{ self .name } "'
98+ )
99+ # Actual pages will be determined during processing
100+ self .pages = []
101+
102+ def determine_pages (self , config : dict [str , Any ]) -> list [Path ]:
103+ """Determine pages for the give (document/variant) configuration."""
104+ if "pages" not in config :
84105 raise ConfigurationError (
85- 'Document "{document .name}" is missing key "pages "'
106+ f'Cannot determine pages for " { self .name } "'
86107 )
87- self . pages = []
88- for page_spec in self ["pages" ]:
108+ pages = []
109+ for page_spec in config ["pages" ]:
89110 if isinstance (page_spec , dict ) and "path" in page_spec :
90- # Path was specified: relative to the config file
111+ # Path was specified: relative to this config file
91112 page = self .resolve_path (
92- page_spec ["path" ], directory = self ["directories" ]["config" ]
113+ page_spec ["path" ], directory = config ["directories" ]["config" ]
93114 )
94115 else :
95116 # Only name was specified: relative to the pages directory
96117 page = self .resolve_path (
97- page_spec , directory = self ["directories" ]["pages" ]
118+ page_spec , directory = config ["directories" ]["pages" ]
98119 )
99120 if not page .suffix :
100121 page = page .with_suffix (".yaml" )
101- self .pages .append (page )
122+ pages .append (page )
123+ self .pages = pages
102124
103125 def __init__ (
104126 self ,
@@ -163,6 +185,7 @@ def process(self) -> Path | list[Path]:
163185 for variant in self .config ["variants" ]:
164186 self .log_info_subsection ('Processing variant "%s"...' , variant ["name" ])
165187 variant_config = deep_merge (self .config , variant )
188+ self .log_trace (variant_config )
166189 variant_config ["variant" ] = variant
167190 variant_config = render_config (variant_config )
168191 page_pdfs = self ._process_pages (variant_config )
@@ -176,9 +199,10 @@ def process(self) -> Path | list[Path]:
176199
177200 def _process_pages (self , config : dict [str , Any ]) -> list [Path ]:
178201 """Process pages with given configuration."""
179- pdf_files = []
202+ self . config . determine_pages ( config )
180203 self .log_debug_subsection ("Pages to process:" )
181204 self .log_debug (self .config .pages )
205+ pdf_files = []
182206 for page_num , page_config in enumerate (self .config .pages , start = 1 ):
183207 page = PDFBakerPage (
184208 document = self ,
0 commit comments