Beispiel #1
0
        /// <summary>
        /// Parses a Mustache-style markup
        /// </summary>
        /// <param name="content">Mustache-style markup</param>
        /// <param name="mustacheStyleTagHandler">Mustache-style tags handler</param>
        /// <param name="textHandler">Text handler</param>
        public static void ParseMarkup(string content, MustacheStyleTagDelegate mustacheStyleTagHandler,
                                       TextDelegate textHandler)
        {
            var innerContext = new InnerMarkupParsingContext(content);
            var context      = new MarkupParsingContext(innerContext);

            MatchCollection mustacheStyleTagMatches = _mustacheStyleTagRegex.Matches(content);

            if (mustacheStyleTagMatches.Count == 0)
            {
                if (textHandler != null)
                {
                    textHandler(context, content);
                }

                innerContext.IncreasePosition(content.Length);

                return;
            }

            int currentPosition = 0;
            int endPosition     = content.Length - 1;

            foreach (Match mustacheStyleTagMatch in mustacheStyleTagMatches)
            {
                int mustacheStyleTagPosition = mustacheStyleTagMatch.Index;
                int mustacheStyleTagLength   = mustacheStyleTagMatch.Length;

                if (mustacheStyleTagPosition > currentPosition)
                {
                    string text = content.Substring(currentPosition, mustacheStyleTagPosition - currentPosition);

                    if (textHandler != null)
                    {
                        textHandler(context, text);
                    }

                    innerContext.IncreasePosition(text.Length);
                }

                GroupCollection mustacheStyleTagGroups = mustacheStyleTagMatch.Groups;

                string expression     = mustacheStyleTagGroups["expression"].Value;
                string startDelimiter = mustacheStyleTagGroups["startDelimiter"].Value;
                string endDelimiter   = mustacheStyleTagGroups["endDelimiter"].Value;

                if (expression.StartsWith("{") && expression.EndsWith("}"))
                {
                    expression     = expression.Substring(1, expression.Length - 2);
                    startDelimiter = "{{{";
                    endDelimiter   = "}}}";
                }

                if (mustacheStyleTagHandler != null)
                {
                    mustacheStyleTagHandler(context, expression, startDelimiter, endDelimiter);
                }

                innerContext.IncreasePosition(mustacheStyleTagLength);
                currentPosition = mustacheStyleTagPosition + mustacheStyleTagLength;
            }

            if (currentPosition > 0 && currentPosition <= endPosition)
            {
                string text = content.Substring(currentPosition, endPosition - currentPosition + 1);

                if (textHandler != null)
                {
                    textHandler(context, text);
                }

                innerContext.IncreasePosition(text.Length);
            }
        }
        /// <summary>
        /// Parses a Mustache-style markup
        /// </summary>
        /// <param name="content">Mustache-style markup</param>
        /// <param name="mustacheStyleTagHandler">Mustache-style tags handler</param>
        /// <param name="textHandler">Text handler</param>
        public static void ParseMarkup(string content, MustacheStyleTagDelegate mustacheStyleTagHandler,
			TextDelegate textHandler)
        {
            var innerContext = new InnerMarkupParsingContext(content);
            var context = new MarkupParsingContext(innerContext);

            MatchCollection mustacheStyleTagMatches = _mustacheStyleTagRegex.Matches(content);
            if (mustacheStyleTagMatches.Count == 0)
            {
                if (textHandler != null)
                {
                    textHandler(context, content);
                }

                innerContext.IncreasePosition(content.Length);

                return;
            }

            int currentPosition = 0;
            int endPosition = content.Length - 1;

            foreach (Match mustacheStyleTagMatch in mustacheStyleTagMatches)
            {
                int mustacheStyleTagPosition = mustacheStyleTagMatch.Index;
                int mustacheStyleTagLength = mustacheStyleTagMatch.Length;

                if (mustacheStyleTagPosition > currentPosition)
                {
                    string text = content.Substring(currentPosition, mustacheStyleTagPosition - currentPosition);

                    if (textHandler != null)
                    {
                        textHandler(context, text);
                    }

                    innerContext.IncreasePosition(text.Length);
                }

                GroupCollection mustacheStyleTagGroups = mustacheStyleTagMatch.Groups;

                string expression = mustacheStyleTagGroups["expression"].Value;
                string startDelimiter = mustacheStyleTagGroups["startDelimiter"].Value;
                string endDelimiter = mustacheStyleTagGroups["endDelimiter"].Value;

                if (expression.StartsWith("{") && expression.EndsWith("}"))
                {
                    expression = expression.Substring(1, expression.Length - 2);
                    startDelimiter = "{{{";
                    endDelimiter = "}}}";
                }

                if (mustacheStyleTagHandler != null)
                {
                    mustacheStyleTagHandler(context, expression, startDelimiter, endDelimiter);
                }

                innerContext.IncreasePosition(mustacheStyleTagLength);
                currentPosition = mustacheStyleTagPosition + mustacheStyleTagLength;
            }

            if (currentPosition > 0 && currentPosition <= endPosition)
            {
                string text = content.Substring(currentPosition, endPosition - currentPosition + 1);

                if (textHandler != null)
                {
                    textHandler(context, text);
                }

                innerContext.IncreasePosition(text.Length);
            }
        }