Skip to content

Commit 739b8fb

Browse files
committed
feat(Handlebars): value constructor helpers
1 parent f9e7af4 commit 739b8fb

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

include/mrdocs/Support/Handlebars.hpp

+12
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,18 @@ MRDOCS_DECL
10261026
void
10271027
registerBuiltinHelpers(Handlebars& hbs);
10281028

1029+
/** Register contructor helpers into a Handlebars instance
1030+
1031+
This function registers a number of common helpers that allows
1032+
the user to create objects of specific types directly from
1033+
literals in the template.
1034+
1035+
@param hbs The Handlebars instance to register the helpers into
1036+
*/
1037+
MRDOCS_DECL
1038+
void
1039+
registerConstructorHelpers(Handlebars& hbs);
1040+
10291041
/** Register all the Antora helpers into a Handlebars instance
10301042
10311043
This function registers all the helpers that are part of the

src/lib/Gen/hbs/Builder.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Builder(
108108
return first;
109109
}));
110110

111+
helpers::registerConstructorHelpers(hbs_);
111112
helpers::registerStringHelpers(hbs_);
112113
helpers::registerAntoraHelpers(hbs_);
113114
helpers::registerLogicalHelpers(hbs_);

src/lib/Support/Handlebars.cpp

+68
Original file line numberDiff line numberDiff line change
@@ -5933,6 +5933,74 @@ registerStringHelpers(Handlebars& hbs)
59335933
}));
59345934
}
59355935

5936+
void
5937+
registerConstructorHelpers(Handlebars& hbs)
5938+
{
5939+
// A helper that constructs a string from the first argument.
5940+
hbs.registerHelper("str", dom::makeVariadicInvocable([](
5941+
dom::Array const& arguments) -> Expected<dom::Value>
5942+
{
5943+
if (arguments.size() != 2)
5944+
{
5945+
return Unexpected(Error("#str requires exactly one argument"));
5946+
}
5947+
return arguments.at(0);
5948+
}));
5949+
5950+
// A helper that constructs an array from the arguments.
5951+
hbs.registerHelper("arr", dom::makeVariadicInvocable([](
5952+
dom::Array const& arguments) -> dom::Value
5953+
{
5954+
dom::Array res;
5955+
for (std::size_t i = 0; i < arguments.size() - 1; ++i)
5956+
{
5957+
res.emplace_back(arguments.at(i));
5958+
}
5959+
return res;
5960+
}));
5961+
5962+
// A helper that constructs an array of indexes.
5963+
hbs.registerHelper("range", dom::makeVariadicInvocable([](
5964+
dom::Array const& arguments) -> dom::Value
5965+
{
5966+
std::int64_t start = 0;
5967+
std::int64_t stop = 0;
5968+
std::int64_t step = 1;
5969+
std::size_t const n = arguments.size() - 1;
5970+
if (n == 1)
5971+
{
5972+
stop = arguments.at(0).getInteger();
5973+
}
5974+
else if (n == 2)
5975+
{
5976+
start = arguments.at(0).getInteger();
5977+
stop = arguments.at(1).getInteger();
5978+
}
5979+
else if (n == 3)
5980+
{
5981+
start = arguments.at(0).getInteger();
5982+
stop = arguments.at(1).getInteger();
5983+
step = arguments.at(2).getInteger();
5984+
}
5985+
dom::Array res;
5986+
if (step > 0)
5987+
{
5988+
for (std::int64_t i = start; i < stop; i += step)
5989+
{
5990+
res.emplace_back(i);
5991+
}
5992+
}
5993+
else
5994+
{
5995+
for (std::int64_t i = start; i > stop; i += step)
5996+
{
5997+
res.emplace_back(i);
5998+
}
5999+
}
6000+
return res;
6001+
}));
6002+
}
6003+
59366004
std::vector<std::string>
59376005
parseKeyPath(std::string const& keyPath)
59386006
{

0 commit comments

Comments
 (0)