Add condition parameters.

This commit is contained in:
chriseth 2019-05-07 14:41:42 +02:00
parent dfe07867e0
commit 606551e4dd
2 changed files with 68 additions and 24 deletions

View File

@ -42,6 +42,13 @@ Whiskers& Whiskers::operator()(string _parameter, string _value)
return *this;
}
Whiskers& Whiskers::operator()(string _parameter, bool _value)
{
checkParameterUnknown(_parameter);
m_conditions[move(_parameter)] = _value;
return *this;
}
Whiskers& Whiskers::operator()(
string _listParameter,
vector<map<string, string>> _values
@ -54,7 +61,7 @@ Whiskers& Whiskers::operator()(
string Whiskers::render() const
{
return replace(m_template, m_parameters, m_listParameters);
return replace(m_template, m_parameters, m_conditions, m_listParameters);
}
void Whiskers::checkParameterUnknown(string const& _parameter)
@ -64,6 +71,11 @@ void Whiskers::checkParameterUnknown(string const& _parameter)
WhiskersError,
_parameter + " already set as value parameter."
);
assertThrow(
!m_conditions.count(_parameter),
WhiskersError,
_parameter + " already set as condition parameter."
);
assertThrow(
!m_listParameters.count(_parameter),
WhiskersError,
@ -74,14 +86,17 @@ void Whiskers::checkParameterUnknown(string const& _parameter)
string Whiskers::replace(
string const& _template,
StringMap const& _parameters,
map<string, bool> const& _conditions,
map<string, vector<StringMap>> const& _listParameters
)
{
using namespace boost;
static regex listOrTag("<([^#/>]+)>|<#([^>]+)>(.*?)</\\2>");
static regex listOrTag("<([^#/?!>]+)>|<#([^>]+)>(.*?)</\\2>|<\\?([^>]+)>(.*?)(<!\\4>(.*?))?</\\4>");
return regex_replace(_template, listOrTag, [&](match_results<string::const_iterator> _match) -> string
{
string tagName(_match[1]);
string listName(_match[2]);
string conditionName(_match[4]);
if (!tagName.empty())
{
assertThrow(
@ -93,20 +108,32 @@ string Whiskers::replace(
);
return _parameters.at(tagName);
}
else
else if (!listName.empty())
{
string listName(_match[2]);
string templ(_match[3]);
assertThrow(!listName.empty(), WhiskersError, "");
assertThrow(
_listParameters.count(listName),
WhiskersError, "List parameter " + listName + " not set."
);
string replacement;
for (auto const& parameters: _listParameters.at(listName))
replacement += replace(templ, joinMaps(_parameters, parameters));
replacement += replace(templ, joinMaps(_parameters, parameters), _conditions);
return replacement;
}
else
{
assertThrow(!conditionName.empty(), WhiskersError, "");
assertThrow(
_conditions.count(conditionName),
WhiskersError, "Condition parameter " + conditionName + " not set."
);
return replace(
_conditions.at(conditionName) ? _match[5] : _match[7],
_parameters,
_conditions,
_listParameters
);
}
});
}

View File

@ -34,23 +34,35 @@ namespace dev
DEV_SIMPLE_EXCEPTION(WhiskersError);
///
/// Moustache-like templates.
///
/// Usage:
/// std::vector<std::map<std::string, std::string>> listValues(2);
/// listValues[0]["k"] = "key1";
/// listValues[0]["v"] = "value1";
/// listValues[1]["k"] = "key2";
/// listValues[1]["v"] = "value2";
/// auto s = Whiskers("<p1>\n<#list><k> -> <v>\n</list>")
/// ("p1", "HEAD")
/// ("list", listValues)
/// .render();
///
/// results in s == "HEAD\nkey1 -> value1\nkey2 -> value2\n"
///
/// Note that lists cannot themselves contain lists - this would be a future feature.
/**
* Moustache-like templates.
*
* Usage:
* std::vector<std::map<std::string, std::string>> listValues(2);
* listValues[0]["k"] = "key1";
* listValues[0]["v"] = "value1";
* listValues[1]["k"] = "key2";
* listValues[1]["v"] = "value2";
* auto s = Whiskers("<?c><p1><!c>y</c>\n<#list><k> -> <v>\n</list>")
* ("p1", "HEAD")
* ("c", true)
* ("list", listValues)
* .render();
*
* results in s == "HEAD\nkey1 -> value1\nkey2 -> value2\n"
*
* Note that lists cannot themselves contain lists - this would be a future feature.
*
* The elements are:
* - Regular parameter: <name>
* just replaced
* - Condition parameter: <?name>...<!name>...</name>, where "<!name>" is optional
* replaced (and recursively expanded) by the first part if the condition is true
* and by the second (or empty string if missing) if the condition is false
* - List parameter: <#list>...</list>
* The part between the tags is repeated as often as values are provided
* in the mapping. Each list element can have its own parameter -> value mapping.
*/
class Whiskers
{
public:
@ -59,8 +71,11 @@ public:
explicit Whiskers(std::string _template);
/// Sets a single parameter, <paramName>.
/// Sets a single regular parameter, <paramName>.
Whiskers& operator()(std::string _parameter, std::string _value);
Whiskers& operator()(std::string _parameter, char const* _value) { return (*this)(_parameter, std::string{_value}); }
/// Sets a condition parameter, <?paramName>...<!paramName>...</paramName>
Whiskers& operator()(std::string _parameter, bool _value);
/// Sets a list parameter, <#listName> </listName>.
Whiskers& operator()(
std::string _listParameter,
@ -75,6 +90,7 @@ private:
static std::string replace(
std::string const& _template,
StringMap const& _parameters,
std::map<std::string, bool> const& _conditions,
StringListMap const& _listParameters = StringListMap()
);
@ -83,6 +99,7 @@ private:
std::string m_template;
StringMap m_parameters;
std::map<std::string, bool> m_conditions;
StringListMap m_listParameters;
};