Exemplo n.º 1
0
        /// <summary>
        /// Replaces the tokens in a given template string by the values found in the token value provider object(s) of this instance.
        /// </summary>
        /// <param name="template">The template string.</param>
        /// <returns>The template string with tokens replaced by the respective values.</returns>
        public string ReplaceTokens(string template)
        {
            var tokenPattern = $@"{LDelim}(?<tokenref>\S[^{RDelim}]*){RDelim}";

            foreach (Match token in Regex.Matches(template, tokenPattern))
            {
                var tokenRef   = TokenRef.Parse(token.Groups["tokenref"].Value);
                var tokenValue = FindTokenValue(tokenRef.TokenName);
                if (tokenValue == null)
                {
                    continue;
                }

                if (tokenRef.IsIndexedValue)
                {
                    tokenValue = GetIndexedValue(tokenValue, tokenRef.Index);
                }

                var formattedTokenValue = string.Format(tokenRef.Format, tokenValue);
                template = template.Replace(token.Value, formattedTokenValue);
            }

            return(template);
        }
 public StringExpression(TokenRef <Tkn> value) => Value = value.ToStringValue();