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; return *this;
} }
Whiskers& Whiskers::operator()(string _parameter, bool _value)
{
checkParameterUnknown(_parameter);
m_conditions[move(_parameter)] = _value;
return *this;
}
Whiskers& Whiskers::operator()( Whiskers& Whiskers::operator()(
string _listParameter, string _listParameter,
vector<map<string, string>> _values vector<map<string, string>> _values
@ -54,7 +61,7 @@ Whiskers& Whiskers::operator()(
string Whiskers::render() const 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) void Whiskers::checkParameterUnknown(string const& _parameter)
@ -64,6 +71,11 @@ void Whiskers::checkParameterUnknown(string const& _parameter)
WhiskersError, WhiskersError,
_parameter + " already set as value parameter." _parameter + " already set as value parameter."
); );
assertThrow(
!m_conditions.count(_parameter),
WhiskersError,
_parameter + " already set as condition parameter."
);
assertThrow( assertThrow(
!m_listParameters.count(_parameter), !m_listParameters.count(_parameter),
WhiskersError, WhiskersError,
@ -74,14 +86,17 @@ void Whiskers::checkParameterUnknown(string const& _parameter)
string Whiskers::replace( string Whiskers::replace(
string const& _template, string const& _template,
StringMap const& _parameters, StringMap const& _parameters,
map<string, bool> const& _conditions,
map<string, vector<StringMap>> const& _listParameters map<string, vector<StringMap>> const& _listParameters
) )
{ {
using namespace boost; 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 return regex_replace(_template, listOrTag, [&](match_results<string::const_iterator> _match) -> string
{ {
string tagName(_match[1]); string tagName(_match[1]);
string listName(_match[2]);
string conditionName(_match[4]);
if (!tagName.empty()) if (!tagName.empty())
{ {
assertThrow( assertThrow(
@ -93,20 +108,32 @@ string Whiskers::replace(
); );
return _parameters.at(tagName); return _parameters.at(tagName);
} }
else else if (!listName.empty())
{ {
string listName(_match[2]);
string templ(_match[3]); string templ(_match[3]);
assertThrow(!listName.empty(), WhiskersError, "");
assertThrow( assertThrow(
_listParameters.count(listName), _listParameters.count(listName),
WhiskersError, "List parameter " + listName + " not set." WhiskersError, "List parameter " + listName + " not set."
); );
string replacement; string replacement;
for (auto const& parameters: _listParameters.at(listName)) for (auto const& parameters: _listParameters.at(listName))
replacement += replace(templ, joinMaps(_parameters, parameters)); replacement += replace(templ, joinMaps(_parameters, parameters), _conditions);
return replacement; 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); DEV_SIMPLE_EXCEPTION(WhiskersError);
/// /**
/// Moustache-like templates. * Moustache-like templates.
/// *
/// Usage: * Usage:
/// std::vector<std::map<std::string, std::string>> listValues(2); * std::vector<std::map<std::string, std::string>> listValues(2);
/// listValues[0]["k"] = "key1"; * listValues[0]["k"] = "key1";
/// listValues[0]["v"] = "value1"; * listValues[0]["v"] = "value1";
/// listValues[1]["k"] = "key2"; * listValues[1]["k"] = "key2";
/// listValues[1]["v"] = "value2"; * listValues[1]["v"] = "value2";
/// auto s = Whiskers("<p1>\n<#list><k> -> <v>\n</list>") * auto s = Whiskers("<?c><p1><!c>y</c>\n<#list><k> -> <v>\n</list>")
/// ("p1", "HEAD") * ("p1", "HEAD")
/// ("list", listValues) * ("c", true)
/// .render(); * ("list", listValues)
/// * .render();
/// results in s == "HEAD\nkey1 -> value1\nkey2 -> value2\n" *
/// * results in s == "HEAD\nkey1 -> value1\nkey2 -> value2\n"
/// Note that lists cannot themselves contain lists - this would be a future feature. *
* 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 class Whiskers
{ {
public: public:
@ -59,8 +71,11 @@ public:
explicit Whiskers(std::string _template); 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, 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>. /// Sets a list parameter, <#listName> </listName>.
Whiskers& operator()( Whiskers& operator()(
std::string _listParameter, std::string _listParameter,
@ -75,6 +90,7 @@ private:
static std::string replace( static std::string replace(
std::string const& _template, std::string const& _template,
StringMap const& _parameters, StringMap const& _parameters,
std::map<std::string, bool> const& _conditions,
StringListMap const& _listParameters = StringListMap() StringListMap const& _listParameters = StringListMap()
); );
@ -83,6 +99,7 @@ private:
std::string m_template; std::string m_template;
StringMap m_parameters; StringMap m_parameters;
std::map<std::string, bool> m_conditions;
StringListMap m_listParameters; StringListMap m_listParameters;
}; };