@@ -35,11 +35,8 @@ Builder(
3535{
3636 namespace fs = std::filesystem;
3737
38- Config const & config = domCorpus->config ;
39-
4038 // load partials
41- std::string partialsPath = files::appendPath (
42- config->addons , " generator" , domCorpus.fileExtension , " partials" );
39+ std::string partialsPath = templatesDir (" partials" );
4340 forEachFile (partialsPath, true ,
4441 [&](std::string_view pathName) -> Error
4542 {
@@ -60,8 +57,7 @@ Builder(
6057 }).maybeThrow ();
6158
6259 // Load JavaScript helpers
63- std::string helpersPath = files::appendPath (
64- config->addons , " generator" , domCorpus.fileExtension , " helpers" );
60+ std::string helpersPath = templatesDir (" helpers" );
6561 forEachFile (helpersPath, true ,
6662 [&](std::string_view pathName)-> Expected<void >
6763 {
@@ -126,11 +122,7 @@ callTemplate(
126122 std::string_view name,
127123 dom::Value const & context)
128124{
129- Config const & config = domCorpus->config ;
130-
131- auto layoutDir = files::appendPath (config->addons ,
132- " generator" , domCorpus.fileExtension , " layouts" );
133- auto pathName = files::appendPath (layoutDir, name);
125+ auto pathName = files::appendPath (layoutDir (), name);
134126 MRDOCS_TRY (auto fileText, files::getFileText (pathName));
135127 HandlebarsOptions options;
136128 options.noEscape = true ;
@@ -244,40 +236,68 @@ operator()(OverloadSet const& OS)
244236
245237Expected<void >
246238Builder::
247- wrapPage (
248- std::ostream& out,
249- std::istream& in)
239+ renderWrapped (std::ostream& os, std::function<Error()> contentsCb)
250240{
251- auto const wrapperFile = fmt::format (" wrapper.{}.hbs" , domCorpus.fileExtension );
241+ auto const wrapperFile = fmt::format (
242+ " wrapper.{}.hbs" , domCorpus.fileExtension );
252243 dom::Object ctx;
253- ctx.set (" contents" , dom::makeInvocable ([&in ](
254- dom::Value const & options) -> Expected<dom::Value>
244+ ctx.set (" contents" , dom::makeInvocable ([&](
245+ dom::Value const & options) -> Expected<dom::Value>
255246 {
256- // Helper to write contents directly to stream
257- // AFREITAS: custom functions should set options["write"]
258- // to avoid creating a string.
259- return std::string (
260- std::istreambuf_iterator< char >(in),
261- std::istreambuf_iterator< char >()) ;
247+ Error e = contentsCb ();
248+ if (e. failed ())
249+ {
250+ return Unexpected (e);
251+ }
252+ return {} ;
262253 }));
263- // Render directly to ostream
264- Config const & config = domCorpus->config ;
265- auto layoutDir = files::appendPath (config->addons ,
266- " generator" , domCorpus.fileExtension , " layouts" );
267- auto pathName = files::appendPath (layoutDir, wrapperFile);
254+
255+ // Render the wrapper directly to ostream
256+ auto pathName = files::appendPath (layoutDir (), wrapperFile);
268257 MRDOCS_TRY (auto fileText, files::getFileText (pathName));
269258 HandlebarsOptions options;
270259 options.noEscape = true ;
271- OutputRef outRef (out );
260+ OutputRef outRef (os );
272261 Expected<void , HandlebarsError> exp =
273- hbs_.try_render_to (outRef, fileText, ctx, options);
262+ hbs_.try_render_to (
263+ outRef, fileText, ctx, options);
274264 if (!exp)
275265 {
276- return Unexpected ( Error (exp.error ().what ()));
266+ Error (exp.error ().what ()). Throw ( );
277267 }
278268 return {};
279269}
280270
271+ std::string
272+ Builder::
273+ layoutDir () const
274+ {
275+ return templatesDir (" layouts" );
276+ }
277+
278+ std::string
279+ Builder::
280+ templatesDir () const
281+ {
282+ Config const & config = domCorpus->config ;
283+ return files::appendPath (
284+ config->addons ,
285+ " generator" ,
286+ domCorpus.fileExtension );
287+ }
288+
289+ std::string
290+ Builder::
291+ templatesDir (std::string_view subdir) const
292+ {
293+ Config const & config = domCorpus->config ;
294+ return files::appendPath (
295+ config->addons ,
296+ " generator" ,
297+ domCorpus.fileExtension ,
298+ subdir);
299+ }
300+
281301
282302// Define Builder::operator() for each Info type
283303#define DEFINE (T ) template Expected<std::string> \
0 commit comments