Skip to content

Commit

Permalink
feat(Handlebars): value constructor helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Nov 21, 2024
1 parent f9e7af4 commit 739b8fb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/mrdocs/Support/Handlebars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,18 @@ MRDOCS_DECL
void
registerBuiltinHelpers(Handlebars& hbs);

/** Register contructor helpers into a Handlebars instance
This function registers a number of common helpers that allows
the user to create objects of specific types directly from
literals in the template.
@param hbs The Handlebars instance to register the helpers into
*/
MRDOCS_DECL
void
registerConstructorHelpers(Handlebars& hbs);

/** Register all the Antora helpers into a Handlebars instance
This function registers all the helpers that are part of the
Expand Down
1 change: 1 addition & 0 deletions src/lib/Gen/hbs/Builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ Builder(
return first;
}));

helpers::registerConstructorHelpers(hbs_);
helpers::registerStringHelpers(hbs_);
helpers::registerAntoraHelpers(hbs_);
helpers::registerLogicalHelpers(hbs_);
Expand Down
68 changes: 68 additions & 0 deletions src/lib/Support/Handlebars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5933,6 +5933,74 @@ registerStringHelpers(Handlebars& hbs)
}));
}

void
registerConstructorHelpers(Handlebars& hbs)
{
// A helper that constructs a string from the first argument.
hbs.registerHelper("str", dom::makeVariadicInvocable([](
dom::Array const& arguments) -> Expected<dom::Value>
{
if (arguments.size() != 2)
{
return Unexpected(Error("#str requires exactly one argument"));
}
return arguments.at(0);
}));

// A helper that constructs an array from the arguments.
hbs.registerHelper("arr", dom::makeVariadicInvocable([](
dom::Array const& arguments) -> dom::Value
{
dom::Array res;
for (std::size_t i = 0; i < arguments.size() - 1; ++i)
{
res.emplace_back(arguments.at(i));
}
return res;
}));

// A helper that constructs an array of indexes.
hbs.registerHelper("range", dom::makeVariadicInvocable([](
dom::Array const& arguments) -> dom::Value
{
std::int64_t start = 0;
std::int64_t stop = 0;
std::int64_t step = 1;
std::size_t const n = arguments.size() - 1;
if (n == 1)
{
stop = arguments.at(0).getInteger();
}
else if (n == 2)
{
start = arguments.at(0).getInteger();
stop = arguments.at(1).getInteger();
}
else if (n == 3)
{
start = arguments.at(0).getInteger();
stop = arguments.at(1).getInteger();
step = arguments.at(2).getInteger();
}
dom::Array res;
if (step > 0)
{
for (std::int64_t i = start; i < stop; i += step)
{
res.emplace_back(i);
}
}
else
{
for (std::int64_t i = start; i > stop; i += step)
{
res.emplace_back(i);
}
}
return res;
}));
}

std::vector<std::string>
parseKeyPath(std::string const& keyPath)
{
Expand Down

0 comments on commit 739b8fb

Please sign in to comment.