Beispiel #1
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));
        }
Beispiel #2
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));
        }
Beispiel #3
0
        public ITextLayout CreateLaytout(string str, SyntaxInfo[] syntaxCollection, IEnumerable <Marker> MarkerRanges, IEnumerable <Selection> SelectRanges, double wrapwidth)
        {
            TextLayout layout;

            if (wrapwidth == LineToIndexTable.NONE_BREAK_LINE)
            {
                layout = new TextLayout(str, this.FontFamily, this.FontSize, Brushes[this.Foreground], this.TextArea.Width);
                layout.TextWarpping = TextWrapping.NoWrap;
            }
            else
            {
                layout = new TextLayout(str, this.FontFamily, this.FontSize, Brushes[this.Foreground], wrapwidth);
                layout.TextWarpping = TextWrapping.Wrap;
            }
            layout.FlowDirection = this.RightToLeft ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;

            if (syntaxCollection != null)
            {
                foreach (SyntaxInfo s in syntaxCollection)
                {
                    Brush brush = this.Brushes[this.Foreground];
                    switch (s.type)
                    {
                    case TokenType.Comment:
                        brush = this.Brushes[this.Comment];
                        break;

                    case TokenType.Keyword1:
                        brush = this.Brushes[this.Keyword1];
                        break;

                    case TokenType.Keyword2:
                        brush = this.Brushes[this.Keyword2];
                        break;

                    case TokenType.Literal:
                        brush = this.Brushes[this.Literal];
                        break;
                    }
                    TextEffect effect = new TextEffect(null, brush, null, s.index, s.length);
                    effect.Freeze();
                    layout.SetTextEffect(effect);
                }
            }

            if (MarkerRanges != null)
            {
                foreach (Marker m in MarkerRanges)
                {
                    if (m.start == -1 || m.length == 0)
                    {
                        continue;
                    }

                    Brush brush;
                    if (m.hilight == HilightType.Url)
                    {
                        brush = this.Brushes[this.Url];
                        TextEffect effect = new TextEffect(null, brush, null, m.start, m.length);
                        effect.Freeze();
                        layout.SetTextEffect(effect);
                    }
                    else
                    {
                        System.Windows.Media.Color color = new System.Windows.Media.Color();
                        color.A = m.color.A;
                        color.R = m.color.R;
                        color.G = m.color.G;
                        color.B = m.color.B;
                        brush   = this.Brushes[color];
                    }

                    Pen pen = this.Pens.Get(brush, m.hilight);

                    TextDecorationCollection collection = new TextDecorationCollection();
                    TextDecoration           decoration = new TextDecoration();
                    decoration.Pen      = pen;
                    decoration.Location = TextDecorationLocation.Underline;
                    decoration.Freeze();
                    collection.Add(decoration);

                    if (m.hilight == HilightType.Squiggle)
                    {
                        layout.SetSquilleLine(m.start, m.length, collection);
                    }
                    else
                    {
                        layout.SetTextDecoration(m.start, m.length, collection);
                    }
                }
            }

            return(layout);
        }