Example #1
0
        private static void VerifyHyperlink(Inline expected, Inline actual)
        {
            actual.Should().BeOfType(expected.GetType());

            Hyperlink expectedLink = expected as Hyperlink;
            Hyperlink actualLink   = actual as Hyperlink;

            actualLink.Inlines.Count.Should().Be(expectedLink.Inlines.Count);
            (actualLink.Inlines.FirstInline as Run).Text.Should().Be((expectedLink.Inlines.FirstInline as Run).Text);
            Tuple <int, object> tagActual   = actualLink.Tag as Tuple <int, object>;
            Tuple <int, object> tagExpected = actualLink.Tag as Tuple <int, object>;

            tagActual.Item1.Should().Be(tagExpected.Item1);
            tagActual.Item2.Should().Be(tagExpected.Item2);
        }
Example #2
0
        protected static string ParseInline(Inline inline)
        {
            string text = "";

            Type type = inline.GetType();

            if (type == typeof(Span))
            {
                Span span = inline as Span;
                foreach (Inline subInline in span.Inlines)
                {
                    text += ParseInline(subInline);
                }
            }
            else if (type == typeof(Run))
            {
                Run run = inline as Run;
                text += run.Text;
            }

            return(text);
        }
Example #3
0
 public void FormatInline(FlowDocument doc, Inline inline, StringBuilder buf, int indent)
 {
     // indent
     buf.Append(' ', indent);
     buf.AppendFormat(
         "{0} {1} to {2}",
         inline.GetType().Name + inline.GetHashCode(),
         inline.ContentStart.CompareTo(doc.ContentStart),
         inline.ContentEnd.CompareTo(doc.ContentStart));
     if (inline is Run)
     {
         buf.Append("  \"" + ((Run)inline).Text + "\"");
     }
     buf.AppendLine();
     if (inline is Underline)
     {
         FormatInlines(doc, ((Underline)inline).Inlines, buf, indent + 3);
     }
     if (inline is Italic)
     {
         FormatInlines(doc, ((Italic)inline).Inlines, buf, indent + 3);
     }
 }
Example #4
0
        private Span[] CreateSpans(Inline inline, FontAttributes attributes = FontAttributes.None, Color?foregroundColor = null)
        {
            switch (inline)
            {
            case LiteralInline literal:
                var span = new Span
                {
                    Text           = literal.Content.Text.Substring(literal.Content.Start, literal.Content.Length),
                    FontAttributes = attributes
                };

                if (foregroundColor.HasValue)
                {
                    span.ForegroundColor = foregroundColor.Value;
                }

                return(new[] { span });

            case EmphasisInline emphasis:
                var childAttributes = attributes | (emphasis.DelimiterCount == 2 ? FontAttributes.Bold : FontAttributes.Italic);
                return(emphasis.SelectMany(x => CreateSpans(x, childAttributes)).ToArray());

            case LineBreakInline breakline:
                return(new[] { new Span {
                                   Text = "\n"
                               } });

            case LinkInline link:
                // TODO: make styling of this more customisable and less hacky.

                var url = link.Url;

                if (link.IsImage)
                {
                    var image = ImageTemplate.CreateContent() as View;
                    image.BindingContext = new Templates.ImageAstNode
                    {
                        Url = url
                    };

                    queuedViews.Add(image);
                    return(new Span[0]);
                }
                else
                {
                    var spans = link.SelectMany(x => CreateSpans(x, foregroundColor: Color.FromHex("#0366d6"))).ToArray();
                    links.Add(new KeyValuePair <string, string>(string.Join("", spans.Select(x => x.Text)), url));
                    return(spans);
                }

            case CodeInline code:
                // TODO: make this customisable + less hacky.
                return(new[]
                {
                    new Span
                    {
                        Text = "\u2002" + code.Content + "\u2002",
                        ForegroundColor = Color.FromHex("#24292e"),
                        BackgroundColor = Color.FromHex("#f6f8fa"),
                        FontFamily = Device.RuntimePlatform == Device.iOS
                                       ? "Courier"
                                       : "monospace"
                    }
                });

            default:
                Debug.WriteLine($"Can't render {inline.GetType()} inlines.");
                return(null);
            }
        }
Example #5
0
        private Span[] CreateSpans(Inline inline, string family, FontAttributes attributes, Color foregroundColor, Color backgroundColor, float size)
        {
            switch (inline)
            {
            case LiteralInline literal:
                return(new[]
                {
                    new Span
                    {
                        Text = literal.Content.Text.Substring(literal.Content.Start, literal.Content.Length),
                        FontAttributes = attributes,
                        ForegroundColor = foregroundColor,
                        BackgroundColor = backgroundColor,
                        FontSize = size,
                        FontFamily = family,
                    }
                });

            case EmphasisInline emphasis:
                var childAttributes = attributes | (emphasis.IsDouble ? FontAttributes.Bold : FontAttributes.Italic);
                return(emphasis.SelectMany(x => CreateSpans(x, family, childAttributes, foregroundColor, backgroundColor, size)).ToArray());

            case LineBreakInline breakline:
                return(new[] { new Span {
                                   Text = "\n"
                               } });

            case LinkInline link:

                var url = link.Url;

                if (!(url.StartsWith("http://") || url.StartsWith("https://") || url.StartsWith("tel:") || url.StartsWith("sms:") || url.StartsWith("mailto:")))
                {
                    url = $"{this.RelativeUrlHost?.TrimEnd('/')}/{url.TrimStart('/')}";
                }

                if (link.IsImage)
                {
                    var image = new Image();

                    if (Path.GetExtension(url) == ".svg")
                    {
                        image.RenderSvg(url);
                    }
                    else
                    {
                        image.Source = url;
                    }

                    queuedViews.Add(image);
                    return(new Span[0]);
                }
                else
                {
                    var spans = link.SelectMany(x => CreateSpans(x, this.Theme.Link.FontFamily ?? family, this.Theme.Link.Attributes, this.Theme.Link.ForegroundColor, this.Theme.Link.BackgroundColor, size)).ToArray();
                    links.Add(new KeyValuePair <string, string>(string.Join("", spans.Select(x => x.Text)), url));
                    return(spans);
                }

            case CodeInline code:
                return(new[]
                {
                    new Span()
                    {
                        Text = "\u2002",
                        FontSize = size,
                        FontFamily = this.Theme.Code.FontFamily,
                        ForegroundColor = this.Theme.Code.ForegroundColor,
                        BackgroundColor = this.Theme.Code.BackgroundColor
                    },
                    new Span
                    {
                        Text = code.Content,
                        FontAttributes = this.Theme.Code.Attributes,
                        FontSize = size,
                        FontFamily = this.Theme.Code.FontFamily,
                        ForegroundColor = this.Theme.Code.ForegroundColor,
                        BackgroundColor = this.Theme.Code.BackgroundColor
                    },
                    new Span()
                    {
                        Text = "\u2002",
                        FontSize = size,
                        FontFamily = this.Theme.Code.FontFamily,
                        ForegroundColor = this.Theme.Code.ForegroundColor,
                        BackgroundColor = this.Theme.Code.BackgroundColor
                    },
                });

            default:
                Debug.WriteLine($"Can't render {inline.GetType()} inlines.");
                return(null);
            }
        }
Example #6
0
 protected virtual void AddNotImplementedInlineText(Inline inline)
 => AddText(inline.GetType() + " not implemented.", t => t.Colour = Color4.Red);
Example #7
0
 private static void VerifyTextRun(Inline expected, Inline actual)
 {
     actual.Should().BeOfType(expected.GetType());
     (actual as Run).Text.Should().Be((expected as Run).Text);
 }
Example #8
0
        Span[] CreateSpans(Inline inline, string family, FontAttributes attributes, TextDecorations textDecorations, Color foregroundColor, Color backgroundColor, float size, float lineHeight)
        {
            switch (inline)
            {
            case LiteralInline literal:
                return(new[]
                {
                    new Span
                    {
                        Text = literal.Content.Text.Substring(literal.Content.Start, literal.Content.Length),
                        FontAttributes = attributes,
                        ForegroundColor = foregroundColor,
                        BackgroundColor = backgroundColor,
                        FontSize = size,
                        FontFamily = family,
                        LineHeight = lineHeight,
                        TextDecorations = textDecorations,
                    }
                });

            case EmphasisInline emphasis:
                var childAttributes = attributes | (emphasis.IsDouble ? FontAttributes.Bold : FontAttributes.Italic);
                return(emphasis.SelectMany(x => CreateSpans(x, family, childAttributes, textDecorations, foregroundColor, backgroundColor, size, lineHeight)).ToArray());

            case LineBreakInline breakline:
                return(new[] { new Span {
                                   Text = "\n"
                               } });

            case LinkInline link:

                var url = link.Url;

                if (!Theme.Link.ExternalProtocols.Any(o => url.StartsWith(o)))
                {
                    url = $"{RelativeUrlHost?.TrimEnd('/')}/{url.TrimStart('/')}";
                }

                if (TryLoadYouTubePreview(link.Url, out var youtubePreview))
                {
                    queuedViews.Add(youtubePreview);
                    return(new Span[0]);
                }
                else if (link.IsImage)
                {
                    var image = new Image();

                    if (Path.GetExtension(url) == ".svg")
                    {
                        image.RenderSvg(url);
                    }
                    else
                    {
                        image.Source = url;
                    }

                    queuedViews.Add(image);
                    return(new Span[0]);
                }
                else
                {
                    var spans = link.SelectMany(x => CreateSpans(x, Theme.Link.FontFamily ?? family, Theme.Link.Attributes, Theme.Link.TextDecorations, Theme.Link.ForegroundColor, Theme.Link.BackgroundColor, size, lineHeight)).ToArray();
                    links.Add(new KeyValuePair <string, string>(string.Join("", spans.Select(x => x.Text)), url));
                    return(spans);
                }

            case AutolinkInline autolink:

                return(RenderAutolink(autolink, size, lineHeight, family));

            case CodeInline code:
                return(new[]
                {
                    new Span()
                    {
                        Text = "\u2002",
                        FontSize = size,
                        FontFamily = Theme.Code.FontFamily,
                        ForegroundColor = Theme.Code.ForegroundColor,
                        BackgroundColor = Theme.Code.BackgroundColor
                    },
                    new Span
                    {
                        Text = code.Content,
                        FontAttributes = Theme.Code.Attributes,
                        TextDecorations = Theme.Code.TextDecorations,
                        FontSize = size,
                        FontFamily = Theme.Code.FontFamily,
                        ForegroundColor = Theme.Code.ForegroundColor,
                        BackgroundColor = Theme.Code.BackgroundColor
                    },
                    new Span()
                    {
                        Text = "\u2002",
                        FontSize = size,
                        FontFamily = Theme.Code.FontFamily,
                        ForegroundColor = Theme.Code.ForegroundColor,
                        BackgroundColor = Theme.Code.BackgroundColor
                    },
                });

            default:
                Debug.WriteLine($"Can't render {inline.GetType()} inlines.");
                return(new Span[0]);
            }
        }