Exemplo n.º 1
0
        /// <summary>
        /// Replaces the text variables by their formatted values.
        /// The original text in the argument remains unchanged.
        /// </summary>
        private StringBuilder ReplaceTextVariablesWithValues(StringBuilder text, List <Variable> textVariables)
        {
            // work on a copy of the orginal text
            var textResult = new StringBuilder(text.ToString());

            if (textResult.Length == 0)
            {
                return(textResult);
            }

            // first read in all external files - this is done only once - no recursion.
            bool textFileWasIncluded = false;

            foreach (Variable variable in textVariables.Where(variable => !string.IsNullOrEmpty(variable.Format) && _formatAsFilename == variable.Format.ToLower()))
            {
                // If a format name called "file" is found, the variable's value is interpreted as a file name
                // The file name may contain variables as well.
                using (TextVariableManager tvm = Clone())
                {
                    string filename = tvm.Process(variable.Value as string ?? string.Empty);
                    // add all new bad variable names to the list
                    foreach (string badVar in tvm.BadVariables.Where(badVar => !BadVariables.Contains(badVar)))
                    {
                        BadVariables.Add(badVar);
                    }
                    filename = MakeFullPath(filename);
                    string content = ReadFile(filename);

                    if (content == null)
                    {
                        if (FileVariableErrors == VariableError.ReplaceWithEmptyString)
                        {
                            textResult = textResult.Replace(variable.MatchingString, string.Empty);
                        }
                    }
                    else
                    {
                        textResult = textResult.Replace(variable.MatchingString, content);
                    }

                    textFileWasIncluded = true;
                }
            }

            // if a text file was included, re-search and replace the text for variables
            if (textFileWasIncluded)
            {
                foreach (Variable variable in SearchTextVariables(textResult).Where(variable => variable.Format != _formatAsFilename || FileVariableErrors != VariableError.ShowTextVariable))
                {
                    textResult = textResult.Replace(variable.MatchingString, variable.ToString());
                }
            }

            // Next replace variables for the whole text (including any text files read before).
            // If a text file was included, it could again contain variables - but do NOT process
            // external text files recursively.

            return(textVariables.Aggregate(textResult, (current, variable) => current.Replace(variable.MatchingString, variable.ToString())));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Replaces all variables in the text with their corresponding values.
        /// Used for subject, body and attachment.
        /// </summary>
        /// <param name="text">Text to search and replace.</param>
        /// <returns>Returns the text with all variables replaced.</returns>
        private StringBuilder SearchAndReplaceVars(StringBuilder text)
        {
            _textVariableManager.Text = text;
            StringBuilder toReturn = _textVariableManager.Process();

            _badVariableNames = _textVariableManager.BadVariables;
            _badInlineFiles   = _textVariableManager.BadFiles;
            return(toReturn);
        }