Exemple #1
0
        static string ProcessInlineElem(Inlines.Inline inline)
        {
            string result = string.Empty;

            while (inline != null)
            {
                var type = inline.GetType();
                if (KnownInlines.ContainsKey(type))
                {
                    result += KnownInlines[type].Invoke(inline);
                }
                else
                {
                    Console.WriteLine($"% WARNING: inline element not handled ({type})");
                }
                inline = inline.NextSibling;
            }
            return(result);
        }
Exemple #2
0
 protected virtual void OnChildInsert(Inline child)
 {
 }
Exemple #3
0
        /// <summary>
        /// Replaces this inline by the specified inline.
        /// </summary>
        /// <param name="inline">The inline.</param>
        /// <param name="copyChildren">if set to <c>true</c> the children of this instance are copied to the specified inline.</param>
        /// <returns>The last children</returns>
        /// <exception cref="System.ArgumentNullException">If inline is null</exception>
        public Inline ReplaceBy(Inline inline, bool copyChildren = true)
        {
            if (inline == null)
            {
                ThrowHelper.ArgumentNullException(nameof(inline));
            }

            // Save sibling
            var parent          = Parent;
            var previousSibling = PreviousSibling;
            var nextSibling     = NextSibling;

            Remove();

            if (previousSibling != null)
            {
                previousSibling.InsertAfter(inline);
            }
            else if (nextSibling != null)
            {
                nextSibling.InsertBefore(inline);
            }
            else if (parent != null)
            {
                parent.AppendChild(inline);
            }

            var container = this as ContainerInline;

            if (copyChildren && container != null)
            {
                var newContainer = inline as ContainerInline;
                // Don't append to a closed container
                if (newContainer != null && newContainer.IsClosed)
                {
                    newContainer = null;
                }
                // TODO: This part is not efficient as it is using child.Remove()
                // We need a method to quickly move all children without having to mess Next/Prev sibling
                var child     = container.FirstChild;
                var lastChild = inline;
                while (child != null)
                {
                    var nextChild = child.NextSibling;
                    child.Remove();
                    if (newContainer != null)
                    {
                        newContainer.AppendChild(child);
                    }
                    else
                    {
                        lastChild.InsertAfter(child);
                    }
                    lastChild = child;
                    child     = nextChild;
                }

                return(lastChild);
            }

            return(inline);
        }
Exemple #4
0
 protected virtual void OnChildRemove(Inline child)
 {
 }
Exemple #5
0
        public static string ToMarkdownString(this Markdig.Syntax.Inlines.Inline inline)
        {
            //Debug.WriteLine(inline.GetType());
            string add = string.Empty;

            if (inline is Markdig.Syntax.Inlines.LineBreakInline)
            {
                add = "\n";
            }
            else if (inline is Markdig.Syntax.Inlines.LiteralInline)
            {
                var literalInline = inline as Markdig.Syntax.Inlines.LiteralInline;
                add = literalInline.Content.ToString();
            }
            else if (inline is Markdig.Syntax.Inlines.EmphasisInline)
            {
                var emphasisInline = inline as Markdig.Syntax.Inlines.EmphasisInline;
                var delimiterChar  = emphasisInline.DelimiterChar.ToString();
                if (emphasisInline.DelimiterCount == 2) //.IsDouble)
                {
                    delimiterChar += delimiterChar;
                }
                add = delimiterChar + emphasisInline.ToMarkdownString() + delimiterChar;
            }
            else if (inline is Markdig.Syntax.Inlines.LinkInline)
            {
                var linkInline = inline as Markdig.Syntax.Inlines.LinkInline;
                add = string.Empty;
                if (linkInline.IsImage)
                {
                    add = "!";
                }
                var label = linkInline.ToMarkdownString();
                var url   = linkInline.Url;
                var title = linkInline.Title;
                if (!string.IsNullOrEmpty(title))
                {
                    add += $"[{label}]({url} \"{title}\")";
                }
                else
                {
                    add += $"[{label}]({url})";
                }
            }
            else if (inline is Markdig.Syntax.Inlines.ContainerInline)
            {
                var containerInline = inline as Markdig.Syntax.Inlines.ContainerInline;
                add = containerInline.ToMarkdownString();
            }
            else if (inline is Markdig.Syntax.Inlines.HtmlInline)
            {
                var htmlInline = inline as Markdig.Syntax.Inlines.HtmlInline;
                add = htmlInline.Tag;
            }
            else
            {
                add = inline.ToString();
            }
            //Debug.WriteLine(add);
            return(add);
        }