Skip to content

Commit 9543810

Browse files
committed
perf(Builder): load template layouts at construction
1 parent 3d38f59 commit 9543810

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/lib/Gen/hbs/Builder.cpp

+26-2
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,29 @@ Builder(
143143
helpers::registerAntoraHelpers(hbs_);
144144
helpers::registerLogicalHelpers(hbs_);
145145
helpers::registerContainerHelpers(hbs_);
146+
147+
// load templates
148+
exp = forEachFile(layoutDir(), false,
149+
[&](std::string_view pathName) -> Expected<void>
150+
{
151+
// Get template relative path
152+
std::filesystem::path relPath = pathName;
153+
relPath = relPath.lexically_relative(layoutDir());
154+
155+
// Skip non-handlebars files
156+
MRDOCS_CHECK_OR(relPath.extension() == ".hbs", {});
157+
158+
// Load template contents
159+
MRDOCS_TRY(std::string text, files::getFileText(pathName));
160+
161+
// Register template
162+
this->templates_.emplace(relPath.generic_string(), text);
163+
return {};
164+
});
165+
if (!exp)
166+
{
167+
exp.error().Throw();
168+
}
146169
}
147170

148171
//------------------------------------------------
@@ -154,8 +177,9 @@ callTemplate(
154177
std::string_view name,
155178
dom::Value const& context)
156179
{
157-
auto pathName = files::appendPath(layoutDir(), name);
158-
MRDOCS_TRY(auto fileText, files::getFileText(pathName));
180+
auto it = templates_.find(name);
181+
MRDOCS_CHECK(it != templates_.end(), formatError("Template {} not found", name));
182+
std::string_view fileText = it->second;
159183
HandlebarsOptions options;
160184
options.escapeFunction = escapeFn_;
161185
OutputRef out(os);

src/lib/Gen/hbs/Builder.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Builder
3333
{
3434
js::Context ctx_;
3535
Handlebars hbs_;
36+
std::map<std::string, std::string, std::less<>> templates_;
3637
std::function<void(OutputRef&, std::string_view)> escapeFn_;
3738

3839
std::string

0 commit comments

Comments
 (0)