@@ -57,7 +57,7 @@ Builder(
57
57
hbs_.registerPartial (
58
58
path.generic_string (), *text);
59
59
return Error::success ();
60
- }).maybeThrow ();
60
+ }).maybeThrow ();
61
61
62
62
// Load JavaScript helpers
63
63
std::string helpersPath = files::appendPath (
@@ -143,23 +143,8 @@ callTemplate(
143
143
return *exp ;
144
144
}
145
145
146
- Expected<std::string>
147
- Builder::
148
- renderSinglePageHeader ()
149
- {
150
- return callTemplate (fmt::format (" single-header.{}.hbs" , domCorpus.fileExtension ), {});
151
- }
152
-
153
- Expected<std::string>
154
- Builder::
155
- renderSinglePageFooter ()
156
- {
157
- return callTemplate (fmt::format (" single-footer.{}.hbs" , domCorpus.fileExtension ), {});
158
- }
159
-
160
146
// ------------------------------------------------
161
147
162
-
163
148
std::string
164
149
Builder::
165
150
getRelPrefix (std::size_t depth)
@@ -177,58 +162,123 @@ getRelPrefix(std::size_t depth)
177
162
return rel_prefix;
178
163
}
179
164
180
- dom::Value
165
+ dom::Object
181
166
Builder::
182
167
createContext (
183
168
Info const & I)
184
169
{
185
- dom::Object::storage_type props;
186
- props.emplace_back (" symbol" ,
187
- domCorpus.get (I.id ));
188
- props.emplace_back (" relfileprefix" ,
189
- getRelPrefix (I.Namespace .size ()));
190
- props.emplace_back (" config" , domCorpus->config .object ());
191
- props.emplace_back (" sectionref" ,
192
- domCorpus.names_ .getQualified (I.id , ' -' ));
193
- return dom::Object (std::move (props));
170
+ dom::Object ctx;
171
+ ctx.set (" symbol" , domCorpus.get (I.id ));
172
+ ctx.set (" relfileprefix" , getRelPrefix (I.Namespace .size ()));
173
+ ctx.set (" config" , domCorpus->config .object ());
174
+ ctx.set (" sectionref" , domCorpus.names_ .getQualified (I.id , ' -' ));
175
+ return ctx;
194
176
}
195
177
196
- dom::Value
178
+ dom::Object
197
179
Builder::
198
180
createContext (
199
181
OverloadSet const & OS)
200
182
{
201
- dom::Object::storage_type props;
202
- props.emplace_back (" symbol" ,
203
- domCorpus.getOverloads (OS));
183
+ dom::Object ctx;
184
+ ctx.set (" symbol" , domCorpus.getOverloads (OS));
204
185
const Info& Parent = domCorpus->get (OS.Parent );
205
- props.emplace_back (" relfileprefix" ,
206
- getRelPrefix (Parent.Namespace .size () + 1 ));
207
- props.emplace_back (" config" , domCorpus->config .object ());
208
- props.emplace_back (" sectionref" ,
209
- domCorpus.names_ .getQualified (OS, ' -' ));
210
- return dom::Object (std::move (props));
186
+ ctx.set (" relfileprefix" , getRelPrefix (Parent.Namespace .size () + 1 ));
187
+ ctx.set (" config" , domCorpus->config .object ());
188
+ ctx.set (" sectionref" , domCorpus.names_ .getQualified (OS, ' -' ));
189
+ return ctx;
211
190
}
212
191
213
192
template <class T >
214
193
Expected<std::string>
215
194
Builder::
216
195
operator ()(T const & I)
217
196
{
218
- return callTemplate (
219
- fmt::format (" single-symbol.{}.hbs" , domCorpus.fileExtension ),
220
- createContext (I));
197
+ auto const templateFile = fmt::format (" index.{}.hbs" , domCorpus.fileExtension );
198
+ dom::Object ctx = createContext (I);
199
+
200
+ auto & config = domCorpus->config ;
201
+ bool isSinglePage = !config->multipage ;
202
+ if (config->embedded ||
203
+ isSinglePage)
204
+ {
205
+ return callTemplate (templateFile, ctx);
206
+ }
207
+
208
+ auto const wrapperFile = fmt::format (" wrapper.{}.hbs" , domCorpus.fileExtension );
209
+ dom::Object wrapperCtx = createFrame (ctx);
210
+ wrapperCtx.set (" contents" , dom::makeInvocable ([this , &I, templateFile](
211
+ dom::Value const & options) -> Expected<dom::Value>
212
+ {
213
+ // Helper to write contents directly to stream
214
+ return callTemplate (templateFile, createContext (I));
215
+ }));
216
+ return callTemplate (wrapperFile, wrapperCtx);
221
217
}
222
218
223
219
Expected<std::string>
224
220
Builder::
225
221
operator ()(OverloadSet const & OS)
226
222
{
227
- return callTemplate (
228
- fmt::format (" overload-set.{}.hbs" , domCorpus.fileExtension ),
229
- createContext (OS));
223
+ auto const templateFile = fmt::format (" index-overload-set.{}.hbs" , domCorpus.fileExtension );
224
+ dom::Object ctx = createContext (OS);
225
+
226
+ auto & config = domCorpus->config ;
227
+ bool isSinglePage = !config->multipage ;
228
+ if (config->embedded ||
229
+ isSinglePage)
230
+ {
231
+ return callTemplate (templateFile, ctx);
232
+ }
233
+
234
+ auto const wrapperFile = fmt::format (" wrapper.{}.hbs" , domCorpus.fileExtension );
235
+ dom::Object wrapperCtx = createFrame (ctx);
236
+ wrapperCtx.set (" contents" , dom::makeInvocable ([this , &OS, templateFile](
237
+ dom::Value const & options) -> Expected<dom::Value>
238
+ {
239
+ // Helper to write contents directly to stream
240
+ return callTemplate (templateFile, createContext (OS));
241
+ }));
242
+ return callTemplate (wrapperFile, wrapperCtx);
230
243
}
231
244
245
+ Expected<void >
246
+ Builder::
247
+ wrapPage (
248
+ std::ostream& out,
249
+ std::istream& in)
250
+ {
251
+ auto const wrapperFile = fmt::format (" wrapper.{}.hbs" , domCorpus.fileExtension );
252
+ dom::Object ctx;
253
+ ctx.set (" contents" , dom::makeInvocable ([&in](
254
+ dom::Value const & options) -> Expected<dom::Value>
255
+ {
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 >());
262
+ }));
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);
268
+ MRDOCS_TRY (auto fileText, files::getFileText (pathName));
269
+ HandlebarsOptions options;
270
+ options.noEscape = true ;
271
+ OutputRef outRef (out);
272
+ Expected<void , HandlebarsError> exp =
273
+ hbs_.try_render_to (outRef, fileText, ctx, options);
274
+ if (!exp )
275
+ {
276
+ return Unexpected (Error (exp .error ().what ()));
277
+ }
278
+ return {};
279
+ }
280
+
281
+
232
282
// Define Builder::operator() for each Info type
233
283
#define DEFINE (T ) template Expected<std::string> \
234
284
Builder::operator ()<T>(T const &)
0 commit comments