protected override void Write(NormalizeRenderer renderer, LinkInline link) { if (link.IsImage) { return; } renderer.WriteChildren(link); if (link.Label != null) { renderer.Write(link.Label); } else if (!string.IsNullOrEmpty(link.Url) && !UrlMatchesChildInline(link)) { renderer.Write(" (").Write(link.Url).Write(")"); if (!string.IsNullOrEmpty(link.Title)) { renderer.Write(" \""); renderer.Write(link.Title.Replace(@"""", @"\""")); renderer.Write("\""); } } }
protected override void Write(NormalizeRenderer renderer, LinkInline link) { if (link.IsImage) { renderer.Write('!'); } renderer.Write('['); renderer.WriteChildren(link); renderer.Write(']'); if (link.Label != null) { if (link.FirstChild is LiteralInline literal && literal.Content.Length == link.Label.Length && literal.Content.Match(link.Label)) { // collapsed reference and shortcut links if (!link.IsShortcut) { renderer.Write("[]"); } } else { // full link renderer.Write('[').Write(link.Label).Write(']'); } }
private static void AppendInline(Inline inline, StringWriter writer, NormalizeRenderer renderer) { switch (inline) { case LineBreakInline: writer.Write(' '); break; case EmphasisInline emphasis: foreach (var item in emphasis) { AppendInline(item, writer, renderer); } break; case CodeInline code: writer.Write(code.Content); break; case LinkInline link: foreach (var item in link) { AppendInline(item, writer, renderer); } writer.Write($" ({link.Url})"); break; default: renderer.Render(inline); break; } }
private static void Render(MarkdownDocument doc, string fileName) { Console.WriteLine("Creating {0}", fileName); string directoryName = Path.GetDirectoryName(fileName); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } var options = new NormalizeOptions(); using (var writer = new StreamWriter(fileName)) { var renderer = new NormalizeRenderer(writer, options); renderer.ObjectRenderers.Add(new NormalizeTableRenderer()); var linkReferenceDefinitionRenderer = renderer.ObjectRenderers.Find <LinkReferenceDefinitionRenderer>(); if (linkReferenceDefinitionRenderer != null) { renderer.ObjectRenderers.Remove(linkReferenceDefinitionRenderer); } renderer.Render(doc); } }
protected override void Write(NormalizeRenderer renderer, CodeInline obj) { var delimiter = obj.Content.Contains(obj.Delimiter + "") ? new string(obj.Delimiter, 2) : obj.Delimiter + ""; renderer.Write(delimiter); renderer.Write(obj.Content); renderer.Write(delimiter); }
protected override void Write(NormalizeRenderer renderer, MathBlock obj) { renderer.EnsureLine(); renderer.WriteLine("$$"); renderer.WriteLeafRawLines(obj, true); renderer.Write("$$"); renderer.FinishBlock(renderer.Options.EmptyLineAfterCodeBlock); }
protected override void Write(NormalizeRenderer renderer, LiteralInline obj) { if (obj.IsFirstCharacterEscaped && obj.Content.Length > 0 && obj.Content[obj.Content.Start].IsAsciiPunctuation()) { renderer.Write('\\'); } renderer.Write(ref obj.Content); }
private static string RenderToString(MarkdownDocument document) { using var writer = new StringWriter { NewLine = "\n" }; var renderer = new NormalizeRenderer(writer); renderer.Render(document); return(writer.ToString()); }
protected override void Write(NormalizeRenderer renderer, EmphasisInline obj) { var emphasisText = new string(obj.DelimiterChar, obj.DelimiterCount); renderer.Write(emphasisText); renderer.WriteChildren(obj); renderer.Write(emphasisText); }
protected override void Write(NormalizeRenderer renderer, LineBreakInline obj) { if (obj.IsHard) { renderer.Write(obj.IsBackslash ? "\\" : " "); } renderer.WriteLine(); }
protected override void Write(NormalizeRenderer renderer, LinkReferenceDefinition linkDef) { if (linkDef.Label == null && linkDef.Url == null) { return; } base.Write(renderer, linkDef); }
private static string GetBlockText(LeafBlock heading) { using (var stringWriter = new StringWriter()) { var renderer = new NormalizeRenderer(stringWriter); renderer.Write(heading.Inline); return(stringWriter.ToString()); } }
public MarkdownSource Update() { using var writer = new StringWriter(); var renderer = new NormalizeRenderer(writer); renderer.Render(Document); return(new MarkdownSource(writer.ToString())); }
protected override void Write( NormalizeRenderer renderer, CodeBlock codeBlock) { if (codeBlock is AnnotatedCodeBlock codeLinkBlock && codeLinkBlock.Annotations != null) { codeLinkBlock.Arguments = $"{codeLinkBlock.Annotations.Language} {codeLinkBlock.Annotations.RunArgs}"; } base.Write(renderer, codeBlock); }
protected override void Write( NormalizeRenderer renderer, CodeBlock codeBlock) { if (codeBlock is AnnotatedCodeBlock block && block.Annotations is CodeBlockAnnotations annotations) { block.Arguments = $"{block.Annotations.Language} {annotations.RunArgs}"; } base.Write(renderer, codeBlock); }
protected override void Write(NormalizeRenderer renderer, CodeInline obj) { var delimiterCount = 0; string content = obj.Content; for (var i = 0; i < content.Length; i++) { var index = content.IndexOf(obj.Delimiter, i); if (index == -1) { break; } var count = 1; for (i = index + 1; i < content.Length; i++) { if (content[i] == obj.Delimiter) { count++; } else { break; } } if (delimiterCount < count) { delimiterCount = count; } } var delimiterRun = new string(obj.Delimiter, delimiterCount + 1); renderer.Write(delimiterRun); if (content.Length != 0) { if (content[0] == obj.Delimiter) { renderer.Write(' '); } renderer.Write(content); if (content[content.Length - 1] == obj.Delimiter) { renderer.Write(' '); } } else { renderer.Write(' '); } renderer.Write(delimiterRun); }
protected override void Write(NormalizeRenderer renderer, Table obj) { renderer.EnsureLine(); foreach (var row in obj.OfType <TableRow>()) { renderer.Write(PipeSeparator); foreach (var tableCell in row) { renderer.Write(MarginSeparator); renderer.Render(tableCell); renderer.Write(MarginSeparator); renderer.Write(PipeSeparator); } renderer.WriteLine(); if (row.IsHeader) { bool alignmentEnabled = obj.ColumnDefinitions.Any(c => c.Alignment != TableColumnAlign.Left); renderer.Write(PipeSeparator); foreach (var column in obj.ColumnDefinitions) { renderer.Write(MarginSeparator); if (alignmentEnabled && (column.Alignment == TableColumnAlign.Left || column.Alignment == TableColumnAlign.Center)) { renderer.Write(AlignmentChar); } renderer.Write(HeaderSeparator); if (alignmentEnabled && (column.Alignment == TableColumnAlign.Right || column.Alignment == TableColumnAlign.Center)) { renderer.Write(AlignmentChar); } renderer.Write(MarginSeparator); renderer.Write(PipeSeparator); } renderer.WriteLine(); } } renderer.FinishBlock(true); }
public void RenderMarkdown(string fileName, MarkdownDocument document) { if (!_renderSettings.RenderMarkdown) { return; } string renamedFileName = fileName.Substring(0, fileName.Length - 3) + ".normalized.md"; var file = new File(_renderSettings.MarkdownFolder, renamedFileName); using (var writer = new StreamWriter(file.AbsolutePath)) { var normalizeRenderer = new NormalizeRenderer(writer); normalizeRenderer.Render(document); } }
static string ToMarkdown(IEnumerable <Block> blocks) { var writer = new StringWriter(); var renderer = new NormalizeRenderer(writer); var pipeline = new MarkdownPipelineBuilder().Build(); pipeline.Setup(renderer); foreach (var block in blocks) { renderer.Render(block); } // We convert \n to \r because the YAML serialization will eventually // output \n\n for \n, but \r\n for \r. return(writer.ToString().TrimEnd().Replace('\n', '\r')); }
// notes may contain html and markdown, so to include: // 1. Convert html into markdown // 2. Parse markdown into ast // 3. Normalize headings // 4. Convert ast into markdown text // 5. Add markdown text to stream public string Normalize(string text) { string markdownText = _converter.Convert(text); MarkdownDocument markdown = Markdown.Parse(markdownText); using (var writer = new StringWriter()) { var pipeline = new MarkdownPipelineBuilder().Build(); pipeline.Extensions.AddIfNotAlready <SoftlineBreakAsHardlineExtension>(); var renderer = new NormalizeRenderer(writer); pipeline.Setup(renderer); renderer.Render(markdown); writer.Flush(); return(writer.ToString()); } }
private static async Task <string> Render( PublishFormat format, MarkdownDocument document, Dictionary <string, string> outputsBySessionName) { MarkdownPipeline pipeline; IMarkdownRenderer renderer; var writer = new StringWriter(); switch (format) { case PublishFormat.Markdown: pipeline = new MarkdownPipelineBuilder() .UseNormalizeCodeBlockAnnotations(outputsBySessionName) .Build(); var normalizeRenderer = new NormalizeRenderer(writer); normalizeRenderer.Writer.NewLine = "\n"; renderer = normalizeRenderer; break; case PublishFormat.HTML: pipeline = new MarkdownPipelineBuilder() .UseCodeBlockAnnotations(inlineControls: false) .Build(); renderer = new HtmlRenderer(writer); break; default: throw new ArgumentOutOfRangeException(nameof(format), format, null); } pipeline.Setup(renderer); var blocks = document .OfType <AnnotatedCodeBlock>() .OrderBy(c => c.Order) .ToList(); await Task.WhenAll(blocks.Select(b => b.InitializeAsync())); renderer.Render(document); writer.Flush(); var rendered = writer.ToString(); return(rendered); }
/// <summary> /// Normalizes the specified markdown to a normalized markdown text. /// </summary> /// <param name="markdown">The markdown.</param> /// <param name="writer">The destination <see cref="TextWriter"/> that will receive the result of the conversion.</param> /// <param name="options">The normalize options</param> /// <param name="pipeline">The pipeline.</param> /// <param name="context">A parser context used for the parsing.</param> /// <returns>A normalized markdown text.</returns> public static MarkdownDocument Normalize(string markdown, TextWriter writer, NormalizeOptions options = null, MarkdownPipeline pipeline = null, MarkdownParserContext context = null) { pipeline = pipeline ?? new MarkdownPipelineBuilder().Build(); pipeline = CheckForSelfPipeline(pipeline, markdown); // We override the renderer with our own writer var renderer = new NormalizeRenderer(writer, options); pipeline.Setup(renderer); var document = Parse(markdown, pipeline, context); renderer.Render(document); writer.Flush(); return(document); }
protected override void Write(NormalizeRenderer renderer, LinkInline link) { if (link.IsImage) { renderer.Write('!'); } renderer.Write('['); renderer.WriteChildren(link); renderer.Write(']'); //if (link.Label != null) //{ // var literal = link.FirstChild as LiteralInline; // if (literal != null && literal.Content.Match(link.Label) && literal.Content.Length == link.Label.Length) // { // // collapsed reference and shortcut links // if (!link.IsShortcut) // { // renderer.Write("[]"); // } // } // else // { // // full link // renderer.Write('[').Write(link.Label).Write(']'); // } //} //else //{ if (!string.IsNullOrEmpty(link.Url)) { //var url = AideDeJeu.Tools.Helpers.RemoveDiacritics(link.Url).Replace(".md#", "_") + ".md"; renderer.Write('(').Write(link.Url); if (!string.IsNullOrEmpty(link.Title)) { renderer.Write(" \""); renderer.Write(link.Title.Replace(@"""", @"\""")); renderer.Write("\""); } renderer.Write(')'); } //} }
/* * public static Item ToItem(string md) * { * var pipeline = new MarkdownPipelineBuilder().UsePipeTables().Build(); * var document = MarkdownParser.Parse(md, pipeline); * * var enumerator = document.GetEnumerator(); * try * { * enumerator.MoveNext(); * while (enumerator.Current != null) * { * var block = enumerator.Current; * * if (block is HtmlBlock) * { * if (block.IsNewItem()) * { * var item = ParseItem(ref enumerator); * return item; * } * } * enumerator.MoveNext(); * } * * } * finally * { * enumerator.Dispose(); * } * return null; * } * * public static Item ParseItem(ref ContainerBlock.Enumerator enumerator) * { * var currentItem = enumerator.Current.GetNewItem(); * * if (currentItem != null) * { * enumerator.MoveNext(); * while (enumerator.Current != null) * { * var block = enumerator.Current; * * if (block is HtmlBlock) * { * if (block.IsClosingItem()) * { * return currentItem; * } * else if (block.IsNewItem()) * { * var subItem = ParseItem(ref enumerator); * * var propertyName = subItem.GetType().Name; * * if (currentItem.GetType().GetProperty(propertyName) != null) * { * PropertyInfo prop = currentItem.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance); * if (null != prop && prop.CanWrite) * { * prop.SetValue(currentItem, subItem, null); * } * } * else if (currentItem is Items) * { * var items = currentItem as Items; * items.Add(subItem); * } * } * } * * else // if (block is ContainerBlock) * { * ParseItemProperties(currentItem, block); * } * * currentItem.Markdown += enumerator.Current.ToMarkdownString(); * * enumerator.MoveNext(); * } * } * * return currentItem; * } * * public static void ParseItemProperties(Item item, Block block) * { * switch(block) * { * case Markdig.Extensions.Tables.Table table: * ParseItemProperties(item, table); * break; * case ContainerBlock blocks: * ParseItemProperties(item, blocks); * break; * case LeafBlock leaf: * ParseItemProperties(item, leaf.Inline); * break; * } * } * * public static void ParseItemProperties(Item item, ContainerBlock blocks) * { * foreach(var block in blocks) * { * ParseItemProperties(item, block); * } * } * * public static void ParseItemProperties(Item item, ContainerInline inlines) * { * if(inlines == null) * { * return; * } * PropertyInfo prop = null; * foreach (var inline in inlines) * { * if(inline is HtmlInline) * { * var tag = (inline as HtmlInline).Tag; * if(tag == "<!--br-->" || tag =="<br>") * { * * } * else if (tag.StartsWith("<!--/")) * { * prop = null; * } * else if (tag.StartsWith("<!--") && !tag.StartsWith("<!--/")) * { * var propertyName = tag.Substring(4, tag.Length - 7); * prop = item.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance); * } * } * else * { * if (null != prop && prop.CanWrite) * { * prop.SetValue(item, inline.ToMarkdownString(), null); * } * } * } * } * * * * public static bool IsNewItem(this Block block) * { * var htmlBlock = block as HtmlBlock; * if (htmlBlock.Type == HtmlBlockType.Comment) * { * var tag = htmlBlock.Lines.Lines.FirstOrDefault().Slice.ToString(); * if (!string.IsNullOrEmpty(tag) && tag != "<!--br-->" && tag != "<br>") * { * if (tag.StartsWith("<!--") && !tag.StartsWith("<!--/")) * { * return true; * } * } * } * return false; * } * * public static bool IsClosingItem(this Block block) * { * var htmlBlock = block as HtmlBlock; * if (htmlBlock.Type == HtmlBlockType.Comment) * { * var tag = htmlBlock.Lines.Lines.FirstOrDefault().Slice.ToString(); * if (!string.IsNullOrEmpty(tag) && tag != "<!--br-->" && tag != "<br>") * { * if (tag.StartsWith("<!--/")) * { * return true; * } * } * } * return false; * } * * public static Item GetNewItem(this Block block) * { * var htmlBlock = block as HtmlBlock; * if (htmlBlock.Type == HtmlBlockType.Comment) * { * var tag = htmlBlock.Lines.Lines.FirstOrDefault().Slice.ToString(); * if (!string.IsNullOrEmpty(tag) && tag != "<!--br-->" && tag != "<br>") * { * if (tag.StartsWith("<!--") && !tag.StartsWith("<!--/")) * { * var name = $"AideDeJeuLib.{tag.Substring(4, tag.Length - 7)}, AideDeJeu"; * var type = Type.GetType(name); * var instance = Activator.CreateInstance(type) as Item; * return instance; * } * } * } * return null; * } */ /* * public static Item ToItem(string md) * { * var pipeline = new MarkdownPipelineBuilder().UsePipeTables().Build(); * var document = MarkdownParser.Parse(md, pipeline); * * var enumerator = document.GetEnumerator(); * try * { * enumerator.MoveNext(); * while (enumerator.Current != null) * { * var block = enumerator.Current; * * if (enumerator.Current is ParagraphBlock) * { * if(block.IsNewItem()) * { * var item = block.GetNewItem(); * item.Parse(ref enumerator); * return item; * } * } * enumerator.MoveNext(); * } * * } * finally * { * enumerator.Dispose(); * } * return null; * } * * public static bool IsNewItem(this Block block) * { * var paragraphBlock = block as ParagraphBlock; * var linkInline = paragraphBlock?.Inline?.FirstChild as LinkInline; * if (linkInline != null) * { * var label = linkInline.Label; * var title = linkInline.Title; * if (title == string.Empty && label != string.Empty) * { * return true; * } * } * return false; * } * * public static bool IsClosingItem(this Block block) * { * var paragraphBlock = block as ParagraphBlock; * var linkInline = paragraphBlock?.Inline?.FirstChild as LinkInline; * if (linkInline != null) * { * var label = linkInline.Label; * var title = linkInline.Title; * if (title == string.Empty && label == string.Empty) * { * return true; * } * } * return false; * } * * public static Item GetNewItem(this Block block) * { * var paragraphBlock = block as ParagraphBlock; * var linkInline = paragraphBlock?.Inline?.FirstChild as LinkInline; * if (linkInline != null) * { * var label = linkInline.Label; * var title = linkInline.Title; * var url = linkInline.Url; * if (title == string.Empty) * { * var name = $"AideDeJeuLib.{label}, AideDeJeu"; * var type = Type.GetType(name); * var instance = Activator.CreateInstance(type) as Item; * return instance; * } * } * return null; * } */ public static string ToMarkdownString(this Block block) { var pipeline = new MarkdownPipelineBuilder() .UsePipeTables() .Build(); using (var writer = new StringWriter()) { var renderer = new NormalizeRenderer(writer); renderer.ObjectRenderers.Remove(renderer.ObjectRenderers.FirstOrDefault(i => i is LinkInlineRenderer)); renderer.ObjectRenderers.Add(new LinkInlineRendererEx()); renderer.ObjectRenderers.Add(new TableRenderer()); pipeline.Setup(renderer); renderer.Render(block); return(writer.ToString()); } }
protected override void Write(NormalizeRenderer renderer, LinkInline link) { if (link.IsImage) { renderer.Write('!'); } renderer.Write('['); renderer.WriteChildren(link); renderer.Write(']'); if (link.Label != null) { var literal = link.FirstChild as LiteralInline; if (literal != null && literal.Content.Match(link.Label) && literal.Content.Length == link.Label.Length) { // collapsed reference and shortcut links if (!link.IsShortcut) { renderer.Write("[]"); } } else { // full link renderer.Write('[').Write(link.Label).Write(']'); } } else { if (!string.IsNullOrEmpty(link.Url)) { renderer.Write('(').Write(link.Url); if (!string.IsNullOrEmpty(link.Title)) { renderer.Write(" \""); renderer.Write(link.Title.Replace(@"""", @"\""")); renderer.Write("\""); } renderer.Write(')'); } } }
public string Process(string text, Func <long, string> urlGenerator) { MarkdownDocument markdown = Markdown.Parse(text); foreach (var link in markdown.Descendants().Where(e => e is LinkInline).Cast <LinkInline>()) { if (int.TryParse(link.Url, out var dataIndex)) { link.Url = urlGenerator(dataIndex); } } var writer = new StringWriter(); NormalizeRenderer renderer = new NormalizeRenderer(writer); renderer.Render(markdown); return(writer.ToString()); }
private static string RenderToPlainText(MarkdownDocument document) { using var writer = new StringWriter { NewLine = "\n" }; var renderer = new NormalizeRenderer(writer); var firstBlock = true; foreach (var block in document) { if (!firstBlock) { writer.WriteLine(); } firstBlock = false; AppendBlock(block, writer, renderer); } return(writer.ToString()); }
private static void AssertSyntax(string expected, MarkdownObject syntax) { var writer = new StringWriter(); var normalizer = new NormalizeRenderer(writer); var document = new MarkdownDocument(); if (syntax is Block) { document.Add(syntax as Block); } else { throw new InvalidOperationException(); } normalizer.Render(document); var actual = writer.ToString(); Assert.AreEqual(expected, actual); }
/// <summary> /// Normalizes the specified markdown to a normalized markdown text. /// </summary> /// <param name="markdown">The markdown.</param> /// <param name="writer">The destination <see cref="TextWriter"/> that will receive the result of the conversion.</param> /// <param name="options">The normalize options</param> /// <param name="pipeline">The pipeline.</param> /// <param name="context">A parser context used for the parsing.</param> /// <returns>A normalized markdown text.</returns> public static MarkdownDocument Normalize(string markdown, TextWriter writer, NormalizeOptions?options = null, MarkdownPipeline?pipeline = null, MarkdownParserContext?context = null) { if (markdown is null) { ThrowHelper.ArgumentNullException_markdown(); } pipeline = GetPipeline(pipeline, markdown); var document = MarkdownParser.Parse(markdown, pipeline, context); var renderer = new NormalizeRenderer(writer, options); pipeline.Setup(renderer); renderer.Render(document); writer.Flush(); return(document); }
public async Task <string> SolveImagesUrlAsync(string source, Func <string, Task <string> > converter) { using (var tw = new StringWriter()) { var writer = new NormalizeRenderer(tw); Pipeline.Setup(writer); var doc1 = Parse(source); await doc1.TransverseAsync <LinkInline>(async item => { if (item.IsImage) { item.Url = await converter(item.Url); } }); writer.Render(doc1); writer.Writer.Flush(); return(tw.ToString()); } }