// Render a list of tokens to a destinatino string builder. private void Render(StringBuilder sb, string str) { var reg = new Regex("<([^ >]+)"); var tags = new Stack <String>(); foreach (Token t in m_Tokens) { switch (t.type) { case TokenType.Text: // Append encoded text m_Markdown.HtmlEncode(sb, str, t.startOffset, t.length); break; case TokenType.HtmlTag: // Append html as is var tag = str.Substring(t.startOffset, t.length); var mtag = reg.Match(tag).Groups[1].Value; var mtag_l = mtag.ToLower(); if (mtag.StartsWith("/")) { while (tags.Count() > 0) { var ptag = tags.Pop(); if (ptag.ToLower() == mtag_l) { Utils.SmartHtmlEncodeAmps(sb, str, t.startOffset, t.length); break; } else { sb.Append("<" + ptag + ">"); } } } else { tags.Push("/" + mtag); Utils.SmartHtmlEncodeAmps(sb, str, t.startOffset, t.length); } break; case TokenType.Html: case TokenType.opening_mark: case TokenType.closing_mark: case TokenType.internal_mark: // Append html as is sb.Append(str, t.startOffset, t.length); break; case TokenType.br: sb.Append("<br />\n"); break; case TokenType.open_em: sb.Append("<em>"); break; case TokenType.close_em: sb.Append("</em>"); break; case TokenType.open_strong: sb.Append("<strong>"); break; case TokenType.close_strong: sb.Append("</strong>"); break; case TokenType.code_span: sb.Append("<code>"); m_Markdown.HtmlEncode(sb, str, t.startOffset, t.length); sb.Append("</code>"); break; case TokenType.link: { LinkInfo li = (LinkInfo)t.data; var sf = new SpanFormatter(m_Markdown); sf.DisableLinks = true; li.def.RenderLink(m_Markdown, sb, sf.Format(li.link_text)); break; } case TokenType.img: { LinkInfo li = (LinkInfo)t.data; li.def.RenderImg(m_Markdown, sb, li.link_text); break; } case TokenType.footnote: { FootnoteReference r = (FootnoteReference)t.data; sb.Append("<sup id=\"fnref:"); sb.Append(r.id); sb.Append("\"><a href=\"#fn:"); sb.Append(r.id); sb.Append("\" rel=\"footnote\">"); sb.Append(r.index + 1); sb.Append("</a></sup>"); break; } case TokenType.abbreviation: { Abbreviation a = (Abbreviation)t.data; sb.Append("<abbr"); if (!String.IsNullOrEmpty(a.Title)) { sb.Append(" title=\""); m_Markdown.HtmlEncode(sb, a.Title, 0, a.Title.Length); sb.Append("\""); } sb.Append(">"); m_Markdown.HtmlEncode(sb, a.Abbr, 0, a.Abbr.Length); sb.Append("</abbr>"); break; } } FreeToken(t); } while (tags.Count() > 0) { sb.Append("<" + tags.Pop() + ">"); } }
// Render a list of tokens to a destinatino string builder. private void Render(StringBuilder sb, string str) { foreach (Token t in m_Tokens) { switch (t.type) { case TokenType.Text: // Append encoded text m_Markdown.HtmlEncode(sb, str, t.startOffset, t.length); break; case TokenType.HtmlTag: // Append html as is Utils.SmartHtmlEncodeAmps(sb, str, t.startOffset, t.length); break; case TokenType.Html: case TokenType.opening_mark: case TokenType.closing_mark: case TokenType.internal_mark: // Append html as is sb.Append(str, t.startOffset, t.length); break; case TokenType.br: sb.Append("<br />\n"); break; case TokenType.open_em: sb.Append("<em>"); break; case TokenType.close_em: sb.Append("</em>"); break; case TokenType.open_strong: sb.Append("<strong>"); break; case TokenType.close_strong: sb.Append("</strong>"); break; case TokenType.code_span: sb.Append("<code>"); m_Markdown.HtmlEncode(sb, str, t.startOffset, t.length); sb.Append("</code>"); break; case TokenType.link: { LinkInfo li = (LinkInfo)t.data; li.RenderLink(m_Markdown, sb); break; } case TokenType.img: { LinkInfo li = (LinkInfo)t.data; li.RenderImage(m_Markdown, sb); break; } case TokenType.footnote: { FootnoteReference r = (FootnoteReference)t.data; sb.Append("<sup id=\"fnref:"); sb.Append(r.id); sb.Append("\"><a href=\"#fn:"); sb.Append(r.id); sb.Append("\" rel=\"footnote\">"); sb.Append(r.index + 1); sb.Append("</a></sup>"); break; } case TokenType.abbreviation: { Abbreviation a = (Abbreviation)t.data; sb.Append("<abbr"); if (!String.IsNullOrEmpty(a.Title)) { sb.Append(" title=\""); m_Markdown.HtmlEncode(sb, a.Title, 0, a.Title.Length); sb.Append("\""); } sb.Append(">"); m_Markdown.HtmlEncode(sb, a.Abbr, 0, a.Abbr.Length); sb.Append("</abbr>"); break; } // DocNet Extensions case TokenType.font_awesome: sb.Append("<i class=\"fas fa-"); m_Markdown.HtmlEncode(sb, str, t.startOffset, t.length); sb.Append("\"></i>"); break; // HnD Extensions case TokenType.strikethrough_span: sb.Append("<s>"); m_Markdown.HtmlEncode(sb, str, t.startOffset, t.length); sb.Append("</s>"); break; case TokenType.emoji: var emojiFilename = string.Empty; var emojiName = t.data as string ?? string.Empty; if (m_Markdown.EmojiFilePerName != null) { m_Markdown.EmojiFilePerName.TryGetValue(emojiName, out emojiFilename); } if (string.IsNullOrWhiteSpace(emojiFilename)) { sb.Append(":"); m_Markdown.HtmlEncode(sb, emojiName, 0, emojiName.Length); sb.Append(":"); } else { sb.Append("<img src=\""); sb.Append(emojiFilename); sb.Append("\" class=\"emoji\" alt=\""); sb.Append(emojiName); sb.Append("\"/>"); } break; } FreeToken(t); } }
// Render a list of tokens to a destinatino string builder. private void Render(StringBuilder sb, string str) { foreach (Token t in m_Tokens) { switch (t.type) { case TokenType.Text: // Append encoded text m_Markdown.HtmlEncode(sb, str, t.startOffset, t.length); break; case TokenType.HtmlTag: // Append html as is Utils.SmartHtmlEncodeAmps(sb, str, t.startOffset, t.length); break; case TokenType.Html: case TokenType.opening_mark: case TokenType.closing_mark: case TokenType.internal_mark: // Append html as is sb.Append(str, t.startOffset, t.length); break; case TokenType.br: sb.Append("<br />\n"); break; case TokenType.open_em: sb.Append("<em>"); break; case TokenType.close_em: sb.Append("</em>"); break; case TokenType.open_strong: sb.Append("<strong>"); break; case TokenType.close_strong: sb.Append("</strong>"); break; case TokenType.code_span: sb.Append("<code>"); m_Markdown.HtmlEncode(sb, str, t.startOffset, t.length); sb.Append("</code>"); break; case TokenType.link: { LinkInfo li = (LinkInfo)t.data; var sf = new SpanFormatter(m_Markdown); sf.DisableLinks = true; li.def.RenderLink(m_Markdown, sb, sf.Format(li.link_text)); break; } case TokenType.img: { LinkInfo li = (LinkInfo)t.data; li.def.RenderImg(m_Markdown, sb, li.link_text); break; } case TokenType.footnote: { FootnoteReference r = (FootnoteReference)t.data; sb.Append("<sup id=\"fnref:"); sb.Append(r.id); sb.Append("\"><a href=\"#fn:"); sb.Append(r.id); sb.Append("\" rel=\"footnote\">"); sb.Append(r.index + 1); sb.Append("</a></sup>"); break; } case TokenType.abbreviation: { Abbreviation a = (Abbreviation)t.data; sb.Append("<abbr"); if (!String.IsNullOrEmpty(a.Title)) { sb.Append(" title=\""); m_Markdown.HtmlEncode(sb, a.Title, 0, a.Title.Length); sb.Append("\""); } sb.Append(">"); m_Markdown.HtmlEncode(sb, a.Abbr, 0, a.Abbr.Length); sb.Append("</abbr>"); break; } } FreeToken(t); } }
// Render a list of tokens to a destination string builder. private void Render(StringBuilder sb, string str) { foreach (var t in _mTokens) { switch (t.Type) { case TokenType.Text: // Append encoded text _mMarkdown.HtmlEncode(sb, str, t.StartOffset, t.Length); break; case TokenType.HtmlTag: // Append html as is Utils.SmartHtmlEncodeAmps(sb, str, t.StartOffset, t.Length); break; case TokenType.Html: case TokenType.opening_mark: case TokenType.closing_mark: case TokenType.internal_mark: // Append html as is sb.Append(str, t.StartOffset, t.Length); break; case TokenType.br: sb.Append("<br />\n"); break; case TokenType.open_em: sb.Append("<em>"); break; case TokenType.close_em: sb.Append("</em>"); break; case TokenType.open_strong: sb.Append("<strong>"); break; case TokenType.close_strong: sb.Append("</strong>"); break; case TokenType.code_span: sb.Append("<code>"); _mMarkdown.HtmlEncode(sb, str, t.StartOffset, t.Length); sb.Append("</code>"); break; case TokenType.link: { var li = (LinkInfo)t.Data; var sf = new SpanFormatter(_mMarkdown) { DisableLinks = true }; li.Def.RenderLink(_mMarkdown, sb, sf.Format(li.LinkText)); break; } case TokenType.img: { var li = (LinkInfo)t.Data; li.Def.RenderImg(_mMarkdown, sb, li.LinkText); break; } case TokenType.footnote: { var r = (FootnoteReference)t.Data; // ReSharper disable once StringLiteralTypo sb.Append("<sup id=\"fnref:"); sb.Append(r.ID); sb.Append("\"><a href=\"#fn:"); sb.Append(r.ID); sb.Append("\" rel=\"footnote\">"); sb.Append(r.Index + 1); sb.Append("</a></sup>"); break; } case TokenType.abbreviation: { var a = (Abbreviation)t.Data; sb.Append("<abbr"); if (!string.IsNullOrEmpty(a.Title)) { sb.Append(" title=\""); _mMarkdown.HtmlEncode(sb, a.Title, 0, a.Title.Length); sb.Append("\""); } sb.Append(">"); _mMarkdown.HtmlEncode(sb, a.Abbr, 0, a.Abbr.Length); sb.Append("</abbr>"); break; } } FreeToken(t); } }