Exemplo n.º 1
0
        private Inline MatchOne(IReadOnlyList <MarkdownToken> tokens, out int consumedCount)
        {
            int totalMatchCount;
            IReadOnlyList <MarkdownToken> match;

            if ((match = Match(tokens, out totalMatchCount,
                               TokenPattern.Required(MarkdownTokenType.Star, MarkdownTokenType.Star),
                               TokenPattern.Optional(MarkdownTokenType.Star, MarkdownTokenType.Star))) != null)
            {
                var children = MatchAll(match);
                var element  = new Bold();
                element.Inlines.AddRange(children);
                consumedCount = 4 + match.Count;
                return(element);
            }

            if ((match = Match(tokens, out totalMatchCount,
                               TokenPattern.Required(MarkdownTokenType.Underscore, MarkdownTokenType.Underscore),
                               TokenPattern.Optional(MarkdownTokenType.Underscore, MarkdownTokenType.Underscore))) != null)
            {
                var children = MatchAll(match);
                var element  = new Bold();
                element.Inlines.AddRange(children);
                consumedCount = 4 + match.Count;
                return(element);
            }

            if ((match = Match(tokens, out totalMatchCount,
                               TokenPattern.Required(MarkdownTokenType.Tilde, MarkdownTokenType.Tilde),
                               TokenPattern.Optional(MarkdownTokenType.Tilde, MarkdownTokenType.Tilde))) != null)
            {
                var children   = MatchAll(match);
                var element    = new Span();
                var decoration = new TextDecoration {
                    Location = TextDecorationLocation.Strikethrough
                };
                decoration.Freeze();
                element.TextDecorations.Add(decoration);
                element.Inlines.AddRange(children);
                consumedCount = 4 + match.Count;
                return(element);
            }

            if ((match = Match(tokens, out totalMatchCount,
                               TokenPattern.Required(MarkdownTokenType.Star),
                               TokenPattern.Optional(MarkdownTokenType.Star))) != null)
            {
                var children = MatchAll(match);
                var element  = new Italic();
                element.Inlines.AddRange(children);
                consumedCount = 2 + match.Count;
                return(element);
            }

            if ((match = Match(tokens, out totalMatchCount,
                               TokenPattern.Required(MarkdownTokenType.Underscore),
                               TokenPattern.Optional(MarkdownTokenType.Underscore))) != null)
            {
                var children = MatchAll(match);
                var element  = new Italic();
                element.Inlines.AddRange(children);
                consumedCount = 2 + match.Count;
                return(element);
            }

            if ((match = Match(tokens, out totalMatchCount,
                               start: TokenPattern.Required(MarkdownTokenType.LineBreak))) != null)
            {
                consumedCount = 1;
                return(new LineBreak());
            }

            if ((match = Match(tokens, out totalMatchCount,
                               start: TokenPattern.Required(MarkdownTokenType.Whitespace))) != null)
            {
                consumedCount = 1;
                return(new Run(" "));
            }

            var token = tokens[index : 0];

            consumedCount = 1;
            return(new Run(token.Text));
        }
Exemplo n.º 2
0
        private Inline MatchOne(IReadOnlyList <MarkdownToken> tokens, out int consumedCount)
        {
            int totalMatchCount;
            IReadOnlyList <MarkdownToken> match;

            if ((match = MatchOneStar(tokens, out totalMatchCount,
                                      TokenPattern.Required(MarkdownTokenType.Star, MarkdownTokenType.Star),
                                      TokenPattern.Optional(MarkdownTokenType.Star, MarkdownTokenType.Star))) != null)
            {
                var children = MatchAll(match);
                var element  = new Bold();
                element.Inlines.AddRange(children);
                consumedCount = 4 + match.Count;
                return(element);
            }

            if ((match = MatchOneStar(tokens, out totalMatchCount,
                                      TokenPattern.Required(MarkdownTokenType.Underscore, MarkdownTokenType.Underscore),
                                      TokenPattern.Optional(MarkdownTokenType.Underscore, MarkdownTokenType.Underscore))) != null)
            {
                var children = MatchAll(match);
                var element  = new Bold();
                element.Inlines.AddRange(children);
                consumedCount = 4 + match.Count;
                return(element);
            }

            if ((match = MatchOneStar(tokens, out totalMatchCount,
                                      TokenPattern.Required(MarkdownTokenType.Tilde, MarkdownTokenType.Tilde),
                                      TokenPattern.Optional(MarkdownTokenType.Tilde, MarkdownTokenType.Tilde))) != null)
            {
                var children   = MatchAll(match);
                var element    = new Span();
                var decoration = new TextDecoration {
                    Location = TextDecorationLocation.Strikethrough
                };
                decoration.Freeze();
                element.TextDecorations.Add(decoration);
                element.Inlines.AddRange(children);
                consumedCount = 4 + match.Count;
                return(element);
            }

            if ((match = MatchOneStar(tokens, out totalMatchCount,
                                      TokenPattern.Required(MarkdownTokenType.Star),
                                      TokenPattern.Optional(MarkdownTokenType.Star))) != null)
            {
                var children = MatchAll(match);
                var element  = new Italic();
                element.Inlines.AddRange(children);
                consumedCount = 2 + match.Count;
                return(element);
            }

            if ((match = MatchOneStar(tokens, out totalMatchCount,
                                      TokenPattern.Required(MarkdownTokenType.Underscore),
                                      TokenPattern.Optional(MarkdownTokenType.Underscore))) != null)
            {
                var children = MatchAll(match);
                var element  = new Italic();
                element.Inlines.AddRange(children);
                consumedCount = 2 + match.Count;
                return(element);
            }

            if (MatchTwoStar(tokens, out totalMatchCount,
                             match1: out var match1,
                             match2: out var match2,
                             start1: TokenPattern.Required(MarkdownTokenType.SquareBracketOpen),
                             end1: TokenPattern.Required(MarkdownTokenType.SquareBracketClose),
                             start2: TokenPattern.Required(MarkdownTokenType.BracketOpen),
                             end2: TokenPattern.Required(MarkdownTokenType.BracketClose)))
            {
                consumedCount = totalMatchCount;
                var uri       = new Uri(ToString(match2));
                var hyperlink = new Hyperlink
                {
                    NavigateUri = uri
                };
                hyperlink.RequestNavigate += (a, b) => HyperlinkRun.NavigateTo(uri, null, null);
                hyperlink.Inlines.AddRange(MatchAll(match1));
                return(hyperlink);
            }

            if ((match = MatchOneStar(tokens, out totalMatchCount,
                                      start: TokenPattern.Required(MarkdownTokenType.LineBreak))) != null)
            {
                consumedCount = 1;
                return(new LineBreak());
            }

            if ((match = MatchOneStar(tokens, out totalMatchCount,
                                      start: TokenPattern.Required(MarkdownTokenType.Whitespace))) != null)
            {
                consumedCount = 1;
                return(new Run(" "));
            }

            var token = tokens[index : 0];

            consumedCount = 1;
            return(new Run(token.Text));
        }