@@ -28,33 +28,56 @@ extern void lua_dump(dom::Object const& obj);
28
28
29
29
namespace hbs {
30
30
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)
35
36
{
36
- namespace fs = std::filesystem;
37
-
38
- // load partials
39
- std::string partialsPath = templatesDir ( " partials " );
37
+ if (! files::exists (partialsPath))
38
+ {
39
+ return ;
40
+ }
40
41
forEachFile (partialsPath, true ,
41
42
[&](std::string_view pathName) -> Error
42
43
{
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 ());
49
53
54
+ // Remove any file extensions
55
+ while (relPath.has_extension ())
56
+ {
57
+ relPath.replace_extension ();
58
+ }
59
+
60
+ // Load partial contents
50
61
auto text = files::getFileText (pathName);
51
- if (! text)
52
- return text.error ();
62
+ MRDOCS_CHECK_OR (text, text.error ());
53
63
54
- hbs_. registerPartial (
55
- path .generic_string (), *text);
64
+ // Register partial
65
+ hbs. registerPartial (relPath .generic_string (), *text);
56
66
return Error::success ();
57
67
}).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" ));
58
81
59
82
// Load JavaScript helpers
60
83
std::string helpersPath = templatesDir (" helpers" );
@@ -299,6 +322,29 @@ templatesDir(std::string_view subdir) const
299
322
subdir);
300
323
}
301
324
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
+
302
348
303
349
// Define Builder::operator() for each Info type
304
350
#define DEFINE (T ) template Expected<std::string> \
0 commit comments