public static string FormatCodePrettyPrint(Markdown m, string code) { var match = rxExtractLanguage.Match(code); string language = null; if (match.Success) { // Save the language var g=(Group)match.Groups[2]; language = g.ToString(); // Remove the first line code = code.Substring(match.Groups[1].Length); } if (language == null) { var d = m.GetLinkDefinition("default_syntax"); if (d!=null) language = d.title; } if (language == "C#") language = "csharp"; if (language == "C++") language = "cpp"; if (string.IsNullOrEmpty(language)) return string.Format("<pre><code>{0}</code></pre>\n", code); else return string.Format("<pre class=\"prettyprint lang-{0}\"><code>{1}</code></pre>\n", language.ToLowerInvariant(), code); }
public static void RunTestJS(string input, bool SafeMode, bool ExtraMode, bool MarkdownInHtml, bool AutoHeadingIDs) { string normalized_input = input.Replace("\r\n", "\n").Replace("\r", "\n"); // Work out the expected output using C# implementation var md = new Markdown(); md.SafeMode = SafeMode; md.ExtraMode = ExtraMode; md.MarkdownInHtml = MarkdownInHtml; md.AutoHeadingIDs = AutoHeadingIDs; string expected = md.Transform(normalized_input); // Transform using javascript implementation string actual = TransformUsingJS(input, SafeMode, ExtraMode, MarkdownInHtml, AutoHeadingIDs); actual = actual.Replace("\r", ""); expected = expected.Replace("\r", ""); string sep = new string('-', 30) + "\n"; Console.WriteLine("Input:\n" + sep + input); Console.WriteLine("Actual:\n" + sep + actual); Console.WriteLine("Expected:\n" + sep + expected); // Check it Assert.AreEqual(expected, actual); }
/// <summary> /// performs a rough benchmark of the Markdown engine using small, medium, and large input samples /// please DO NOT MODIFY the input samples or the benchmark itself as this will invalidate previous /// benchmark runs! /// </summary> static void Benchmark(string text, int iterations) { var m = new Markdown(); var sw = new Stopwatch(); sw.Start(); for (int i = 0; i < iterations; i++) m.Transform(text); sw.Stop(); Console.WriteLine("input string length: " + text.Length); Console.Write(iterations + " iteration" + (iterations == 1 ? "" : "s") + " in " + sw.ElapsedMilliseconds + " ms"); if (iterations == 1) Console.WriteLine(); else Console.WriteLine(" (" + Convert.ToDouble(sw.ElapsedMilliseconds) / Convert.ToDouble(iterations) + " ms per iteration)"); }
public static void RunResourceTest(string resourceName) { string input = Utils.LoadTextResource(resourceName); string expected = Utils.LoadTextResource(System.IO.Path.ChangeExtension(resourceName, "html")); var md = new Markdown(); md.SafeMode = resourceName.IndexOf("(SafeMode)") >= 0;; md.ExtraMode = resourceName.IndexOf("(ExtraMode)") >= 0;; md.MarkdownInHtml = resourceName.IndexOf("(MarkdownInHtml)") >= 0; md.AutoHeadingIDs = resourceName.IndexOf("(AutoHeadingIDs)") >= 0; string actual = md.Transform(input); string actual_clean = Utils.strip_redundant_whitespace(actual); string expected_clean = Utils.strip_redundant_whitespace(expected); string sep = new string('-', 30) + "\n"; Console.WriteLine("Input:\n" + sep + input); Console.WriteLine("Actual:\n" + sep + actual); Console.WriteLine("Expected:\n" + sep + expected); Assert.AreEqual(expected_clean, actual_clean); }
// Constructor // A reference to the owning markdown object is passed incase // we need to check for formatting options public SpanFormatter(Markdown m) { m_Markdown = m; }
public void SetUp() { m = new Markdown(); m.AddLinkDefinition(new LinkDefinition("link1", "url.com", "title")); m.AddLinkDefinition(new LinkDefinition("link2", "url.com")); m.AddLinkDefinition(new LinkDefinition("img1", "url.com/image.png", "title")); m.AddLinkDefinition(new LinkDefinition("img2", "url.com/image.png")); s = new SpanFormatter(m); }
internal BlockProcessor(Markdown m, bool MarkdownInHtml, BlockType parentType) { m_markdown = m; m_bMarkdownInHtml = MarkdownInHtml; m_parentType = parentType; }
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 string ResolveHeaderID(Markdown m) { // Already resolved? if (this.data!=null) return (string)this.data; // Approach 1 - PHP Markdown Extra style header id int end=contentEnd; string id = Utils.StripHtmlID(buf, contentStart, ref end); if (id != null) { contentEnd = end; } else { // Approach 2 - pandoc style header id id = m.MakeUniqueHeaderID(buf, contentStart, contentLen); } this.data = id; return id; }
internal void RenderChildrenPlain(Markdown m, StringBuilder b) { foreach (var block in children) { block.RenderPlain(m, b); } }
internal void RenderPlain(Markdown m, StringBuilder b) { switch (blockType) { case BlockType.Blank: return; case BlockType.p: case BlockType.span: m.SpanFormatter.FormatPlain(b, buf, contentStart, contentLen); b.Append(" "); break; case BlockType.h1: case BlockType.h2: case BlockType.h3: case BlockType.h4: case BlockType.h5: case BlockType.h6: m.SpanFormatter.FormatPlain(b, buf, contentStart, contentLen); b.Append(" - "); break; case BlockType.ol_li: case BlockType.ul_li: b.Append("* "); m.SpanFormatter.FormatPlain(b, buf, contentStart, contentLen); b.Append(" "); break; case BlockType.dd: if (children != null) { b.Append("\n"); RenderChildrenPlain(m, b); } else m.SpanFormatter.FormatPlain(b, buf, contentStart, contentLen); break; case BlockType.dt: { if (children == null) { foreach (var l in Content.Split('\n')) { var str = l.Trim(); m.SpanFormatter.FormatPlain(b, str, 0, str.Length); } } else { RenderChildrenPlain(m, b); } break; } case BlockType.dl: RenderChildrenPlain(m, b); return; case BlockType.codeblock: foreach (var line in children) { b.Append(line.buf, line.contentStart, line.contentLen); b.Append(" "); } return; case BlockType.quote: case BlockType.li: case BlockType.ol: case BlockType.ul: case BlockType.HtmlTag: RenderChildrenPlain(m, b); return; } }
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(">"); } } 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 { 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(" "); } 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; } }
internal void RenderRow(Markdown m, StringBuilder b, List<TableCellDefinition> row, string type) { // Count of columns spanned var totalColSpan = 0; for (int i = 0; i < row.Count; i++) totalColSpan += row[i].ColSpan; // Add missing columns in row for (int i = totalColSpan; i < Columns.Count; i++) row.Add(new TableCellDefinition(" ", TableCellAlignment.NA, 1, 1, TableCellStyle.TD)); // Render row for (int i = 0; i < row.Count; i++) { var alig = TableCellAlignment.NA; if (i < Columns.Count && Columns[i] != TableCellAlignment.NA) alig = Columns[i]; var cell = row[i]; b.Append("\t"); cell.RenderOpenTag(b, type, alig); m.SpanFormatter.Format(b, cell.Content); cell.RenderCloseTag(b, type); b.Append("\n"); } }
public void Render(Markdown m, StringBuilder b) { b.Append("<table>\n"); if (Headers != null && Headers.Count > 0) { b.Append("<thead>\n"); foreach (var headerRow in Headers) { b.Append("<tr>\n"); RenderRow(m, b, headerRow, "th"); b.Append("</tr>\n"); } b.Append("</thead>\n"); } b.Append("<tbody>\n"); foreach (var contentRow in Rows) { b.Append("<tr>\n"); RenderRow(m, b, contentRow, null); b.Append("</tr>\n"); } b.Append("</tbody>\n"); b.Append("</table>\n"); }
public void SetUp() { m = new Markdown(); m.AutoHeadingIDs = true; m.ExtraMode = true; }
public void SetUp() { m = new Markdown(); s = new SpanFormatter(m); }
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(">"); } } 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 { 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(" "); } 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; } }
static void Main(string[] args) { Markdown m = new Markdown(); m.SafeMode = false; m.ExtraMode = true; m.AutoHeadingIDs = true; // m.SectionHeader = "<div class=\"header\">{0}</div>\n"; // m.SectionHeadingSuffix = "<div class=\"heading\">{0}</div>\n"; // m.SectionFooter = "<div class=\"footer\">{0}</div>\n\n"; // m.SectionHeader = "\n<div class=\"section_links\"><a href=\"/edit?section={0}\">Edit</a></div>\n"; // m.HtmlClassTitledImages = "figure"; // m.DocumentRoot = "C:\\users\\bradr\\desktop"; // m.DocumentLocation = "C:\\users\\bradr\\desktop\\100D5000"; // m.MaxImageWidth = 500; m.FormatCodeBlock = FormatCodePrettyPrint; m.ExtractHeadBlocks = true; m.UserBreaks = true; string markdown=FileContents("input.txt"); string str = m.Transform(markdown); Console.Write(str); var sections = Markdown.SplitUserSections(markdown); for (int i = 0; i < sections.Count; i++) { Console.WriteLine("---- Section {0} ----", i); Console.Write(sections[i]); Console.Write("\n"); } Console.WriteLine("------------------"); Console.WriteLine("------Joined-------"); Console.WriteLine(Markdown.JoinUserSections(sections)); Console.WriteLine("------Joined-------"); Console.WriteLine("------start head block-------"); Console.WriteLine(m.HeadBlockContent); Console.WriteLine("------end head block-------"); }
internal void RenderPlain(Markdown m, StringBuilder b) { switch (blockType) { case BlockType.Blank: return; case BlockType.p: case BlockType.span: m.SpanFormatter.FormatPlain(b, buf, contentStart, contentLen); b.Append(" "); break; case BlockType.h1: case BlockType.h2: case BlockType.h3: case BlockType.h4: case BlockType.h5: case BlockType.h6: m.SpanFormatter.FormatPlain(b, buf, contentStart, contentLen); b.Append(" - "); break; case BlockType.ol_li: case BlockType.ul_li: b.Append("* "); m.SpanFormatter.FormatPlain(b, buf, contentStart, contentLen); b.Append(" "); break; case BlockType.dd: if (children != null) { b.Append("\n"); RenderChildrenPlain(m, b); } else { m.SpanFormatter.FormatPlain(b, buf, contentStart, contentLen); } break; case BlockType.dt: { if (children == null) { foreach (var l in Content.Split('\n')) { var str = l.Trim(); m.SpanFormatter.FormatPlain(b, str, 0, str.Length); } } else { RenderChildrenPlain(m, b); } break; } case BlockType.dl: RenderChildrenPlain(m, b); return; case BlockType.codeblock: foreach (var line in children) { b.Append(line.buf, line.contentStart, line.contentLen); b.Append(" "); } return; case BlockType.quote: case BlockType.li: case BlockType.ol: case BlockType.ul: case BlockType.HtmlTag: RenderChildrenPlain(m, b); return; } }
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); // Render the opening tag tag.RenderOpening(b); b.Append(link_text); // Link text already escaped by SpanFormatter b.Append("</a>"); } }
public BlockProcessor(Markdown m, bool MarkdownInHtml) { m_markdown = m; m_bMarkdownInHtml = MarkdownInHtml; m_parentType = BlockType.Blank; }