Exemplo n.º 1
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(">");
                    }
                    m.OnHeadingGenerated(blockType.ToString(), id, buf, contentStart, contentLen);
                }
                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
                {
                    if (this.data != null)
                    {
                        b.Append("<pre><code class=\"language-" + data + "\">");
                    }
                    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;
            }
        }