OnPrepareLink() public method

public OnPrepareLink ( HtmlTag tag ) : void
tag HtmlTag
return void
Example #1
0
        internal void RenderLink(Markdown m, StringBuilder b, string link_text)
        {
            if (url.StartsWith("mailto:"))
            {
                b.Append("<a href=\"");
                Utils.HtmlRandomize(b, url);
                b.Append('\"');
                if (!String.IsNullOrEmpty(title))
                {
                    b.Append(" title=\"");
                    Utils.SmartHtmlEncodeAmpsAndAngles(b, title);
                    b.Append('\"');
                }
                b.Append('>');
                Utils.HtmlRandomize(b, link_text);
                b.Append("</a>");
            }
            else
            {
                HtmlTag tag = new HtmlTag("a");

                // encode url
                StringBuilder sb = m.GetStringBuilder();
                Utils.SmartHtmlEncodeAmpsAndAngles(sb, url);
                tag.attributes["href"] = sb.ToString();

                // encode title
                if (!String.IsNullOrEmpty(title))
                {
                    sb.Length = 0;
                    Utils.SmartHtmlEncodeAmpsAndAngles(sb, title);
                    tag.attributes["title"] = sb.ToString();
                }

                // Do user processing
                m.OnPrepareLink(tag);
                var href = tag.attributes["href"];
                if (href == "")
                {
                    tag = new HtmlTag("span");
                    tag.attributes["class"] = "brokenlink";
                }
                else if (href.StartsWith("http://") || href.StartsWith("https://"))
                {
                    tag.attributes["class"] = "foreignlink";
                }

                // Render the opening tag
                tag.RenderOpening(b);

                b.Append(link_text);                      // Link text already escaped by SpanFormatter
                b.AppendFormat("</{0}>", tag.name);
                if (href == "")
                {
                    m.AfterBrokenLink(b, sb.ToString(), link_text);
                }
            }
        }
Example #2
0
        internal void RenderLink(Markdown m, StringBuilder b, string link_text, List <string> specialAttributes)
        {
            if (this.Url.StartsWith("mailto:"))
            {
                b.Append("<a href=\"");
                Utils.HtmlRandomize(b, this.Url);
                b.Append('\"');
                if (!String.IsNullOrEmpty(this.Title))
                {
                    b.Append(" title=\"");
                    Utils.SmartHtmlEncodeAmpsAndAngles(b, this.Title);
                    b.Append('\"');
                }
                b.Append('>');
                Utils.HtmlRandomize(b, link_text);
                b.Append("</a>");
            }
            else
            {
                HtmlTag tag = new HtmlTag("a");

                // encode url
                StringBuilder sb = m.GetStringBuilder();
                Utils.SmartHtmlEncodeAmpsAndAngles(sb, this.Url);
                tag.attributes["href"] = sb.ToString();

                // encode title
                if (!String.IsNullOrEmpty(this.Title))
                {
                    sb.Length = 0;
                    Utils.SmartHtmlEncodeAmpsAndAngles(sb, this.Title);
                    tag.attributes["title"] = sb.ToString();
                }

                if (specialAttributes.Any())
                {
                    LinkDefinition.HandleSpecialAttributes(specialAttributes, sb, tag);
                }

                // Do user processing
                m.OnPrepareLink(tag);

                // Render the opening tag
                tag.RenderOpening(b);

                b.Append(link_text);                      // Link text already escaped by SpanFormatter
                b.Append("</a>");
            }
        }
Example #3
0
        internal void RenderLink(Markdown m, StringBuilder b, string linkText)
        {
            if (URL.StartsWith("mailto:"))
            {
                b.Append("<a href=\"");
                Utils.HtmlRandomize(b, URL);
                b.Append('\"');
                if (!string.IsNullOrEmpty(Title))
                {
                    b.Append(" title=\"");
                    Utils.SmartHtmlEncodeAmpsAndAngles(b, Title);
                    b.Append('\"');
                }
                b.Append('>');
                Utils.HtmlRandomize(b, linkText);
                b.Append("</a>");
            }
            else
            {
                var tag = new HtmlTag("a");

                // encode url
                var sb = m.GetStringBuilder();
                Utils.SmartHtmlEncodeAmpsAndAngles(sb, URL);
                tag.Attributes["href"] = sb.ToString();

                // encode title
                if (!string.IsNullOrEmpty(Title))
                {
                    sb.Length = 0;
                    Utils.SmartHtmlEncodeAmpsAndAngles(sb, Title);
                    tag.Attributes["title"] = sb.ToString();
                }

                // Do user processing
                m.OnPrepareLink(tag);

                // Render the opening tag
                tag.RenderOpening(b);

                b.Append(linkText);      // Link text already escaped by SpanFormatter
                b.Append("</a>");
            }
        }
Example #4
0
		internal void RenderLink(Markdown m, StringBuilder b, string link_text)
		{
			if (url.StartsWith("mailto:"))
			{
				b.Append("<a href=\"");
				Utils.HtmlRandomize(b, url);
				b.Append('\"');
				if (!String.IsNullOrEmpty(title))
				{
					b.Append(" title=\"");
					Utils.SmartHtmlEncodeAmpsAndAngles(b, title);
					b.Append('\"');
				}
				b.Append('>');
				Utils.HtmlRandomize(b, link_text);
				b.Append("</a>");
			}
			else
			{
				HtmlTag tag = new HtmlTag("a");

				// encode url
				StringBuilder sb = m.GetStringBuilder();
				Utils.SmartHtmlEncodeAmpsAndAngles(sb, url);
				tag.attributes["href"] = sb.ToString();

				// encode title
				if (!String.IsNullOrEmpty(title ))
				{
					sb.Length = 0;
					Utils.SmartHtmlEncodeAmpsAndAngles(sb, title);
					tag.attributes["title"] = sb.ToString();
				}

				// Do user processing
				m.OnPrepareLink(tag);

				// Render the opening tag
				tag.RenderOpening(b);

				b.Append(link_text);	  // Link text already escaped by SpanFormatter
				b.Append("</a>");
			}
		}
Example #5
0
        internal void Render(Markdown m, StringBuilder b)
        {
            switch (blockType)
            {
            case BlockType.Blank:
                return;

            case BlockType.p:
                m.SpanFormatter.FormatParagraph(b, buf, contentStart, contentLen);
                break;

            case BlockType.span:
                m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                b.Append("\n");
                break;

            case BlockType.h1:
            case BlockType.h2:
            case BlockType.h3:
            case BlockType.h4:
            case BlockType.h5:
            case BlockType.h6:
                if (m.ExtraMode && !m.SafeMode)
                {
                    b.Append("<" + blockType.ToString());
                    string id = ResolveHeaderID(m);
                    if (!String.IsNullOrEmpty(id))
                    {
                        b.Append(" id=\"");
                        b.Append(id);
                        b.Append("\">");
                    }
                    else
                    {
                        b.Append(">");
                    }
                }
                else
                {
                    b.Append("<" + blockType.ToString() + ">");
                }
                m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                b.Append("</" + blockType.ToString() + ">\n");
                break;

            case BlockType.hr:
                b.Append("<hr />\n");
                return;

            case BlockType.user_break:
                return;

            case BlockType.ol_li:
            case BlockType.ul_li:
                b.Append("<li>");
                m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                b.Append("</li>\n");
                break;

            case BlockType.dd:
                b.Append("<dd>");
                if (children != null)
                {
                    b.Append("\n");
                    RenderChildren(m, b);
                }
                else
                {
                    m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                }
                b.Append("</dd>\n");
                break;

            case BlockType.dt:
            {
                if (children == null)
                {
                    foreach (var l in Content.Split('\n'))
                    {
                        b.Append("<dt>");
                        m.SpanFormatter.Format(b, l.Trim());
                        b.Append("</dt>\n");
                    }
                }
                else
                {
                    b.Append("<dt>\n");
                    RenderChildren(m, b);
                    b.Append("</dt>\n");
                }
                break;
            }

            case BlockType.dl:
                b.Append("<dl>\n");
                RenderChildren(m, b);
                b.Append("</dl>\n");
                return;

            case BlockType.html:
                b.Append(buf, contentStart, contentLen);
                return;

            case BlockType.unsafe_html:
                m.HtmlEncode(b, buf, contentStart, contentLen);
                return;

            case BlockType.codeblock:
                if (m.FormatCodeBlock != null)
                {
                    var sb = new StringBuilder();
                    foreach (var line in children)
                    {
                        m.HtmlEncodeAndConvertTabsToSpaces(sb, line.buf, line.contentStart, line.contentLen);
                        sb.Append("\n");
                    }
                    b.Append(m.FormatCodeBlock(m, sb.ToString()));
                }
                else
                {
                    b.Append("<pre><code>");
                    foreach (var line in children)
                    {
                        m.HtmlEncodeAndConvertTabsToSpaces(b, line.buf, line.contentStart, line.contentLen);
                        b.Append("\n");
                    }
                    b.Append("</code></pre>\n\n");
                }
                return;

            case BlockType.quote:
                b.Append("<blockquote>\n");
                RenderChildren(m, b);
                b.Append("</blockquote>\n");
                return;

            case BlockType.li:
                b.Append("<li>\n");
                RenderChildren(m, b);
                b.Append("</li>\n");
                return;

            case BlockType.ol:
                b.Append("<ol>\n");
                RenderChildren(m, b);
                b.Append("</ol>\n");
                return;

            case BlockType.ul:
                b.Append("<ul>\n");
                RenderChildren(m, b);
                b.Append("</ul>\n");
                return;

            case BlockType.HtmlTag:
                var tag = (HtmlTag)data;

                // Prepare special tags
                var name = tag.name.ToLowerInvariant();
                if (name == "a")
                {
                    m.OnPrepareLink(tag);
                }
                else if (name == "img")
                {
                    m.OnPrepareImage(tag, m.RenderingTitledImage);
                }

                tag.RenderOpening(b);
                b.Append("\n");
                RenderChildren(m, b);
                tag.RenderClosing(b);
                b.Append("\n");
                return;

            case BlockType.Composite:
            case BlockType.footnote:
                RenderChildren(m, b);
                return;

            case BlockType.table_spec:
                ((TableSpec)data).Render(m, b);
                break;

            case BlockType.p_footnote:
                b.Append("<p>");
                if (contentLen > 0)
                {
                    m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                    b.Append("&nbsp;");
                }
                b.Append((string)data);
                b.Append("</p>\n");
                break;

            default:
                b.Append("<" + blockType.ToString() + ">");
                m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                b.Append("</" + blockType.ToString() + ">\n");
                break;
            }
        }
Example #6
0
        internal void Render(Markdown m, StringBuilder b)
        {
            switch (blockType)
            {
                case BlockType.Blank:
                    return;

                case BlockType.p:
                    m.SpanFormatter.FormatParagraph(b, buf, contentStart, contentLen);
                    break;

                case BlockType.span:
                    m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                    b.Append("\n");
                    break;

                case BlockType.h1:
                case BlockType.h2:
                case BlockType.h3:
                case BlockType.h4:
                case BlockType.h5:
                case BlockType.h6:
                    if (m.ExtraMode && !m.SafeMode)
                    {
                        b.Append("<" + blockType.ToString());
                        string id = ResolveHeaderID(m);
                        if (!String.IsNullOrEmpty(id))
                        {
                            b.Append(" id=\"");
                            b.Append(id);
                            b.Append("\">");
                        }
                        else
                        {
                            b.Append(">");
                        }
                    }
                    else
                    {
                        b.Append("<" + blockType.ToString() + ">");
                    }
                    m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                    b.Append("</" + blockType.ToString() + ">\n");
                    break;

                case BlockType.hr:
                    b.Append("<hr />\n");
                    return;

                case BlockType.user_break:
                    return;

                case BlockType.ol_li:
                case BlockType.ul_li:
                    b.Append("<li>");
                    m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                    b.Append("</li>\n");
                    break;

                case BlockType.dd:
                    b.Append("<dd>");
                    if (children != null)
                    {
                        b.Append("\n");
                        RenderChildren(m, b);
                    }
                    else
                        m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                    b.Append("</dd>\n");
                    break;

                case BlockType.dt:
                {
                    if (children == null)
                    {
                        foreach (var l in Content.Split('\n'))
                        {
                            b.Append("<dt>");
                            m.SpanFormatter.Format(b, l.Trim());
                            b.Append("</dt>\n");
                        }
                    }
                    else
                    {
                        b.Append("<dt>\n");
                        RenderChildren(m, b);
                        b.Append("</dt>\n");
                    }
                    break;
                }

                case BlockType.dl:
                    b.Append("<dl>\n");
                    RenderChildren(m, b);
                    b.Append("</dl>\n");
                    return;

                case BlockType.html:
                    b.Append(buf, contentStart, contentLen);
                    return;

                case BlockType.unsafe_html:
                    m.HtmlEncode(b, buf, contentStart, contentLen);
                    return;

                case BlockType.codeblock:
                    RenderCodeBlock(m, b);
                    return;

                case BlockType.quote:
                    b.Append("<blockquote>\n");
                    RenderChildren(m, b);
                    b.Append("</blockquote>\n");
                    return;

                case BlockType.li:
                    b.Append("<li>\n");
                    RenderChildren(m, b);
                    b.Append("</li>\n");
                    return;

                case BlockType.ol:
                    b.Append("<ol>\n");
                    RenderChildren(m, b);
                    b.Append("</ol>\n");
                    return;

                case BlockType.ul:
                    b.Append("<ul>\n");
                    RenderChildren(m, b);
                    b.Append("</ul>\n");
                    return;

                case BlockType.HtmlTag:
                    var tag = (HtmlTag)data;

                    // Prepare special tags
                    var name=tag.name.ToLowerInvariant();
                    if (name == "a")
                    {
                        m.OnPrepareLink(tag);
                    }
                    else if (name == "img")
                    {
                        m.OnPrepareImage(tag, m.RenderingTitledImage);
                    }

                    tag.RenderOpening(b);
                    b.Append("\n");
                    RenderChildren(m, b);
                    tag.RenderClosing(b);
                    b.Append("\n");
                    return;

                case BlockType.Composite:
                case BlockType.footnote:
                    RenderChildren(m, b);
                    return;

                case BlockType.table_spec:
                    ((TableSpec)data).Render(m, b);
                    break;

                case BlockType.p_footnote:
                    b.Append("<p>");
                    if (contentLen > 0)
                    {
                        m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                        b.Append("&nbsp;");
                    }
                    b.Append((string)data);
                    b.Append("</p>\n");
                    break;

                default:
                    b.Append("<" + blockType.ToString() + ">");
                    m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                    b.Append("</" + blockType.ToString() + ">\n");
                    break;
            }
        }
Example #7
0
        internal void Render(Markdown m, StringBuilder b)
        {
            switch (BlockType)
            {
            case BlockType.Blank:
                return;

            case BlockType.p:
                m.SpanFormatter.FormatParagraph(b, Buf, ContentStart, ContentLen);
                break;

            case BlockType.span:
                m.SpanFormatter.Format(b, Buf, ContentStart, ContentLen);
                b.Append("\n");
                break;

            case BlockType.h1:
            case BlockType.h2:
            case BlockType.h3:
            case BlockType.h4:
            case BlockType.h5:
            case BlockType.h6:
                string id = string.Empty;
                bool   insertPermalink = false;
                if (m.ExtraMode && !m.SafeMode)
                {
                    b.Append("<" + BlockType.ToString());
                    id = ResolveHeaderID(m);
                    if (!String.IsNullOrEmpty(id))
                    {
                        b.Append(" id=\"");
                        b.Append(id);
                        b.Append("\">");
                        insertPermalink = true;
                    }
                    else
                    {
                        b.Append(">");
                    }
                }
                else
                {
                    b.Append("<" + BlockType.ToString() + ">");
                }
                if (m.DocNetMode && BlockType == BlockType.h2 && !string.IsNullOrWhiteSpace(id))
                {
                    // collect h2 id + text in collector
                    var h2ContentSb = new StringBuilder();
                    m.SpanFormatter.Format(h2ContentSb, Buf, ContentStart, ContentLen);
                    var h2ContentAsString = h2ContentSb.ToString();
                    b.Append(h2ContentAsString);
                    m.CreatedH2IdCollector.Add(new Tuple <string, string>(id, h2ContentAsString));
                }
                else
                {
                    m.SpanFormatter.Format(b, Buf, ContentStart, ContentLen);
                }
                if (insertPermalink)
                {
                    // append header permalink link
                    b.Append("<a class=\"headerlink\" href=\"#");
                    b.Append(id);
                    b.Append("\" title=\"Permalink to this headline\"><i class=\"fa fa-link\" aria-hidden=\"true\"></i></a>");
                }
                b.Append("</" + BlockType.ToString() + ">\n");
                break;

            case BlockType.hr:
                b.Append("<hr />\n");
                return;

            case BlockType.user_break:
                return;

            case BlockType.ol_li:
            case BlockType.ul_li:
                b.Append("<li>");
                m.SpanFormatter.Format(b, Buf, ContentStart, ContentLen);
                b.Append("</li>\n");
                break;

            case BlockType.dd:
                b.Append("<dd>");
                if (Children != null)
                {
                    b.Append("\n");
                    RenderChildren(m, b);
                }
                else
                {
                    m.SpanFormatter.Format(b, Buf, ContentStart, ContentLen);
                }
                b.Append("</dd>\n");
                break;

            case BlockType.dt:
            {
                if (Children == null)
                {
                    foreach (var l in Content.Split('\n'))
                    {
                        b.Append("<dt>");
                        m.SpanFormatter.Format(b, l.Trim());
                        b.Append("</dt>\n");
                    }
                }
                else
                {
                    b.Append("<dt>\n");
                    RenderChildren(m, b);
                    b.Append("</dt>\n");
                }
                break;
            }

            case BlockType.dl:
                b.Append("<dl>\n");
                RenderChildren(m, b);
                b.Append("</dl>\n");
                return;

            case BlockType.html:
                b.Append(Buf, ContentStart, ContentLen);
                return;

            case BlockType.unsafe_html:
                m.HtmlEncode(b, Buf, ContentStart, ContentLen);
                return;

            case BlockType.codeblock:
                if (m.FormatCodeBlockFunc == null)
                {
                    var    dataArgument = this.Data as string ?? string.Empty;
                    string tagSuffix    = "</code></pre>\n\n";

                    if (m.GitHubCodeBlocks && !string.IsNullOrWhiteSpace(dataArgument))
                    {
                        if (dataArgument == "nohighlight")
                        {
                            b.Append("<pre class=\"nocode\">");
                            tagSuffix = "</pre>";
                        }
                        else
                        {
                            b.AppendFormat("<pre><code class=\"{0}\">", dataArgument);
                        }
                    }
                    else
                    {
                        b.Append("<pre><code>");
                    }
                    foreach (var line in Children)
                    {
                        m.HtmlEncodeAndConvertTabsToSpaces(b, line.Buf, line.ContentStart, line.ContentLen);
                        b.Append("\n");
                    }
                    b.Append(tagSuffix);
                }
                else
                {
                    var sb = new StringBuilder();
                    foreach (var line in Children)
                    {
                        m.HtmlEncodeAndConvertTabsToSpaces(sb, line.Buf, line.ContentStart, line.ContentLen);
                        sb.Append("\n");
                    }
                    b.Append(m.FormatCodeBlockFunc(m, sb.ToString()));
                }
                return;

            case BlockType.quote:
                b.Append("<blockquote>\n");
                RenderChildren(m, b);
                b.Append("</blockquote>\n");
                return;

            case BlockType.li:
                b.Append("<li>\n");
                RenderChildren(m, b);
                b.Append("</li>\n");
                return;

            case BlockType.ol:
                b.Append("<ol>\n");
                RenderChildren(m, b);
                b.Append("</ol>\n");
                return;

            case BlockType.ul:
                b.Append("<ul>\n");
                RenderChildren(m, b);
                b.Append("</ul>\n");
                return;

            case BlockType.HtmlTag:
                var tag = (HtmlTag)Data;

                // Prepare special tags
                var name = tag.name.ToLowerInvariant();
                if (name == "a")
                {
                    m.OnPrepareLink(tag);
                }
                else if (name == "img")
                {
                    m.OnPrepareImage(tag, m.RenderingTitledImage);
                }

                tag.RenderOpening(b);
                b.Append("\n");
                RenderChildren(m, b);
                tag.RenderClosing(b);
                b.Append("\n");
                return;

            case BlockType.Composite:
            case BlockType.footnote:
                RenderChildren(m, b);
                return;

            case BlockType.table_spec:
                ((TableSpec)Data).Render(m, b);
                break;

            case BlockType.p_footnote:
                b.Append("<p>");
                if (ContentLen > 0)
                {
                    m.SpanFormatter.Format(b, Buf, ContentStart, ContentLen);
                    b.Append("&nbsp;");
                }
                b.Append((string)Data);
                b.Append("</p>\n");
                break;

// DocNet Extensions
            case BlockType.font_awesome:
                if (m.DocNetMode)
                {
                    b.Append("<i class=\"fa fa-");
                    b.Append(this.Data as string ?? string.Empty);
                    b.Append("\"></i>");
                }
                break;

            case BlockType.alert:
                if (m.DocNetMode)
                {
                    RenderAlert(m, b);
                }
                break;

            case BlockType.tabs:
                if (m.DocNetMode)
                {
                    RenderTabs(m, b);
                }
                break;

// End DocNet Extensions
            default:
                b.Append("<" + BlockType.ToString() + ">");
                m.SpanFormatter.Format(b, Buf, ContentStart, ContentLen);
                b.Append("</" + BlockType.ToString() + ">\n");
                break;
            }
        }
Example #8
0
        internal void RenderLink(Markdown m, StringBuilder b, string link_text, List <string> specialAttributes)
        {
            if (this.Url.StartsWith("mailto:"))
            {
                b.Append("<a href=\"");
                Utils.HtmlRandomize(b, this.Url);
                b.Append('\"');
                if (!String.IsNullOrEmpty(this.Title))
                {
                    b.Append(" title=\"");
                    Utils.SmartHtmlEncodeAmpsAndAngles(b, this.Title);
                    b.Append('\"');
                }
                b.Append('>');
                Utils.HtmlRandomize(b, link_text);
                b.Append("</a>");
            }
            else
            {
                HtmlTag tag = new HtmlTag("a");

                var url = this.Url;

                if (m.DocNetMode && m.ConvertLocalLinks)
                {
                    // A few requirements before we can convert local links:
                    //   1. Link contains .md
                    //   2. Link is relative
                    //   3. Link is included in the index
                    var index = url.LastIndexOf(".md", StringComparison.OrdinalIgnoreCase);
                    if (index >= 0)
                    {
                        var linkProcessor = m.LocalLinkProcessor;
                        if (linkProcessor != null)
                        {
                            url = linkProcessor(url);
                        }
                        else
                        {
                            Uri uri;
                            if (Uri.TryCreate(url, UriKind.Relative, out uri))
                            {
                                url = String.Concat(url.Substring(0, index), ".htm", url.Substring(index + ".md".Length));
                            }
                        }
                    }
                }

                // encode url
                StringBuilder sb = m.GetStringBuilder();
                Utils.SmartHtmlEncodeAmpsAndAngles(sb, url);
                tag.attributes["href"] = sb.ToString();

                // encode title
                if (!String.IsNullOrEmpty(this.Title))
                {
                    sb.Length = 0;
                    Utils.SmartHtmlEncodeAmpsAndAngles(sb, this.Title);
                    tag.attributes["title"] = sb.ToString();
                }

                if (specialAttributes.Any())
                {
                    LinkDefinition.HandleSpecialAttributes(specialAttributes, sb, tag);
                }

                // Do user processing
                m.OnPrepareLink(tag);

                // Render the opening tag
                tag.RenderOpening(b);

                b.Append(link_text);                      // Link text already escaped by SpanFormatter
                b.Append("</a>");
            }
        }
Example #9
0
        internal void Render(Markdown m, StringBuilder b)
        {
            switch (blockType)
            {
                case BlockType.Blank:
                    return;

                case BlockType.p:
                    m.SpanFormatter.FormatParagraph(b, buf, contentStart, contentLen);
                    break;

                case BlockType.span:
                    m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                    b.Append("\n");
                    break;

                case BlockType.h1:
                case BlockType.h2:
                case BlockType.h3:
                case BlockType.h4:
                case BlockType.h5:
                case BlockType.h6:
                    if (m.ExtraMode && !m.SafeMode)
                    {
                        b.Append("<" + blockType.ToString());
                        string id = ResolveHeaderID(m);
                        if (!String.IsNullOrEmpty(id))
                        {
                            b.Append(" id=\"");
                            b.Append(id);
                            b.Append("\">");
                        }
                        else
                        {
                            b.Append(">");
                        }
                    }
                    else
                    {
                        b.Append("<" + blockType.ToString() + ">");
                    }
                    m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                    b.Append("</" + blockType.ToString() + ">\n");
                    break;

                case BlockType.hr:
                    b.Append("<hr />\n");
                    return;

                case BlockType.user_break:
                    return;

                case BlockType.ol_li:
                case BlockType.ul_li:
                    b.Append("<li>");
                    m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                    b.Append("</li>\n");
                    break;

                case BlockType.dd:
                    b.Append("<dd>");
                    if (children != null)
                    {
                        b.Append("\n");
                        RenderChildren(m, b);
                    }
                    else
                        m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                    b.Append("</dd>\n");
                    break;

                case BlockType.dt:
                {
                    if (children == null)
                    {
                        foreach (var l in Content.Split('\n'))
                        {
                            b.Append("<dt>");
                            m.SpanFormatter.Format(b, l.Trim());
                            b.Append("</dt>\n");
                        }
                    }
                    else
                    {
                        b.Append("<dt>\n");
                        RenderChildren(m, b);
                        b.Append("</dt>\n");
                    }
                    break;
                }

                case BlockType.dl:
                    b.Append("<dl>\n");
                    RenderChildren(m, b);
                    b.Append("</dl>\n");
                    return;

                case BlockType.html:
                    b.Append(buf, contentStart, contentLen);
                    return;

                case BlockType.unsafe_html:
                    m.HtmlEncode(b, buf, contentStart, contentLen);
                    return;

                case BlockType.codeblock:
                    if(m.FormatCodeBlock == null)
                    {
                        var dataArgument = this.data as string ?? string.Empty;
                        string tagSuffix = "</code></pre>\n\n";

                        if(m.GitHubCodeBlocks && !string.IsNullOrWhiteSpace(dataArgument))
                        {
                            if(dataArgument == "nohighlight")
                            {
                                b.Append("<pre class=\"nocode\">");
                                tagSuffix = "</pre>";
                            }
                            else
                            {
                                b.AppendFormat("<pre><code class=\"{0}\">", dataArgument);
                            }
                        }
                        else
                        {
                            b.Append("<pre><code>");
                        }
                        foreach(var line in children)
                        {
                            m.HtmlEncodeAndConvertTabsToSpaces(b, line.buf, line.contentStart, line.contentLen);
                            b.Append("\n");
                        }
                        b.Append(tagSuffix);
                    }
                    else
                    {
                        var sb = new StringBuilder();
                        foreach(var line in children)
                        {
                            m.HtmlEncodeAndConvertTabsToSpaces(sb, line.buf, line.contentStart, line.contentLen);
                            sb.Append("\n");
                        }
                        b.Append(m.FormatCodeBlock(m, sb.ToString()));
                    }
                    return;

                case BlockType.quote:
                    b.Append("<blockquote>\n");
                    RenderChildren(m, b);
                    b.Append("</blockquote>\n");
                    return;

                case BlockType.li:
                    b.Append("<li>\n");
                    RenderChildren(m, b);
                    b.Append("</li>\n");
                    return;

                case BlockType.ol:
                    b.Append("<ol>\n");
                    RenderChildren(m, b);
                    b.Append("</ol>\n");
                    return;

                case BlockType.ul:
                    b.Append("<ul>\n");
                    RenderChildren(m, b);
                    b.Append("</ul>\n");
                    return;

                case BlockType.HtmlTag:
                    var tag = (HtmlTag)data;

                    // Prepare special tags
                    var name=tag.name.ToLowerInvariant();
                    if (name == "a")
                    {
                        m.OnPrepareLink(tag);
                    }
                    else if (name == "img")
                    {
                        m.OnPrepareImage(tag, m.RenderingTitledImage);
                    }

                    tag.RenderOpening(b);
                    b.Append("\n");
                    RenderChildren(m, b);
                    tag.RenderClosing(b);
                    b.Append("\n");
                    return;

                case BlockType.Composite:
                case BlockType.footnote:
                    RenderChildren(m, b);
                    return;

                case BlockType.table_spec:
                    ((TableSpec)data).Render(m, b);
                    break;

                case BlockType.p_footnote:
                    b.Append("<p>");
                    if (contentLen > 0)
                    {
                        m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                        b.Append("&nbsp;");
                    }
                    b.Append((string)data);
                    b.Append("</p>\n");
                    break;

                default:
                    b.Append("<" + blockType.ToString() + ">");
                    m.SpanFormatter.Format(b, buf, contentStart, contentLen);
                    b.Append("</" + blockType.ToString() + ">\n");
                    break;
            }
        }
Example #10
0
        internal void Render(Markdown m, StringBuilder b)
        {
            switch (BlockType)
            {
                case BlockType.Blank:
                    return;

                case BlockType.p:
                    m.SpanFormatter.FormatParagraph(b, Buf, ContentStart, ContentLen);
                    break;

                case BlockType.span:
                    m.SpanFormatter.Format(b, Buf, ContentStart, ContentLen);
                    b.Append("\n");
                    break;

                case BlockType.h1:
                case BlockType.h2:
                case BlockType.h3:
                case BlockType.h4:
                case BlockType.h5:
                case BlockType.h6:
                    string id = string.Empty;
                    bool insertPermalink = false;
                    if (m.ExtraMode && !m.SafeMode)
                    {
                        b.Append("<" + BlockType.ToString());
                        id = ResolveHeaderID(m);
                        if (!String.IsNullOrEmpty(id))
                        {
                            b.Append(" id=\"");
                            b.Append(id);
                            b.Append("\">");
                            insertPermalink = true;
                        }
                        else
                        {
                            b.Append(">");
                        }
                    }
                    else
                    {
                        b.Append("<" + BlockType.ToString() + ">");
                    }
                    if(m.DocNetMode && BlockType == BlockType.h2 && !string.IsNullOrWhiteSpace(id))
                    {
                        // collect h2 id + text in collector
                        var h2ContentSb = new StringBuilder();
                        m.SpanFormatter.Format(h2ContentSb, Buf, ContentStart, ContentLen);
                        var h2ContentAsString = h2ContentSb.ToString();
                        b.Append(h2ContentAsString);
                        m.CreatedH2IdCollector.Add(new Tuple<string, string>(id, h2ContentAsString));
                    }
                    else
                    {
                        m.SpanFormatter.Format(b, Buf, ContentStart, ContentLen);
                    }
                    if(insertPermalink)
                    {
                        // append header permalink link
                        b.Append("<a class=\"headerlink\" href=\"#");
                        b.Append(id);
                        b.Append("\" title=\"Permalink to this headline\"><i class=\"fa fa-link\" aria-hidden=\"true\"></i></a>");
                    }
                    b.Append("</" + BlockType.ToString() + ">\n");
                    break;

                case BlockType.hr:
                    b.Append("<hr />\n");
                    return;

                case BlockType.user_break:
                    return;

                case BlockType.ol_li:
                case BlockType.ul_li:
                    b.Append("<li>");
                    m.SpanFormatter.Format(b, Buf, ContentStart, ContentLen);
                    b.Append("</li>\n");
                    break;

                case BlockType.dd:
                    b.Append("<dd>");
                    if (Children != null)
                    {
                        b.Append("\n");
                        RenderChildren(m, b);
                    }
                    else
                        m.SpanFormatter.Format(b, Buf, ContentStart, ContentLen);
                    b.Append("</dd>\n");
                    break;

                case BlockType.dt:
                {
                    if (Children == null)
                    {
                        foreach (var l in Content.Split('\n'))
                        {
                            b.Append("<dt>");
                            m.SpanFormatter.Format(b, l.Trim());
                            b.Append("</dt>\n");
                        }
                    }
                    else
                    {
                        b.Append("<dt>\n");
                        RenderChildren(m, b);
                        b.Append("</dt>\n");
                    }
                    break;
                }

                case BlockType.dl:
                    b.Append("<dl>\n");
                    RenderChildren(m, b);
                    b.Append("</dl>\n");
                    return;

                case BlockType.html:
                    b.Append(Buf, ContentStart, ContentLen);
                    return;

                case BlockType.unsafe_html:
                    m.HtmlEncode(b, Buf, ContentStart, ContentLen);
                    return;

                case BlockType.codeblock:
                    if(m.FormatCodeBlockFunc == null)
                    {
                        var dataArgument = this.Data as string ?? string.Empty;
                        string tagSuffix = "</code></pre>\n\n";

                        if(m.GitHubCodeBlocks && !string.IsNullOrWhiteSpace(dataArgument))
                        {
                            if(dataArgument == "nohighlight")
                            {
                                b.Append("<pre class=\"nocode\">");
                                tagSuffix = "</pre>";
                            }
                            else
                            {
                                b.AppendFormat("<pre><code class=\"{0}\">", dataArgument);
                            }
                        }
                        else
                        {
                            b.Append("<pre><code>");
                        }
                        foreach(var line in Children)
                        {
                            m.HtmlEncodeAndConvertTabsToSpaces(b, line.Buf, line.ContentStart, line.ContentLen);
                            b.Append("\n");
                        }
                        b.Append(tagSuffix);
                    }
                    else
                    {
                        var sb = new StringBuilder();
                        foreach(var line in Children)
                        {
                            m.HtmlEncodeAndConvertTabsToSpaces(sb, line.Buf, line.ContentStart, line.ContentLen);
                            sb.Append("\n");
                        }
                        b.Append(m.FormatCodeBlockFunc(m, sb.ToString()));
                    }
                    return;

                case BlockType.quote:
                    b.Append("<blockquote>\n");
                    RenderChildren(m, b);
                    b.Append("</blockquote>\n");
                    return;

                case BlockType.li:
                    b.Append("<li>\n");
                    RenderChildren(m, b);
                    b.Append("</li>\n");
                    return;

                case BlockType.ol:
                    b.Append("<ol>\n");
                    RenderChildren(m, b);
                    b.Append("</ol>\n");
                    return;

                case BlockType.ul:
                    b.Append("<ul>\n");
                    RenderChildren(m, b);
                    b.Append("</ul>\n");
                    return;

                case BlockType.HtmlTag:
                    var tag = (HtmlTag)Data;

                    // Prepare special tags
                    var name=tag.name.ToLowerInvariant();
                    if (name == "a")
                    {
                        m.OnPrepareLink(tag);
                    }
                    else if (name == "img")
                    {
                        m.OnPrepareImage(tag, m.RenderingTitledImage);
                    }

                    tag.RenderOpening(b);
                    b.Append("\n");
                    RenderChildren(m, b);
                    tag.RenderClosing(b);
                    b.Append("\n");
                    return;

                case BlockType.Composite:
                case BlockType.footnote:
                    RenderChildren(m, b);
                    return;

                case BlockType.table_spec:
                    ((TableSpec)Data).Render(m, b);
                    break;

                case BlockType.p_footnote:
                    b.Append("<p>");
                    if (ContentLen > 0)
                    {
                        m.SpanFormatter.Format(b, Buf, ContentStart, ContentLen);
                        b.Append("&nbsp;");
                    }
                    b.Append((string)Data);
                    b.Append("</p>\n");
                    break;
            // DocNet Extensions
                case BlockType.font_awesome:
                    if(m.DocNetMode)
                    {
                        b.Append("<i class=\"fa fa-");
                        b.Append(this.Data as string ?? string.Empty);
                        b.Append("\"></i>");
                    }
                    break;
                case BlockType.alert:
                    if(m.DocNetMode)
                    {
                        RenderAlert(m, b);
                    }
                    break;
                case BlockType.tabs:
                    if(m.DocNetMode)
                    {
                        RenderTabs(m, b);
                    }
                    break;
            // End DocNet Extensions
                default:
                    b.Append("<" + BlockType.ToString() + ">");
                    m.SpanFormatter.Format(b, Buf, ContentStart, ContentLen);
                    b.Append("</" + BlockType.ToString() + ">\n");
                    break;
            }
        }