OnPrepareImage() public method

public OnPrepareImage ( HtmlTag tag, bool TitledImage ) : void
tag HtmlTag
TitledImage bool
return void
Example #1
0
        internal void RenderImg(Markdown m, StringBuilder b, string alt_text, List <string> specialAttributes)
        {
            HtmlTag tag = new HtmlTag("img");

            // encode url
            StringBuilder sb = m.GetStringBuilder();

            Utils.SmartHtmlEncodeAmpsAndAngles(sb, Url);
            tag.attributes["src"] = sb.ToString();

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

            // encode title
            if (!String.IsNullOrEmpty(Title))
            {
                sb.Length = 0;
                Utils.SmartHtmlEncodeAmpsAndAngles(sb, Title);
                tag.attributes["title"] = sb.ToString();
            }
            if (specialAttributes.Any())
            {
                LinkDefinition.HandleSpecialAttributes(specialAttributes, sb, tag);
            }
            tag.closed = true;

            m.OnPrepareImage(tag, m.RenderingTitledImage);

            tag.RenderOpening(b);
        }
Example #2
0
        internal void RenderImg(Markdown m, StringBuilder b, string alt_text)
        {
            HtmlTag tag = new HtmlTag("img");

            // encode url
            StringBuilder sb = m.GetStringBuilder();

            Utils.SmartHtmlEncodeAmpsAndAngles(sb, url);
            tag.attributes["src"] = sb.ToString();

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

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

            tag.closed = true;

            m.OnPrepareImage(tag, m.RenderingTitledImage);

            tag.RenderOpening(b);
        }
Example #3
0
		internal void RenderImg(Markdown m, StringBuilder b, string alt_text)
		{
			HtmlTag tag = new HtmlTag("img");

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

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

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

			tag.closed = true;

			m.OnPrepareImage(tag, m.RenderingTitledImage);

			tag.RenderOpening(b);
		}
Example #4
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 #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:
                    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 #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:
                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 #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:
                    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 #8
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 #9
0
        internal void RenderImg(Markdown m, StringBuilder b, string alt_text, List<string> specialAttributes)
        {
            HtmlTag tag = new HtmlTag("img");

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

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

            // encode title
            if (!String.IsNullOrEmpty(Title))
            {
                sb.Length = 0;
                Utils.SmartHtmlEncodeAmpsAndAngles(sb, Title);
                tag.attributes["title"] = sb.ToString();
            }
            if(specialAttributes.Any())
            {
                LinkDefinition.HandleSpecialAttributes(specialAttributes, sb, tag);
            }
            tag.closed = true;

            m.OnPrepareImage(tag, m.RenderingTitledImage);

            tag.RenderOpening(b);
        }