public static void ExecuteTest(string commonMark, string html, CommonMarkSettings settings = null, Func<System.IO.TextWriter, CommonMarkSettings, Formatters.HtmlFormatter> htmlFormatterFactory = null) { if (settings == null) settings = CommonMarkSettings.Default.Clone(); Helpers.LogValue("CommonMark", Denormalize(commonMark)); Helpers.LogValue("Expected", Denormalize(html)); // Arrange commonMark = Helpers.Normalize(commonMark); html = Helpers.Normalize(html); string actual; Syntax.Block document; // Act using (var reader = new System.IO.StringReader(commonMark)) using (var writer = new System.IO.StringWriter()) { if (htmlFormatterFactory != null) { settings = settings.Clone(); settings.OutputFormat = OutputFormat.CustomDelegate; settings.OutputDelegate = (doc, target, stngs) => htmlFormatterFactory(target, stngs).WriteDocument(doc); } document = CommonMarkConverter.ProcessStage1(reader, settings); CommonMarkConverter.ProcessStage2(document, settings); CommonMarkConverter.ProcessStage3(document, writer, settings); actual = writer.ToString(); } // Assert Helpers.LogValue("Actual", Denormalize(actual)); actual = Helpers.Tidy(actual); Assert.AreEqual(Helpers.Tidy(html), actual); if (htmlFormatterFactory == null) { // Verify that the extendable HTML formatter returns the same result, unless // the test was run with a custom extendable HTML formatter factory. In the // latter case, only the output of the custom extendable HTML formatter is // verified. var settingsHtmlFormatter = settings.Clone(); settingsHtmlFormatter.OutputDelegate = (doc, target, stngs) => new Formatters.HtmlFormatter(target, stngs).WriteDocument(doc); var actual2 = CommonMarkConverter.Convert(commonMark, settingsHtmlFormatter); Assert.AreEqual(actual, Helpers.Tidy(actual2), "HtmlFormatter returned a different result than HtmlFormatterSlim."); } // Additionally verify that the parser included source position information. // This is done here to catch cases during specification tests that might not be // covered in SourcePositionTests.cs. var firstFail = document.AsEnumerable().FirstOrDefault(o => o.Inline != null && o.IsOpening && o.Inline.SourceLength <= 0); if (firstFail != null) { Assert.Fail("Incorrect source position: " + firstFail); } }
public static void markdown2html(ApplicationContext context, ActiveEventArgs e) { // Making sure we clean up and remove all arguments passed in after execution using (new ArgsRemover(e.Args, false)) { // Assumes there's only one document, or creates one result of it. var md = XUtil.Single <string> (context, e.Args); // Making sure we correctly resolve URLs, if user specified a [root-url] argument. var root = e.Args.GetExChildValue("root-url", context, ""); CommonMarkSettings settings = CommonMarkSettings.Default; if (root != "") { // Unrolling path. root = context.RaiseEvent("p5.io.unroll-path", new Node("", root)).Get <string> (context); // To make sure we don't change global settings. settings = settings.Clone(); settings.UriResolver = delegate(string arg) { if (arg.StartsWithEx("http://") || arg.StartsWithEx("https://")) { return(arg); } return(root + arg.TrimStart('/')); }; } // Doing actual conversion. e.Args.Value = CommonMarkConverter.Convert(md, settings); } }
public static void ExecuteTest(string commonMark, string html, CommonMarkSettings settings = null) { if (settings == null) { settings = CommonMarkSettings.Default.Clone(); } Helpers.LogValue("CommonMark", Denormalize(commonMark)); Helpers.LogValue("Expected", Denormalize(html)); // Arrange commonMark = Helpers.Normalize(commonMark); html = Helpers.Normalize(html); string actual; Syntax.Block document; // Act using (var reader = new System.IO.StringReader(commonMark)) using (var writer = new System.IO.StringWriter()) { document = CommonMarkConverter.ProcessStage1(reader, settings); CommonMarkConverter.ProcessStage2(document, settings); CommonMarkConverter.ProcessStage3(document, writer, settings); actual = writer.ToString(); } // Assert Helpers.LogValue("Actual", Denormalize(actual)); actual = Helpers.Tidy(actual); Assert.AreEqual(Helpers.Tidy(html), actual); // Verify that the extendable HTML formatter returns the same result var settingsHtmlFormatter = settings.Clone(); settingsHtmlFormatter.OutputDelegate = (doc, target, stngs) => new Formatters.HtmlFormatter(target, stngs).WriteDocument(doc); var actual2 = CommonMarkConverter.Convert(commonMark, settingsHtmlFormatter); Assert.AreEqual(actual, Helpers.Tidy(actual2), "HtmlFormatter returned a different result than HtmlFormatterSlim."); // Additionally verify that the parser included source position information. // This is done here to catch cases during specification tests that might not be // covered in SourcePositionTests.cs. var firstFail = document.AsEnumerable().FirstOrDefault(o => o.Inline != null && o.IsOpening && o.Inline.SourceLength <= 0); if (firstFail != null) { Assert.Fail("Incorrect source position: " + firstFail); } }