public static string UpdateTemplateParameterValue(string templateCall, string parameter, string newvalue)
{
// HACK we are allowing matching on tilde character around parameter name to represent cleaned HTML comment, so may falsely match
// on stray templates with stray tildes. Will that ever happen?
Regex paramRegex = new Regex(@"\|[\s~]*" + Regex.Escape(parameter) + @"[\s~]*= *\s*?(.*?)\s*(?=(?:\||}}$))", RegexOptions.Singleline);
string pipecleanedtemplate = PipeCleanedTemplate(templateCall, true);
Match m = paramRegex.Match(pipecleanedtemplate);
if (m.Success)
{
int start = m.Groups[1].Index, valuelength = m.Groups[1].Length;
// retain any newlines at start of parameter value if existing parameter has value
string startNewline = StartWhitespace.Match(m.Groups[1].Value).Value;
if (startNewline.Length > 0 && m.Groups[1].Value.Trim().Length > 0)
newvalue = startNewline + newvalue;
return (templateCall.Substring(0, start) + newvalue + templateCall.Substring(start + valuelength));
}
return templateCall;
}