@@ -35,11 +35,8 @@ Builder(
35
35
{
36
36
namespace fs = std::filesystem;
37
37
38
- Config const & config = domCorpus->config ;
39
-
40
38
// load partials
41
- std::string partialsPath = files::appendPath (
42
- config->addons , " generator" , domCorpus.fileExtension , " partials" );
39
+ std::string partialsPath = templatesDir (" partials" );
43
40
forEachFile (partialsPath, true ,
44
41
[&](std::string_view pathName) -> Error
45
42
{
@@ -60,8 +57,7 @@ Builder(
60
57
}).maybeThrow ();
61
58
62
59
// Load JavaScript helpers
63
- std::string helpersPath = files::appendPath (
64
- config->addons , " generator" , domCorpus.fileExtension , " helpers" );
60
+ std::string helpersPath = templatesDir (" helpers" );
65
61
forEachFile (helpersPath, true ,
66
62
[&](std::string_view pathName)-> Expected<void >
67
63
{
@@ -126,11 +122,7 @@ callTemplate(
126
122
std::string_view name,
127
123
dom::Value const & context)
128
124
{
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);
134
126
MRDOCS_TRY (auto fileText, files::getFileText (pathName));
135
127
HandlebarsOptions options;
136
128
options.noEscape = true ;
@@ -244,40 +236,68 @@ operator()(OverloadSet const& OS)
244
236
245
237
Expected<void >
246
238
Builder::
247
- wrapPage (
248
- std::ostream& out,
249
- std::istream& in)
239
+ renderWrapped (std::ostream& os, std::function<Error()> contentsCb)
250
240
{
251
- auto const wrapperFile = fmt::format (" wrapper.{}.hbs" , domCorpus.fileExtension );
241
+ auto const wrapperFile = fmt::format (
242
+ " wrapper.{}.hbs" , domCorpus.fileExtension );
252
243
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>
255
246
{
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 {} ;
262
253
}));
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);
268
257
MRDOCS_TRY (auto fileText, files::getFileText (pathName));
269
258
HandlebarsOptions options;
270
259
options.noEscape = true ;
271
- OutputRef outRef (out );
260
+ OutputRef outRef (os );
272
261
Expected<void , HandlebarsError> exp =
273
- hbs_.try_render_to (outRef, fileText, ctx, options);
262
+ hbs_.try_render_to (
263
+ outRef, fileText, ctx, options);
274
264
if (!exp )
275
265
{
276
- return Unexpected ( Error (exp .error ().what ()));
266
+ Error (exp .error ().what ()). Throw ( );
277
267
}
278
268
return {};
279
269
}
280
270
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
+
281
301
282
302
// Define Builder::operator() for each Info type
283
303
#define DEFINE (T ) template Expected<std::string> \
0 commit comments