Ejemplo n.º 1
0
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] EmphasisInline obj)
        {
            Span span = null;

            switch (obj.DelimiterChar)
            {
            case '*':
            case '_':
                span = obj.IsDouble ? (Span) new Bold() : new Italic();
                break;

            case '~':
                span = new Span();
                if (obj.IsDouble)
                {
                    renderer.Styles.ApplyStrikeThroughStyle(span);
                }
                else
                {
                    renderer.Styles.ApplySubscriptStyle(span);
                    if (span.FontSize < 1)
                    {
                        span.FontSize = renderer.CurrentFontSize() * span.FontSize;
                    }
                }
                break;

            case '^':
                span = new Span();
                renderer.Styles.ApplySuperscriptStyle(span);
                if (span.FontSize < 1)
                {
                    span.FontSize = renderer.CurrentFontSize() * span.FontSize;
                }
                break;

            case '+':
                span = new Span();
                renderer.Styles.ApplyInsertedStyle(span);
                break;

            case '=':
                span = new Span();
                renderer.Styles.ApplyMarkedStyle(span);
                break;
            }

            if (span != null)
            {
                span.Tag = obj;
                renderer.Push(span);
                renderer.WriteChildren(obj);
                renderer.Pop();
            }
            else
            {
                renderer.WriteChildren(obj);
            }
        }
        protected override void Write(WpfRenderer renderer, EmphasisInline obj)
        {
            if (renderer == null)
            {
                throw new ArgumentNullException(nameof(renderer));
            }
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            Span?span = null;

            switch (obj.DelimiterChar)
            {
            case '*':
            case '_':
                span = obj.DelimiterCount == 2 ? (Span) new Bold() : new Italic();
                break;

            case '~':
                span = new Span();
                span.SetResourceReference(FrameworkContentElement.StyleProperty, obj.DelimiterCount == 2 ? Styles.StrikeThroughStyleKey : Styles.SubscriptStyleKey);
                break;

            case '^':
                span = new Span();
                span.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.SuperscriptStyleKey);
                break;

            case '+':
                span = new Span();
                span.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.InsertedStyleKey);
                break;

            case '=':
                span = new Span();
                span.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.MarkedStyleKey);
                break;
            }

            if (span != null)
            {
                renderer.Push(span);
                renderer.WriteChildren(obj);
                renderer.Pop();
            }
            else
            {
                renderer.WriteChildren(obj);
            }
        }
Ejemplo n.º 3
0
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] CodeBlock obj)
        {
            var paragraph = new Paragraph()
            {
                Tag = obj
            };

            renderer.Styles.ApplyCodeBlockStyle(paragraph);
            renderer.Push(paragraph);

            if (obj.Inline != null)
            {
                // there was a post-processor which has already processed the lines in this code block
                renderer.WriteChildren(obj.Inline);
            }
            else // there was no post-processor - we have to do the writing of the code lines
            {
                // original code: renderer.WriteLeafRawLines(obj); // Expand this call directly here in order to be able to include tags
                var lines = obj.Lines;
                if (lines.Lines != null)
                {
                    var slices = lines.Lines;
                    for (var i = 0; i < lines.Count; i++)
                    {
                        renderer.WriteInline(new Run(slices[i].Slice.ToString()));
                        renderer.WriteInline(new LineBreak());
                    }
                }
            }

            renderer.Pop();
        }
Ejemplo n.º 4
0
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] ListBlock listBlock)
        {
            var list = new List();

            if (listBlock.IsOrdered)
            {
                list.MarkerStyle = TextMarkerStyle.Decimal;

                if (listBlock.OrderedStart != null && (listBlock.DefaultOrderedStart != listBlock.OrderedStart))
                {
                    list.StartIndex = int.Parse(listBlock.OrderedStart);
                }
            }
            else
            {
                list.MarkerStyle = TextMarkerStyle.Disc;
            }

            renderer.Push(list);

            foreach (var item in listBlock)
            {
                var listItemBlock = (ListItemBlock)item;
                var listItem      = new ListItem();
                renderer.Push(listItem);
                renderer.WriteChildren(listItemBlock);
                renderer.Pop();
            }

            renderer.Pop();
        }
Ejemplo n.º 5
0
        /// <inheritdoc/>
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] LinkInline link)
        {
            var url = link.GetDynamicUrl != null?link.GetDynamicUrl() ?? link.Url : link.Url;

            if (!Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
            {
                url = "#";
            }

            if (link.IsImage)
            {
                var image = new Image
                {
                    Source = new BitmapImage(new Uri(url))
                };

                image.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.ImageStyleKey);

                renderer.WriteInline(new InlineUIContainer(image));
            }
            else
            {
                var hyperlink = new Hyperlink
                {
                    Command          = Commands.Hyperlink,
                    CommandParameter = url,
                    NavigateUri      = new Uri(url, UriKind.RelativeOrAbsolute),
                    ToolTip          = link.Title != string.Empty ? link.Title : null,
                };

                renderer.Push(hyperlink);
                renderer.WriteChildren(link);
                renderer.Pop();
            }
        }
Ejemplo n.º 6
0
        protected override void Write([NotNull] WpfRenderer renderer, EmphasisInline obj)
        {
            var inline = GetInline(obj);

            // TODO: "render" the inline
            renderer.WriteChildren(obj);
            // TODO: add children to the inline
            // Note: could be a push/pop API with a stack. Pop automatically adds children into the inline and removes it from the stack.
        }
Ejemplo n.º 7
0
        /// <inheritdoc/>
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] QuoteBlock obj)
        {
            var section = new Section();

            renderer.Push(section);
            renderer.WriteChildren(obj);
            section.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.QuoteBlockStyleKey);
            renderer.Pop();
        }
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] EmphasisInline obj)
        {
            Span span = null;

            switch (obj.DelimiterChar)
            {
            case '*':
            case '_':
                span = obj.IsDouble ? (Span) new Bold() : new Italic();
                break;

            case '~':
                span = new Span();
                span.SetResourceReference(FrameworkContentElement.StyleProperty, obj.IsDouble ? Styles.StrikeThroughStyleKey: Styles.SubscriptStyleKey);
                break;

            case '^':
                span = new Span();
                span.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.SuperscriptStyleKey);
                break;

            case '+':
                span = new Span();
                span.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.InsertedStyleKey);
                break;

            case '=':
                span = new Span();
                span.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.MarkedStyleKey);
                break;
            }

            if (span != null)
            {
                renderer.Push(span);
                renderer.WriteChildren(obj);
                renderer.Pop();
            }
            else
            {
                renderer.WriteChildren(obj);
            }
        }
Ejemplo n.º 9
0
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] EmphasisInline obj)
        {
            Span span = null;

            if (obj.DelimiterChar == '*' || obj.DelimiterChar == '_')
            {
                span = obj.IsDouble ? (Span) new Bold() : new Italic();
            }

            if (span != null)
            {
                renderer.Push(span);
                renderer.WriteChildren(obj);
                renderer.Pop();
            }
            else
            {
                renderer.WriteChildren(obj);
            }
        }
Ejemplo n.º 10
0
        /// <inheritdoc/>
        protected override void Write(WpfRenderer renderer, LinkInline link)
        {
            if (renderer == null)
            {
                throw new ArgumentNullException(nameof(renderer));
            }
            if (link == null)
            {
                throw new ArgumentNullException(nameof(link));
            }

            var url = link.GetDynamicUrl != null?link.GetDynamicUrl() ?? link.Url : link.Url;

            if (!Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
            {
                url = "#";
            }

            if (link.IsImage)
            {
                var template = new ControlTemplate();
                var image    = new FrameworkElementFactory(typeof(Image));
                image.SetValue(Image.SourceProperty, new BitmapImage(new Uri(url, UriKind.RelativeOrAbsolute)));
                image.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.ImageStyleKey);
                template.VisualTree = image;

                var btn = new Button()
                {
                    Template         = template,
                    Command          = Commands.Image,
                    CommandParameter = url
                };

                renderer.WriteInline(new InlineUIContainer(btn));
            }
            else
            {
                var hyperlink = new Hyperlink
                {
                    Command          = Commands.Hyperlink,
                    CommandParameter = url,
                    NavigateUri      = new Uri(url, UriKind.RelativeOrAbsolute),
                    ToolTip          = !string.IsNullOrEmpty(link.Title) ? link.Title : null,
                };

                hyperlink.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.HyperlinkStyleKey);

                renderer.Push(hyperlink);
                renderer.WriteChildren(link);
                renderer.Pop();
            }
        }
Ejemplo n.º 11
0
        /// <inheritdoc/>
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] QuoteBlock obj)
        {
            var section = new Section()
            {
                Tag = obj
            };

            renderer.Styles.ApplyQuoteBlockStyle(section);

            renderer.Push(section);
            renderer.WriteChildren(obj);
            renderer.Pop();
        }
Ejemplo n.º 12
0
        /// <inheritdoc/>
        protected override void Write(WpfRenderer renderer, DelimiterInline obj)
        {
            if (renderer == null)
            {
                throw new ArgumentNullException(nameof(renderer));
            }
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            renderer.WriteText(obj.ToLiteral());
            renderer.WriteChildren(obj);
        }
Ejemplo n.º 13
0
        /// <inheritdoc/>
        protected override void Write(WpfRenderer renderer, QuoteBlock obj)
        {
            if (renderer == null)
            {
                throw new ArgumentNullException(nameof(renderer));
            }
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            var section = new Section();

            renderer.Push(section);
            renderer.WriteChildren(obj);
            section.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.QuoteBlockStyleKey);
            renderer.Pop();
        }
Ejemplo n.º 14
0
        protected override void Write(WpfRenderer renderer, ListBlock listBlock)
        {
            if (renderer == null)
            {
                throw new ArgumentNullException(nameof(renderer));
            }
            if (listBlock == null)
            {
                throw new ArgumentNullException(nameof(listBlock));
            }

            var list = new List();

            if (listBlock.IsOrdered)
            {
                list.MarkerStyle = TextMarkerStyle.Decimal;

                if (listBlock.OrderedStart != null && (listBlock.DefaultOrderedStart != listBlock.OrderedStart))
                {
                    list.StartIndex = int.Parse(listBlock.OrderedStart, NumberFormatInfo.InvariantInfo);
                }
            }
            else
            {
                list.MarkerStyle = TextMarkerStyle.Disc;
            }

            renderer.Push(list);

            foreach (var item in listBlock)
            {
                var listItemBlock = (ListItemBlock)item;
                var listItem      = new ListItem();
                renderer.Push(listItem);
                renderer.WriteChildren(listItemBlock);
                renderer.Pop();
            }

            renderer.Pop();
        }
Ejemplo n.º 15
0
 /// <inheritdoc/>
 protected override void Write([NotNull] WpfRenderer renderer, [NotNull] DelimiterInline obj)
 {
     renderer.WriteText(obj.ToLiteral());
     renderer.WriteChildren(obj);
 }
Ejemplo n.º 16
0
        /// <inheritdoc/>
        protected override void Write([NotNull] WpfRenderer renderer, [NotNull] LinkInline link)
        {
            var url = link.GetDynamicUrl != null?link.GetDynamicUrl() ?? link.Url : link.Url;

            if (link.IsImage)
            {
                var inline = renderer.ImageProvider.GetInlineItem(url, out var inlineItemIsErrorMessage);

                double?width = null, height = null;

                if (link.ContainsData(typeof(Markdig.Renderers.Html.HtmlAttributes)))
                {
                    var htmlAttributes = (Markdig.Renderers.Html.HtmlAttributes)link.GetData(typeof(Markdig.Renderers.Html.HtmlAttributes));
                    if (null != htmlAttributes.Properties)
                    {
                        foreach (var entry in htmlAttributes.Properties)
                        {
                            switch (entry.Key.ToLowerInvariant())
                            {
                            case "width":
                                width = GetLength(entry.Value);
                                break;

                            case "height":
                                height = GetLength(entry.Value);
                                break;
                            }
                        }
                    }
                }

                if (null != inline)
                {
                    inline.Tag = link;

                    if (inline is InlineUIContainer container && container.Child is Image image)
                    {
                        renderer.Styles.ApplyImageStyle(image);

                        var bitmapSource = image.Source as BitmapSource;

                        if (width.HasValue && height.HasValue)
                        {
                            // then we do a non-uniform stretching
                            if (null != bitmapSource && (bitmapSource.PixelWidth < width.Value || bitmapSource.PixelHeight < height.Value))
                            {
                                // we have to use scale to up-scale the image
                                image.LayoutTransform = new System.Windows.Media.ScaleTransform(width.Value / image.Source.Width, height.Value / image.Source.Height);
                                image.Width           = width.Value;
                                image.Height          = height.Value;
                                image.Stretch         = System.Windows.Media.Stretch.Fill;
                            }
                            else
                            {
                                image.Width   = width.Value;
                                image.Height  = height.Value;
                                image.Stretch = System.Windows.Media.Stretch.Fill;
                            }
                        }
                        else if (width.HasValue)
                        {
                            if (null != bitmapSource && bitmapSource.PixelWidth < width.Value)
                            {
                                // we have to use scale to up-scale the image
                                double scale = width.Value / bitmapSource.PixelWidth;
                                image.LayoutTransform = new System.Windows.Media.ScaleTransform(scale, scale);
                            }
                            else
                            {
                                image.Width   = width.Value;
                                image.Stretch = System.Windows.Media.Stretch.Uniform;
                            }
                        }
                        else if (height.HasValue)
                        {
                            if (null != bitmapSource && bitmapSource.PixelHeight < height.Value)
                            {
                                // we have to use scale to up-scale the image
                                double scale = height.Value / bitmapSource.PixelHeight;
                                image.LayoutTransform = new System.Windows.Media.ScaleTransform(scale, scale);
                            }
                            else
                            {
                                image.Height  = height.Value;
                                image.Stretch = System.Windows.Media.Stretch.Uniform;
                            }
                        }
                        else // neither width nor height provided
                        {
                            // it seems like a bug (or a feature?) in Wpf that to determine the size of the image,
                            // _not_ the width and height property of the image source is used.
                            // Instead it seems here that the PixelWidth and the PixelHeight is used and interpreted
                            // as 1/96th inch.
                            // We correct for that by assigning the image the width and height of the imageSource

                            // TODO: if the above theory is correct, then the next lines will work only if the Dpi of the bitmapSource is >=96
                            image.Width   = image.Source.Width;
                            image.Height  = image.Source.Height;
                            image.Stretch = System.Windows.Media.Stretch.Uniform;
                        }
                    }

                    renderer.WriteInline(inline);
                }
            }
            else
            {
                Hyperlink hyperlink = null;
                if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
                {
                    hyperlink = new Hyperlink
                    {
                        Command          = Commands.Hyperlink,
                        CommandParameter = url,
                        NavigateUri      = new Uri(url, UriKind.RelativeOrAbsolute),
                        ToolTip          = link.Title != string.Empty ? link.Title : null,
                        Tag = link
                    };
                }
                else // not a well formed Uri String - then it is probably a fragment reference
                {
                    hyperlink = new Hyperlink
                    {
                        Command          = Commands.FragmentLink,
                        CommandParameter = url,
                        NavigateUri      = new Uri(url, UriKind.RelativeOrAbsolute),
                        ToolTip          = link.Title != string.Empty ? link.Title : null,
                        Tag = link
                    };
                }

                renderer.Styles.ApplyHyperlinkStyle(hyperlink);
                renderer.Push(hyperlink);
                renderer.WriteChildren(link);
                renderer.Pop();
            }
        }
Ejemplo n.º 17
0
 protected override void Write([NotNull] WpfRenderer renderer, [NotNull] Markdig.Extensions.Figures.Figure obj)
 {
     renderer.WriteChildren(obj);
 }