WikiFunctions.Tools.UpdateTemplateParameterValue C# (CSharp) Method

UpdateTemplateParameterValue() public static method

Sets the template parameter value to the new value input, only if the template already has the parameter (with or without a value)
public static UpdateTemplateParameterValue ( string templateCall, string parameter, string newvalue ) : string
templateCall string The template call to update
parameter string The template parameter
newvalue string The new value for the parameter
return string
        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;
        }
Tools