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); }
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); }
internal void RenderLink(Markdown m, StringBuilder b, string link_text) { if (url.StartsWith("mailto:")) { b.Append("<a href=\""); Utils.HtmlRandomize(b, url); b.Append('\"'); if (!String.IsNullOrEmpty(title)) { b.Append(" title=\""); Utils.SmartHtmlEncodeAmpsAndAngles(b, title); b.Append('\"'); } b.Append('>'); Utils.HtmlRandomize(b, link_text); b.Append("</a>"); } else { HtmlTag tag = new HtmlTag("a"); // encode url StringBuilder sb = m.GetStringBuilder(); Utils.SmartHtmlEncodeAmpsAndAngles(sb, url); tag.attributes["href"] = sb.ToString(); // encode title if (!String.IsNullOrEmpty(title)) { sb.Length = 0; Utils.SmartHtmlEncodeAmpsAndAngles(sb, title); tag.attributes["title"] = sb.ToString(); } // Do user processing m.OnPrepareLink(tag); var href = tag.attributes["href"]; if (href == "") { tag = new HtmlTag("span"); tag.attributes["class"] = "brokenlink"; } else if (href.StartsWith("http://") || href.StartsWith("https://")) { tag.attributes["class"] = "foreignlink"; } // Render the opening tag tag.RenderOpening(b); b.Append(link_text); // Link text already escaped by SpanFormatter b.AppendFormat("</{0}>", tag.name); if (href == "") { m.AfterBrokenLink(b, sb.ToString(), link_text); } } }
internal void RenderLink(Markdown m, StringBuilder b, string link_text, List <string> specialAttributes) { if (this.Url.StartsWith("mailto:")) { b.Append("<a href=\""); Utils.HtmlRandomize(b, this.Url); b.Append('\"'); if (!String.IsNullOrEmpty(this.Title)) { b.Append(" title=\""); Utils.SmartHtmlEncodeAmpsAndAngles(b, this.Title); b.Append('\"'); } b.Append('>'); Utils.HtmlRandomize(b, link_text); b.Append("</a>"); } else { HtmlTag tag = new HtmlTag("a"); // encode url StringBuilder sb = m.GetStringBuilder(); Utils.SmartHtmlEncodeAmpsAndAngles(sb, this.Url); tag.attributes["href"] = sb.ToString(); // encode title if (!String.IsNullOrEmpty(this.Title)) { sb.Length = 0; Utils.SmartHtmlEncodeAmpsAndAngles(sb, this.Title); tag.attributes["title"] = sb.ToString(); } if (specialAttributes.Any()) { LinkDefinition.HandleSpecialAttributes(specialAttributes, sb, tag); } // Do user processing m.OnPrepareLink(tag); // Render the opening tag tag.RenderOpening(b); b.Append(link_text); // Link text already escaped by SpanFormatter b.Append("</a>"); } }
internal void RenderLink(Markdown m, StringBuilder b, string linkText) { if (URL.StartsWith("mailto:")) { b.Append("<a href=\""); Utils.HtmlRandomize(b, URL); b.Append('\"'); if (!string.IsNullOrEmpty(Title)) { b.Append(" title=\""); Utils.SmartHtmlEncodeAmpsAndAngles(b, Title); b.Append('\"'); } b.Append('>'); Utils.HtmlRandomize(b, linkText); b.Append("</a>"); } else { var tag = new HtmlTag("a"); // encode url var sb = m.GetStringBuilder(); Utils.SmartHtmlEncodeAmpsAndAngles(sb, URL); tag.Attributes["href"] = sb.ToString(); // encode title if (!string.IsNullOrEmpty(Title)) { sb.Length = 0; Utils.SmartHtmlEncodeAmpsAndAngles(sb, Title); tag.Attributes["title"] = sb.ToString(); } // Do user processing m.OnPrepareLink(tag); // Render the opening tag tag.RenderOpening(b); b.Append(linkText); // Link text already escaped by SpanFormatter b.Append("</a>"); } }
internal void FormatParagraph(StringBuilder dest, string str, int start, int len) { // Parse the string into a list of tokens Tokenize(str, start, len); // If the image is a titled image and there's a class defined for titled images, we'll render it using special markup, otherwise we'll render // the image as-is. if (m_Tokens.Count == 1 && !string.IsNullOrWhiteSpace(m_Markdown.HtmlClassTitledImages) && m_Tokens[0].type == TokenType.img && m_Tokens[0].data is LinkInfo && !string.IsNullOrWhiteSpace(((LinkInfo)m_Tokens[0].data).Definition.Title)) { // Grab the link info LinkInfo li = (LinkInfo)m_Tokens[0].data; // Render the div opening dest.Append("<div class=\""); dest.Append(m_Markdown.HtmlClassTitledImages); dest.Append("\">\n"); // Render the img m_Markdown.RenderingTitledImage = true; Render(dest, str); m_Markdown.RenderingTitledImage = false; dest.Append("\n"); // Render the title if (!String.IsNullOrEmpty(li.Definition.Title)) { dest.Append("<p>"); Utils.SmartHtmlEncodeAmpsAndAngles(dest, li.Definition.Title); dest.Append("</p>\n"); } dest.Append("</div>\n"); } else { // Render the paragraph dest.Append("<p>"); Render(dest, str); dest.Append("</p>\n"); } }
internal void FormatParagraph(StringBuilder dest, string str, int start, int len) { // Parse the string into a list of tokens Tokenize(str, start, len); // Titled image? if (m_Tokens.Count == 1 && m_Markdown.HtmlClassTitledImages != null && m_Tokens[0].type == TokenType.img) { // Grab the link info LinkInfo li = (LinkInfo)m_Tokens[0].data; // Render the div opening dest.Append("<div class=\""); dest.Append(m_Markdown.HtmlClassTitledImages); dest.Append("\">\n"); // Render the img m_Markdown.RenderingTitledImage = true; Render(dest, str); m_Markdown.RenderingTitledImage = false; dest.Append("\n"); // Render the title if (!String.IsNullOrEmpty(li.def.title)) { dest.Append("<p>"); Utils.SmartHtmlEncodeAmpsAndAngles(dest, li.def.title); dest.Append("</p>\n"); } dest.Append("</div>\n"); } else { // Render the paragraph dest.Append("<p>"); Render(dest, str); dest.Append("</p>\n"); } }
private static void HandleSpecialAttributes(List <string> specialAttributes, StringBuilder sb, HtmlTag tag) { string id = specialAttributes.FirstOrDefault(s => s.StartsWith("#")); if (id != null && id.Length > 1) { sb.Length = 0; Utils.SmartHtmlEncodeAmpsAndAngles(sb, id.Substring(1)); tag.attributes["id"] = sb.ToString(); } var cssClasses = new List <string>(); foreach (var cssClass in specialAttributes.Where(s => s.StartsWith(".") && s.Length > 1)) { sb.Length = 0; Utils.SmartHtmlEncodeAmpsAndAngles(sb, cssClass.Substring(1)); cssClasses.Add(sb.ToString()); } if (cssClasses.Any()) { tag.attributes["class"] = string.Join(" ", cssClasses.ToArray()); } foreach (var nameValuePair in specialAttributes.Where(s => s.Contains("=") && s.Length > 2 && !s.StartsWith(".") && !s.StartsWith("#"))) { var pair = nameValuePair.Split('='); if (pair.Length == 2) { sb.Length = 0; Utils.SmartHtmlEncodeAmpsAndAngles(sb, pair[0]); var key = sb.ToString(); sb.Length = 0; Utils.SmartHtmlEncodeAmpsAndAngles(sb, pair[1]); var value = sb.ToString(); tag.attributes[key] = value; } } }
internal void RenderLink(Markdown m, StringBuilder b, string link_text, List <string> specialAttributes) { if (this.Url.StartsWith("mailto:")) { b.Append("<a href=\""); Utils.HtmlRandomize(b, this.Url); b.Append('\"'); if (!String.IsNullOrEmpty(this.Title)) { b.Append(" title=\""); Utils.SmartHtmlEncodeAmpsAndAngles(b, this.Title); b.Append('\"'); } b.Append('>'); Utils.HtmlRandomize(b, link_text); b.Append("</a>"); } else { HtmlTag tag = new HtmlTag("a"); var url = this.Url; if (m.DocNetMode && m.ConvertLocalLinks) { // A few requirements before we can convert local links: // 1. Link contains .md // 2. Link is relative // 3. Link is included in the index var index = url.LastIndexOf(".md", StringComparison.OrdinalIgnoreCase); if (index >= 0) { var linkProcessor = m.LocalLinkProcessor; if (linkProcessor != null) { url = linkProcessor(url); } else { Uri uri; if (Uri.TryCreate(url, UriKind.Relative, out uri)) { url = String.Concat(url.Substring(0, index), ".htm", url.Substring(index + ".md".Length)); } } } } // encode url StringBuilder sb = m.GetStringBuilder(); Utils.SmartHtmlEncodeAmpsAndAngles(sb, url); tag.attributes["href"] = sb.ToString(); // encode title if (!String.IsNullOrEmpty(this.Title)) { sb.Length = 0; Utils.SmartHtmlEncodeAmpsAndAngles(sb, this.Title); tag.attributes["title"] = sb.ToString(); } if (specialAttributes.Any()) { LinkDefinition.HandleSpecialAttributes(specialAttributes, sb, tag); } // Do user processing m.OnPrepareLink(tag); // Render the opening tag tag.RenderOpening(b); b.Append(link_text); // Link text already escaped by SpanFormatter b.Append("</a>"); } }