Esempio n. 1
0
        // 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);
            }
        }
Esempio n. 2
0
        // 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() + ">");
            }
        }
Esempio n. 3
0
        // 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);
            }
        }