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(" "); } 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; } }
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(" "); } 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; } }