Beispiel #1
0
        private TextElement[] CreateText(string text)
        {
            var results = new List <TextElement>();

            if (text.Length == 0)
            {
                return(new TextElement[0]);
            }
            var options = new[]
            {
                new CreateTextOption()
                {
                    Pattern   = inlineCodeMatcher,
                    Evaluator = (match) => new InlineCode(match.Groups[1].Value)
                },
                new CreateTextOption()
                {
                    Pattern   = formulaMatcher,
                    Evaluator = (match) => new Formula(match.Groups[1].Value),
                },
                new CreateTextOption()
                {
                    Pattern   = urlMatcher,
                    Evaluator = (match) =>
                    {
                        var link = new Link();
                        link.Target = match.Groups[2].Value;
                        foreach (var item in CreateText(match.Groups[1].Value))
                        {
                            link.Add(item);
                        }
                        return(link);
                    },
                },
                new CreateTextOption()
                {
                    Pattern   = boldMatcher,
                    Evaluator = (match) =>
                    {
                        var bold = new Bold();
                        foreach (var item in CreateText(match.Groups[1].Value))
                        {
                            bold.Add(item);
                        }
                        return(bold);
                    },
                },
                new CreateTextOption()
                {
                    Pattern   = italicMatcher,
                    Evaluator = (match) =>
                    {
                        var bold = new Italic();
                        foreach (var item in CreateText(match.Groups[1].Value))
                        {
                            bold.Add(item);
                        }
                        return(bold);
                    },
                },
            };

            for (int i = 0; i < options.Length; i++)
            {
                if (ProcessPattern(results, text, options[i].Pattern, options[i].Evaluator))
                {
                    return(results.ToArray());
                }
            }
            return(new[] { new Run(text) });
        }