Skip to content

Commit 3cce490

Browse files
committed
feat(HandlebarsGenerator): common helper templates
1 parent 739b8fb commit 3cce490

File tree

2 files changed

+74
-18
lines changed

2 files changed

+74
-18
lines changed

src/lib/Gen/hbs/Builder.cpp

+64-18
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,56 @@ extern void lua_dump(dom::Object const& obj);
2828

2929
namespace hbs {
3030

31-
Builder::
32-
Builder(
33-
HandlebarsCorpus const& corpus)
34-
: domCorpus(corpus)
31+
namespace {
32+
void
33+
loadPartials(
34+
Handlebars& hbs,
35+
std::string const& partialsPath)
3536
{
36-
namespace fs = std::filesystem;
37-
38-
// load partials
39-
std::string partialsPath = templatesDir("partials");
37+
if (!files::exists(partialsPath))
38+
{
39+
return;
40+
}
4041
forEachFile(partialsPath, true,
4142
[&](std::string_view pathName) -> Error
4243
{
43-
fs::path path = pathName;
44-
if(path.extension() != ".hbs")
45-
return Error::success();
46-
path = path.lexically_relative(partialsPath);
47-
while(path.has_extension())
48-
path.replace_extension();
44+
// Skip directories
45+
MRDOCS_CHECK_OR(!files::isDirectory(pathName), Error::success());
46+
47+
// Get template relative path
48+
std::filesystem::path relPath = pathName;
49+
relPath = relPath.lexically_relative(partialsPath);
50+
51+
// Skip non-handlebars files
52+
MRDOCS_CHECK_OR(relPath.extension() == ".hbs", Error::success());
4953

54+
// Remove any file extensions
55+
while(relPath.has_extension())
56+
{
57+
relPath.replace_extension();
58+
}
59+
60+
// Load partial contents
5061
auto text = files::getFileText(pathName);
51-
if (! text)
52-
return text.error();
62+
MRDOCS_CHECK_OR(text, text.error());
5363

54-
hbs_.registerPartial(
55-
path.generic_string(), *text);
64+
// Register partial
65+
hbs.registerPartial(relPath.generic_string(), *text);
5666
return Error::success();
5767
}).maybeThrow();
68+
}
69+
}
70+
71+
Builder::
72+
Builder(
73+
HandlebarsCorpus const& corpus)
74+
: domCorpus(corpus)
75+
{
76+
namespace fs = std::filesystem;
77+
78+
// load partials
79+
loadPartials(hbs_, commonTemplatesDir("partials"));
80+
loadPartials(hbs_, templatesDir("partials"));
5881

5982
// Load JavaScript helpers
6083
std::string helpersPath = templatesDir("helpers");
@@ -299,6 +322,29 @@ templatesDir(std::string_view subdir) const
299322
subdir);
300323
}
301324

325+
std::string
326+
Builder::
327+
commonTemplatesDir() const
328+
{
329+
Config const& config = domCorpus->config;
330+
return files::appendPath(
331+
config->addons,
332+
"generator",
333+
"common");
334+
}
335+
336+
std::string
337+
Builder::
338+
commonTemplatesDir(std::string_view subdir) const
339+
{
340+
Config const& config = domCorpus->config;
341+
return files::appendPath(
342+
config->addons,
343+
"generator",
344+
"common",
345+
subdir);
346+
}
347+
302348

303349
// Define Builder::operator() for each Info type
304350
#define DEFINE(T) template Expected<std::string> \

src/lib/Gen/hbs/Builder.hpp

+10
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ class Builder
8686
std::string
8787
templatesDir(std::string_view subdir) const;
8888

89+
/** The directory with the common templates.
90+
*/
91+
std::string
92+
commonTemplatesDir() const;
93+
94+
/** A subdirectory of the common templates dir
95+
*/
96+
std::string
97+
commonTemplatesDir(std::string_view subdir) const;
98+
8999
/** The directory with the layout templates.
90100
*/
91101
std::string

0 commit comments

Comments
 (0)