Ejemplo n.º 1
0
        /// <summary>
        /// Returns the next fragment found in the current unconsumed block of text
        /// </summary>
        /// <returns></returns>
        internal bool Consume()
        {
            if (string.IsNullOrEmpty(_unconsumedTemplateText)) return false;

            string fragmentText = string.Empty;
            var openMatch = Regex.Match(_unconsumedTemplateText, "{{2,3}"); // regex that looks for either 2 or 3 {'s
            var closeMatch = Regex.Match(_unconsumedTemplateText, "}{2,3}"); // regex that looks for either 2 or 3 }'s

            if (openMatch.Success && closeMatch.Success)
            {
                // remaining text contains a placeholder
                // extract the placeholder text including braces
                fragmentText = _unconsumedTemplateText.Substring(openMatch.Index, (closeMatch.Index + closeMatch.Value.Length) - (openMatch.Index)).Trim();
                int fragmentIndex = _unconsumedTemplateText.IndexOf(fragmentText);
                string preText = _unconsumedTemplateText.Substring(0, fragmentIndex);
                string postText = _unconsumedTemplateText.Substring(fragmentIndex + fragmentText.Length);

                if (!string.IsNullOrEmpty(preText))
                {
                    // there is plain text before the placeholder, process that first
                    _unconsumedTemplateText = fragmentText + postText;
                    NextFragment = new TemplateFragment(preText);
                }
                else
                {
                    _unconsumedTemplateText = postText;
                    NextFragment = new TemplateFragment(fragmentText);
                }
            }
            else
            {
                // remaining text is plain text
                var placeholder = new TemplateFragment(_unconsumedTemplateText);
                _unconsumedTemplateText = string.Empty; // finish processing
                NextFragment = placeholder;
            }

            return true;
        }
Ejemplo n.º 2
0
 public IRule Match(TemplateFragment p)
 {
     try
     {
         foreach (var rule in Rules)
         {
             if (rule.RuleApplies(p)) return rule;
         }
     }
     catch (Exception ex)
     {
         throw new Exception("A malformed template fragment was detected: " + p.String, ex);
     }
     throw new Exception("A rule was not found that matches text:" + p.String);
 }