@@ -38,112 +38,105 @@ Builder(
38
38
39
39
Config const & config = corpus_.config ;
40
40
41
- js::Scope scope (ctx_);
42
-
43
- scope.script (files::getFileText (
44
- files::appendPath (
45
- config->addonsDir , " js" , " handlebars.js" )
46
- ).value ()).maybeThrow ();
47
- auto Handlebars = scope.getGlobal (" Handlebars" ).value ();
48
-
49
- // VFALCO refactor this
50
- Handlebars.setlog ();
51
-
52
- // load templates
53
- #if 0
54
- err = forEachFile(options_.template_dir,
55
- [&](std::string_view fileName)
41
+ // load partials
42
+ std::string partialsPath = files::appendPath (
43
+ config->addonsDir , " generator" , " html" , " partials" );
44
+ forEachFile (partialsPath,
45
+ [&](std::string_view pathName) -> Error
46
+ {
47
+ constexpr std::string_view ext = " .html.hbs" ;
48
+ if (!pathName.ends_with (ext))
56
49
{
57
- if(! fileName.ends_with(".html.hbs"))
58
- return Error::success();
59
50
return Error::success ();
60
- });
61
- #endif
62
-
63
- Error err;
64
-
65
- // load partials
66
- forEachFile (
67
- files::appendPath (config->addonsDir ,
68
- " generator" , " html" , " partials" ),
69
- [&](std::string_view pathName)
51
+ }
52
+ auto name = files::getFileName (pathName);
53
+ name.remove_suffix (ext.size ());
54
+ auto text = files::getFileText (pathName);
55
+ if (!text)
70
56
{
71
- constexpr std::string_view ext = " .html.hbs" ;
72
- if (! pathName.ends_with (ext))
73
- return Error::success ();
74
- auto name = files::getFileName (pathName);
75
- name.remove_suffix (ext.size ());
76
- auto text = files::getFileText (pathName);
77
- if (! text)
78
- return text.error ();
79
- return Handlebars.callProp (
80
- " registerPartial" , name, *text)
81
- .error_or (Error::success ());
82
- }).maybeThrow ();
57
+ return text.error ();
58
+ }
59
+ hbs_.registerPartial (name, *text);
60
+ return Error::success ();
61
+ }).maybeThrow ();
83
62
84
63
// load helpers
85
- #if 0
86
- err = forEachFile(
87
- files::appendPath(config->addonsDir,
88
- "generator", "js", "helpers"),
89
- [&](std::string_view pathName)
64
+ js::Scope scope (ctx_);
65
+ std::string helpersPath = files::appendPath (
66
+ config->addonsDir , " generator" , " asciidoc" , " helpers" );
67
+ forEachFile (helpersPath,
68
+ [&](std::string_view pathName)
69
+ {
70
+ // Register JS helper function in the global object
71
+ constexpr std::string_view ext = " .js" ;
72
+ if (!pathName.ends_with (ext))
90
73
{
91
- constexpr std::string_view ext = ".js";
92
- if(! pathName.ends_with(ext))
93
- return Error::success();
94
- auto name = files::getFileName(pathName);
95
- name.remove_suffix(ext.size());
96
- //Handlebars.callProp("registerHelper", name, *text);
97
- auto err = ctx_.scriptFromFile(pathName);
98
74
return Error::success ();
99
- }).maybeThrow();
100
- #endif
101
-
102
- scope.script (fmt::format (
103
- R"( Handlebars.registerHelper(
104
- 'is_multipage', function()
105
- {{
106
- return {};
107
- }});
108
- )" , config->multiPage )).maybeThrow ();
109
-
110
- scope.script (R"(
111
- Handlebars.registerHelper(
112
- 'to_string', function(context, depth)
113
- {
114
- return JSON.stringify(context, null, 2);
115
- });
116
-
117
- Handlebars.registerHelper(
118
- 'eq', function(a, b)
75
+ }
76
+ auto name = files::getFileName (pathName);
77
+ name.remove_suffix (ext.size ());
78
+ auto text = files::getFileText (pathName);
79
+ if (!text)
119
80
{
120
- return a === b;
121
- });
122
-
123
- Handlebars.registerHelper(
124
- 'neq', function(a, b)
81
+ return text.error ();
82
+ }
83
+ auto JSFn = scope.compile (*text);
84
+ if (!JSFn)
125
85
{
126
- return a !== b;
127
- });
128
-
129
- Handlebars.registerHelper(
130
- 'not', function(a)
131
- {
132
- return ! a;
133
- });
134
-
135
- Handlebars.registerHelper(
136
- 'or', function(a, b)
86
+ return JSFn.error ();
87
+ }
88
+ scope.getGlobalObject ().set (name, *JSFn);
89
+
90
+ // Register C++ helper that retrieves the helper
91
+ // from the global object, converts the arguments,
92
+ // and invokes the JS function.
93
+ hbs_.registerHelper (name, dom::makeVariadicInvocable (
94
+ [this , name=std::string (name)](
95
+ dom::Array const & args) -> Expected<dom::Value>
137
96
{
138
- return a || b;
139
- });
140
-
141
- Handlebars.registerHelper(
142
- 'and', function(a, b)
143
- {
144
- return a && b;
145
- });
146
- )" ).maybeThrow ();
97
+ // Get function from global scope
98
+ js::Scope scope (ctx_);
99
+ js::Value fn = scope.getGlobalObject ().get (name);
100
+ if (fn.isUndefined ())
101
+ {
102
+ return Unexpected (Error (" helper not found" ));
103
+ }
104
+ if (!fn.isFunction ())
105
+ {
106
+ return Unexpected (Error (" helper is not a function" ));
107
+ }
108
+
109
+ // Call function
110
+ std::vector<dom::Value> arg_span;
111
+ arg_span.reserve (args.size ());
112
+ for (auto const & arg : args)
113
+ {
114
+ arg_span.push_back (arg);
115
+ }
116
+ auto result = fn.apply (arg_span);
117
+ if (!result)
118
+ {
119
+ return dom::Kind::Undefined;
120
+ }
121
+
122
+ // Convert result to dom::Value
123
+ return result->getDom ();
124
+ }));
125
+ return Error::success ();
126
+ }).maybeThrow ();
127
+ hbs_.registerHelper (
128
+ " is_multipage" ,
129
+ dom::makeInvocable ([res = config->multiPage ]() -> Expected<dom::Value> {
130
+ return res;
131
+ }));
132
+ helpers::registerAntoraHelpers (hbs_);
133
+ hbs_.registerHelper (" neq" , dom::makeVariadicInvocable (helpers::ne_fn));
134
+ hbs_.registerHelper (
135
+ " to_string" ,
136
+ dom::makeInvocable (
137
+ [](dom::Value const & context) -> Expected<dom::Value> {
138
+ return dom::JSON::stringify (context);
139
+ }));
147
140
}
148
141
149
142
// ------------------------------------------------
@@ -157,18 +150,23 @@ callTemplate(
157
150
Config const & config = corpus_.config ;
158
151
159
152
js::Scope scope (ctx_);
153
+
154
+
160
155
auto Handlebars = scope.getGlobal (" Handlebars" );
161
156
auto layoutDir = files::appendPath (config->addonsDir ,
162
157
" generator" , " html" , " layouts" );
163
158
auto pathName = files::appendPath (layoutDir, name);
164
159
MRDOX_TRY (auto fileText, files::getFileText (pathName));
165
- dom::Object options;
166
- options.set (" allowProtoPropertiesByDefault" , true );
167
- // VFALCO This makes Proxy objects stop working
168
- // options.set("allowProtoMethodsByDefault", true);
169
- MRDOX_TRY (auto templateFn, Handlebars->callProp (" compile" , fileText, options));
170
- MRDOX_TRY (auto result, templateFn.call (context, options));
171
- return result.getString ();
160
+ HandlebarsOptions options;
161
+ options.noEscape = true ;
162
+
163
+ Expected<std::string, HandlebarsError> exp =
164
+ hbs_.try_render (fileText, context, options);
165
+ if (!exp )
166
+ {
167
+ return Unexpected (Error (exp .error ().what ()));
168
+ }
169
+ return *exp ;
172
170
}
173
171
174
172
Expected<std::string>
0 commit comments