Skip to content

Commit

Permalink
perf(Builder): load template layouts at construction
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Nov 25, 2024
1 parent 3d38f59 commit 9543810
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/lib/Gen/hbs/Builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ Builder(
helpers::registerAntoraHelpers(hbs_);
helpers::registerLogicalHelpers(hbs_);
helpers::registerContainerHelpers(hbs_);

// load templates
exp = forEachFile(layoutDir(), false,
[&](std::string_view pathName) -> Expected<void>
{
// Get template relative path
std::filesystem::path relPath = pathName;
relPath = relPath.lexically_relative(layoutDir());

// Skip non-handlebars files
MRDOCS_CHECK_OR(relPath.extension() == ".hbs", {});

// Load template contents
MRDOCS_TRY(std::string text, files::getFileText(pathName));

// Register template
this->templates_.emplace(relPath.generic_string(), text);
return {};
});
if (!exp)
{
exp.error().Throw();
}
}

//------------------------------------------------
Expand All @@ -154,8 +177,9 @@ callTemplate(
std::string_view name,
dom::Value const& context)
{
auto pathName = files::appendPath(layoutDir(), name);
MRDOCS_TRY(auto fileText, files::getFileText(pathName));
auto it = templates_.find(name);
MRDOCS_CHECK(it != templates_.end(), formatError("Template {} not found", name));
std::string_view fileText = it->second;
HandlebarsOptions options;
options.escapeFunction = escapeFn_;
OutputRef out(os);
Expand Down
1 change: 1 addition & 0 deletions src/lib/Gen/hbs/Builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Builder
{
js::Context ctx_;
Handlebars hbs_;
std::map<std::string, std::string, std::less<>> templates_;
std::function<void(OutputRef&, std::string_view)> escapeFn_;

Check warning on line 37 in src/lib/Gen/hbs/Builder.hpp

View workflow job for this annotation

GitHub Actions / GCC 14: C++20

Build Warning - g++-14 - [-Wreorder]

g++-14 - 'std::function<void(clang::mrdocs::OutputRef&, std::basic_string_view<char>)> clang::mrdocs::hbs::Builder::escapeFn_' ([-Wreorder])

Check warning on line 37 in src/lib/Gen/hbs/Builder.hpp

View workflow job for this annotation

GitHub Actions / GCC 14: C++20 (Coverage)

Build Warning - g++-14 - [-Wreorder]

g++-14 - 'std::function<void(clang::mrdocs::OutputRef&, std::basic_string_view<char>)> clang::mrdocs::hbs::Builder::escapeFn_' ([-Wreorder])

std::string
Expand Down

0 comments on commit 9543810

Please sign in to comment.