Skip to content

Commit

Permalink
feat(HandlebarsGenerator): common helper templates
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Nov 21, 2024
1 parent 739b8fb commit 3cce490
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 18 deletions.
82 changes: 64 additions & 18 deletions src/lib/Gen/hbs/Builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,56 @@ extern void lua_dump(dom::Object const& obj);

namespace hbs {

Builder::
Builder(
HandlebarsCorpus const& corpus)
: domCorpus(corpus)
namespace {
void
loadPartials(
Handlebars& hbs,
std::string const& partialsPath)
{
namespace fs = std::filesystem;

// load partials
std::string partialsPath = templatesDir("partials");
if (!files::exists(partialsPath))
{
return;
}
forEachFile(partialsPath, true,
[&](std::string_view pathName) -> Error
{
fs::path path = pathName;
if(path.extension() != ".hbs")
return Error::success();
path = path.lexically_relative(partialsPath);
while(path.has_extension())
path.replace_extension();
// Skip directories
MRDOCS_CHECK_OR(!files::isDirectory(pathName), Error::success());

// Get template relative path
std::filesystem::path relPath = pathName;
relPath = relPath.lexically_relative(partialsPath);

// Skip non-handlebars files
MRDOCS_CHECK_OR(relPath.extension() == ".hbs", Error::success());

// Remove any file extensions
while(relPath.has_extension())
{
relPath.replace_extension();
}

// Load partial contents
auto text = files::getFileText(pathName);
if (! text)
return text.error();
MRDOCS_CHECK_OR(text, text.error());

hbs_.registerPartial(
path.generic_string(), *text);
// Register partial
hbs.registerPartial(relPath.generic_string(), *text);
return Error::success();
}).maybeThrow();
}
}

Builder::
Builder(
HandlebarsCorpus const& corpus)
: domCorpus(corpus)
{
namespace fs = std::filesystem;

// load partials
loadPartials(hbs_, commonTemplatesDir("partials"));
loadPartials(hbs_, templatesDir("partials"));

// Load JavaScript helpers
std::string helpersPath = templatesDir("helpers");
Expand Down Expand Up @@ -299,6 +322,29 @@ templatesDir(std::string_view subdir) const
subdir);
}

std::string
Builder::
commonTemplatesDir() const
{
Config const& config = domCorpus->config;
return files::appendPath(
config->addons,
"generator",
"common");
}

std::string
Builder::
commonTemplatesDir(std::string_view subdir) const
{
Config const& config = domCorpus->config;
return files::appendPath(
config->addons,
"generator",
"common",
subdir);
}


// Define Builder::operator() for each Info type
#define DEFINE(T) template Expected<std::string> \
Expand Down
10 changes: 10 additions & 0 deletions src/lib/Gen/hbs/Builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ class Builder
std::string
templatesDir(std::string_view subdir) const;

/** The directory with the common templates.
*/
std::string
commonTemplatesDir() const;

/** A subdirectory of the common templates dir
*/
std::string
commonTemplatesDir(std::string_view subdir) const;

/** The directory with the layout templates.
*/
std::string
Expand Down

0 comments on commit 3cce490

Please sign in to comment.