mirror of
https://github.com/xoofx/markdig.git
synced 2026-11-16 17:30:52 +00:00
Add normalize renderers for core CommonMark components (wip #17)
This commit is contained in:
committed by
T. Thiery
parent
d58db530bb
commit
3cc405b05b
@@ -62,6 +62,7 @@
|
||||
<Compile Include="TestLineReader.cs" />
|
||||
<Compile Include="TestLinkHelper.cs" />
|
||||
<Compile Include="TestOrderedList.cs" />
|
||||
<Compile Include="TestNormalize.cs" />
|
||||
<Compile Include="TestPragmaLines.cs" />
|
||||
<Compile Include="TestSourcePosition.cs" />
|
||||
<Compile Include="TestStringSliceList.cs" />
|
||||
|
||||
121
src/Markdig.Tests/TestNormalize.cs
Normal file
121
src/Markdig.Tests/TestNormalize.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Markdig.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class TestNormalize
|
||||
{
|
||||
|
||||
[Test]
|
||||
public void TestParagraph()
|
||||
{
|
||||
AssertNormalize("This is a plain paragraph");
|
||||
AssertNormalize(@"This
|
||||
is
|
||||
a
|
||||
plain
|
||||
paragraph");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestParagraphMulti()
|
||||
{
|
||||
AssertNormalize(@"line1
|
||||
|
||||
line2
|
||||
|
||||
line3");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestListUnordered()
|
||||
{
|
||||
AssertNormalize(@"
|
||||
- a
|
||||
- b
|
||||
- c
|
||||
");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestListOrdered()
|
||||
{
|
||||
AssertNormalize(@"
|
||||
1. a
|
||||
2. b
|
||||
3. c
|
||||
");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestHeaderAndParagraph()
|
||||
{
|
||||
AssertNormalize(@"
|
||||
# heading
|
||||
|
||||
paragraph
|
||||
");
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void TestQuote()
|
||||
{
|
||||
AssertNormalize(@"
|
||||
> test1
|
||||
>
|
||||
> test2
|
||||
");
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void TestEmphasis()
|
||||
{
|
||||
AssertNormalize("This is a plain **paragraph**");
|
||||
AssertNormalize("This is a plain *paragraph*");
|
||||
AssertNormalize("This is a plain _paragraph_");
|
||||
AssertNormalize("This is a plain __paragraph__");
|
||||
AssertNormalize("This is a pl*ai*n **paragraph**");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestLinks()
|
||||
{
|
||||
AssertNormalize("This is a [link](http://company.com)");
|
||||
AssertNormalize("This is an ");
|
||||
}
|
||||
|
||||
|
||||
public void AssertNormalize(string input, string expected = null)
|
||||
{
|
||||
expected = expected ?? input;
|
||||
input = NormText(input);
|
||||
expected = NormText(expected);
|
||||
|
||||
var result = Markdown.Normalize(input);
|
||||
result = NormText(result);
|
||||
|
||||
Console.WriteLine("```````````````````Source");
|
||||
Console.WriteLine(TestParser.DisplaySpaceAndTabs(input));
|
||||
Console.WriteLine("```````````````````Result");
|
||||
Console.WriteLine(TestParser.DisplaySpaceAndTabs(result));
|
||||
Console.WriteLine("```````````````````Expected");
|
||||
Console.WriteLine(TestParser.DisplaySpaceAndTabs(expected));
|
||||
Console.WriteLine("```````````````````");
|
||||
Console.WriteLine();
|
||||
|
||||
TextAssert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
private static string NormText(string text)
|
||||
{
|
||||
return text.Trim().Replace("\r\n", "\n").Replace('\r', '\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ using System.Reflection;
|
||||
using Markdig.Extensions.SelfPipeline;
|
||||
using Markdig.Parsers;
|
||||
using Markdig.Renderers;
|
||||
using Markdig.Renderers.Normalize;
|
||||
using Markdig.Syntax;
|
||||
|
||||
namespace Markdig
|
||||
@@ -22,6 +23,42 @@ namespace Markdig
|
||||
public static readonly string Version = ((AssemblyFileVersionAttribute) typeof(Markdown).Assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)[0]).Version;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes the specified markdown to a normalized markdown text.
|
||||
/// </summary>
|
||||
/// <param name="markdown">The markdown.</param>
|
||||
/// <param name="pipeline">The pipeline.</param>
|
||||
/// <returns>A normalized markdown text.</returns>
|
||||
public static string Normalize(string markdown, MarkdownPipeline pipeline = null)
|
||||
{
|
||||
var writer = new StringWriter();
|
||||
Normalize(markdown, writer, pipeline);
|
||||
return writer.ToString();
|
||||
}
|
||||
|
||||
/// <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="pipeline">The pipeline.</param>
|
||||
/// <returns>A normalized markdown text.</returns>
|
||||
public static MarkdownDocument Normalize(string markdown, TextWriter writer, MarkdownPipeline pipeline = null)
|
||||
{
|
||||
pipeline = pipeline ?? new MarkdownPipelineBuilder().Build();
|
||||
pipeline = CheckForSelfPipeline(pipeline, markdown);
|
||||
|
||||
// We override the renderer with our own writer
|
||||
var renderer = new NormalizeRenderer(writer);
|
||||
pipeline.Setup(renderer);
|
||||
|
||||
var document = Parse(markdown, pipeline);
|
||||
renderer.Render(document);
|
||||
writer.Flush();
|
||||
|
||||
return document;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a Markdown string to HTML.
|
||||
/// </summary>
|
||||
|
||||
@@ -88,7 +88,9 @@ namespace Markdig.Parsers
|
||||
processor.NewBlocks.Push(new ThematicBreakBlock(this)
|
||||
{
|
||||
Column = processor.Column,
|
||||
Span = new SourceSpan(startPosition, line.End)
|
||||
Span = new SourceSpan(startPosition, line.End),
|
||||
ThematicChar = breakChar,
|
||||
ThematicCharCount = breakCharCount
|
||||
});
|
||||
return BlockState.BreakDiscard;
|
||||
}
|
||||
|
||||
@@ -39,12 +39,12 @@ namespace Markdig.Renderers.Normalize
|
||||
}
|
||||
renderer.WriteLine();
|
||||
|
||||
renderer.WriteLeafRawLines(obj, true, false);
|
||||
renderer.WriteLeafRawLines(obj, true);
|
||||
renderer.WriteLine(opening);
|
||||
}
|
||||
else
|
||||
{
|
||||
renderer.WriteLeafRawLines(obj, true, false, true);
|
||||
renderer.WriteLeafRawLines(obj, true, true);
|
||||
renderer.WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace Markdig.Renderers.Normalize
|
||||
|
||||
renderer.Write(headingText).Write(' ');
|
||||
renderer.WriteLeafInline(obj);
|
||||
renderer.WriteLine();
|
||||
renderer.WriteLine().WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
using Markdig.Syntax.Inlines;
|
||||
|
||||
namespace Markdig.Renderers.Normalize.Inlines
|
||||
{
|
||||
/// <summary>
|
||||
/// A Normalize renderer for an <see cref="AutolinkInline"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref="Markdig.Renderers.Normalize.NormalizeObjectRenderer{Markdig.Syntax.Inlines.AutolinkInline}" />
|
||||
public class AutolinkInlineRenderer : NormalizeObjectRenderer<AutolinkInline>
|
||||
{
|
||||
protected override void Write(NormalizeRenderer renderer, AutolinkInline obj)
|
||||
{
|
||||
renderer.Write('<').Write(obj.Url).Write('>');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
using Markdig.Syntax.Inlines;
|
||||
|
||||
namespace Markdig.Renderers.Normalize.Inlines
|
||||
{
|
||||
/// <summary>
|
||||
/// A Normalize renderer for a <see cref="CodeInline"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref="Markdig.Renderers.Normalize.NormalizeObjectRenderer{Markdig.Syntax.Inlines.CodeInline}" />
|
||||
public class CodeInlineRenderer : NormalizeObjectRenderer<CodeInline>
|
||||
{
|
||||
protected override void Write(NormalizeRenderer renderer, CodeInline obj)
|
||||
{
|
||||
renderer.Write(obj.Delimiter);
|
||||
renderer.Write(obj.Content); // TODO: escape `
|
||||
renderer.Write(obj.Delimiter);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
using Markdig.Syntax.Inlines;
|
||||
|
||||
namespace Markdig.Renderers.Normalize.Inlines
|
||||
{
|
||||
/// <summary>
|
||||
/// A Normalize renderer for a <see cref="DelimiterInline"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref="Markdig.Renderers.Normalize.NormalizeObjectRenderer{Markdig.Syntax.Inlines.DelimiterInline}" />
|
||||
public class DelimiterInlineRenderer : NormalizeObjectRenderer<DelimiterInline>
|
||||
{
|
||||
protected override void Write(NormalizeRenderer renderer, DelimiterInline obj)
|
||||
{
|
||||
renderer.Write(obj.ToLiteral());
|
||||
renderer.WriteChildren(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
using Markdig.Syntax.Inlines;
|
||||
|
||||
namespace Markdig.Renderers.Normalize.Inlines
|
||||
{
|
||||
/// <summary>
|
||||
/// A Normalize renderer for an <see cref="EmphasisInline"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref="Markdig.Renderers.Normalize.NormalizeObjectRenderer{Markdig.Syntax.Inlines.EmphasisInline}" />
|
||||
public class EmphasisInlineRenderer : NormalizeObjectRenderer<EmphasisInline>
|
||||
{
|
||||
protected override void Write(NormalizeRenderer renderer, EmphasisInline obj)
|
||||
{
|
||||
var emphasisText = new string(obj.DelimiterChar, obj.IsDouble ? 2 : 1);
|
||||
renderer.Write(emphasisText);
|
||||
renderer.WriteChildren(obj);
|
||||
renderer.Write(emphasisText);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
using Markdig.Syntax.Inlines;
|
||||
|
||||
namespace Markdig.Renderers.Normalize.Inlines
|
||||
{
|
||||
/// <summary>
|
||||
/// A Normalize renderer for a <see cref="LineBreakInline"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref="Markdig.Renderers.Normalize.NormalizeObjectRenderer{Markdig.Syntax.Inlines.LineBreakInline}" />
|
||||
public class LineBreakInlineRenderer : NormalizeObjectRenderer<LineBreakInline>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether to render this softline break as a Normalize hardline break tag (<br />)
|
||||
/// </summary>
|
||||
public bool RenderAsHardlineBreak { get; set; }
|
||||
|
||||
protected override void Write(NormalizeRenderer renderer, LineBreakInline obj)
|
||||
{
|
||||
// TODO: handle hard
|
||||
renderer.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
using Markdig.Syntax.Inlines;
|
||||
|
||||
namespace Markdig.Renderers.Normalize.Inlines
|
||||
{
|
||||
/// <summary>
|
||||
/// A Normalize renderer for a <see cref="LinkInline"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref="Markdig.Renderers.Normalize.NormalizeObjectRenderer{Markdig.Syntax.Inlines.LinkInline}" />
|
||||
public class LinkInlineRenderer : NormalizeObjectRenderer<LinkInline>
|
||||
{
|
||||
protected override void Write(NormalizeRenderer renderer, LinkInline link)
|
||||
{
|
||||
if (link.IsImage)
|
||||
{
|
||||
renderer.Write('!');
|
||||
}
|
||||
renderer.Write('[');
|
||||
renderer.WriteChildren(link);
|
||||
renderer.Write(']');
|
||||
if (!string.IsNullOrEmpty(link.Url))
|
||||
{
|
||||
renderer.Write('(').Write(link.Url);
|
||||
|
||||
if (!string.IsNullOrEmpty(link.Title))
|
||||
{
|
||||
renderer.Write(" \"");
|
||||
renderer.Write(link.Title); // TODO: escape "
|
||||
renderer.Write("\"");
|
||||
}
|
||||
|
||||
renderer.Write(')');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
using Markdig.Syntax.Inlines;
|
||||
|
||||
namespace Markdig.Renderers.Normalize.Inlines
|
||||
{
|
||||
/// <summary>
|
||||
/// A Normalize renderer for a <see cref="LiteralInline"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref="Markdig.Renderers.Normalize.NormalizeObjectRenderer{Markdig.Syntax.Inlines.LiteralInline}" />
|
||||
public class LiteralInlineRenderer : NormalizeObjectRenderer<LiteralInline>
|
||||
{
|
||||
protected override void Write(NormalizeRenderer renderer, LiteralInline obj)
|
||||
{
|
||||
renderer.Write(ref obj.Content);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
using Markdig.Syntax.Inlines;
|
||||
|
||||
namespace Markdig.Renderers.Normalize.Inlines
|
||||
{
|
||||
/// <summary>
|
||||
/// A Normalize renderer for a <see cref="HtmlEntityInline"/>.
|
||||
/// </summary>
|
||||
public class NormalizeHtmlEntityInlineRenderer : NormalizeObjectRenderer<HtmlEntityInline>
|
||||
{
|
||||
protected override void Write(NormalizeRenderer renderer, HtmlEntityInline obj)
|
||||
{
|
||||
renderer.Write(obj.Transcoded);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
using Markdig.Syntax.Inlines;
|
||||
|
||||
namespace Markdig.Renderers.Normalize.Inlines
|
||||
{
|
||||
/// <summary>
|
||||
/// A Normalize renderer for a <see cref="HtmlInline"/>.
|
||||
/// </summary>
|
||||
public class NormalizeHtmlInlineRenderer : NormalizeObjectRenderer<HtmlInline>
|
||||
{
|
||||
protected override void Write(NormalizeRenderer renderer, HtmlInline obj)
|
||||
{
|
||||
renderer.Write(obj.Tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
73
src/Markdig/Renderers/Normalize/ListRenderer.cs
Normal file
73
src/Markdig/Renderers/Normalize/ListRenderer.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
using System.Globalization;
|
||||
using Markdig.Syntax;
|
||||
|
||||
namespace Markdig.Renderers.Normalize
|
||||
{
|
||||
/// <summary>
|
||||
/// A Normalize renderer for a <see cref="ListBlock"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref="Markdig.Renderers.Normalize.NormalizeObjectRenderer{Markdig.Syntax.ListBlock}" />
|
||||
public class ListRenderer : NormalizeObjectRenderer<ListBlock>
|
||||
{
|
||||
protected override void Write(NormalizeRenderer renderer, ListBlock listBlock)
|
||||
{
|
||||
renderer.EnsureLine();
|
||||
if (listBlock.IsOrdered)
|
||||
{
|
||||
int index = 0;
|
||||
if (listBlock.OrderedStart != null)
|
||||
{
|
||||
switch (listBlock.BulletType)
|
||||
{
|
||||
case '1':
|
||||
int.TryParse(listBlock.OrderedStart, out index);
|
||||
break;
|
||||
}
|
||||
}
|
||||
foreach (var item in listBlock)
|
||||
{
|
||||
var listItem = (ListItemBlock)item;
|
||||
renderer.EnsureLine();
|
||||
|
||||
renderer.Write(index + "");
|
||||
renderer.Write(listBlock.OrderedDelimiter);
|
||||
renderer.Write(' ');
|
||||
renderer.PushIndent(" "); // TODO: output accurate indent
|
||||
renderer.WriteChildren(listItem);
|
||||
renderer.PopIndent();
|
||||
if (!listBlock.IsLoose)
|
||||
{
|
||||
renderer.WriteLine();
|
||||
}
|
||||
|
||||
switch (listBlock.BulletType)
|
||||
{
|
||||
case '1':
|
||||
index++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var item in listBlock)
|
||||
{
|
||||
var listItem = (ListItemBlock)item;
|
||||
renderer.EnsureLine();
|
||||
renderer.Write("- ");
|
||||
renderer.PushIndent(" ");
|
||||
renderer.WriteChildren(listItem);
|
||||
renderer.PopIndent();
|
||||
if (!listBlock.IsLoose)
|
||||
{
|
||||
renderer.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
renderer.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ using System.Text;
|
||||
using Markdig.Helpers;
|
||||
using Markdig.Renderers.Html;
|
||||
using Markdig.Syntax;
|
||||
using Markdig.Renderers.Normalize.Inlines;
|
||||
|
||||
namespace Markdig.Renderers.Normalize
|
||||
{
|
||||
@@ -26,265 +27,92 @@ namespace Markdig.Renderers.Normalize
|
||||
{
|
||||
// Default block renderers
|
||||
ObjectRenderers.Add(new CodeBlockRenderer());
|
||||
//ObjectRenderers.Add(new ListRenderer());
|
||||
ObjectRenderers.Add(new ListRenderer());
|
||||
ObjectRenderers.Add(new HeadingRenderer());
|
||||
//ObjectRenderers.Add(new HtmlBlockRenderer());
|
||||
//ObjectRenderers.Add(new ParagraphRenderer());
|
||||
ObjectRenderers.Add(new HtmlBlockRenderer());
|
||||
ObjectRenderers.Add(new ParagraphRenderer());
|
||||
ObjectRenderers.Add(new QuoteBlockRenderer());
|
||||
//ObjectRenderers.Add(new ThematicBreakRenderer());
|
||||
ObjectRenderers.Add(new ThematicBreakRenderer());
|
||||
|
||||
// Default inline renderers
|
||||
//ObjectRenderers.Add(new AutolinkInlineRenderer());
|
||||
//ObjectRenderers.Add(new CodeInlineRenderer());
|
||||
//ObjectRenderers.Add(new DelimiterInlineRenderer());
|
||||
//ObjectRenderers.Add(new EmphasisInlineRenderer());
|
||||
//ObjectRenderers.Add(new LineBreakInlineRenderer());
|
||||
//ObjectRenderers.Add(new HtmlInlineRenderer());
|
||||
//ObjectRenderers.Add(new HtmlEntityInlineRenderer());
|
||||
//ObjectRenderers.Add(new LinkInlineRenderer());
|
||||
//ObjectRenderers.Add(new LiteralInlineRenderer());
|
||||
|
||||
EnableHtmlForInline = true;
|
||||
EnableHtmlEscape = true;
|
||||
ObjectRenderers.Add(new AutolinkInlineRenderer());
|
||||
ObjectRenderers.Add(new CodeInlineRenderer());
|
||||
ObjectRenderers.Add(new DelimiterInlineRenderer());
|
||||
ObjectRenderers.Add(new EmphasisInlineRenderer());
|
||||
ObjectRenderers.Add(new LineBreakInlineRenderer());
|
||||
ObjectRenderers.Add(new NormalizeHtmlInlineRenderer());
|
||||
ObjectRenderers.Add(new NormalizeHtmlEntityInlineRenderer());
|
||||
ObjectRenderers.Add(new LinkInlineRenderer());
|
||||
ObjectRenderers.Add(new LiteralInlineRenderer());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether to ouput HTML tags when rendering. See remarks.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is used by some renderers to disable HTML tags when rendering some inlines (for image links).
|
||||
/// </remarks>
|
||||
public bool EnableHtmlForInline { get; set; }
|
||||
///// <summary>
|
||||
///// Writes the attached <see cref="HtmlAttributes"/> on the specified <see cref="MarkdownObject"/>.
|
||||
///// </summary>
|
||||
///// <param name="obj">The object.</param>
|
||||
///// <returns></returns>
|
||||
//public NormalizeRenderer WriteAttributes(MarkdownObject obj)
|
||||
//{
|
||||
// if (obj == null) throw new ArgumentNullException(nameof(obj));
|
||||
// return WriteAttributes(obj.TryGetAttributes());
|
||||
//}
|
||||
|
||||
public bool EnableHtmlEscape { get; set; }
|
||||
///// <summary>
|
||||
///// Writes the specified <see cref="HtmlAttributes"/>.
|
||||
///// </summary>
|
||||
///// <param name="attributes">The attributes to render.</param>
|
||||
///// <returns>This instance</returns>
|
||||
//public NormalizeRenderer WriteAttributes(HtmlAttributes attributes)
|
||||
//{
|
||||
// if (attributes == null)
|
||||
// {
|
||||
// return this;
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether to use implicit paragraph (optional <p>)
|
||||
/// </summary>
|
||||
public bool ImplicitParagraph { get; set; }
|
||||
// if (attributes.Id != null)
|
||||
// {
|
||||
// Write(" id=\"").WriteEscape(attributes.Id).Write("\"");
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Writes the content escaped for HTML.
|
||||
/// </summary>
|
||||
/// <param name="content">The content.</param>
|
||||
/// <returns>This instance</returns>
|
||||
[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
|
||||
public NormalizeRenderer WriteEscape(string content)
|
||||
{
|
||||
if (string.IsNullOrEmpty(content))
|
||||
return this;
|
||||
// if (attributes.Classes != null && attributes.Classes.Count > 0)
|
||||
// {
|
||||
// Write(" class=\"");
|
||||
// for (int i = 0; i < attributes.Classes.Count; i++)
|
||||
// {
|
||||
// var cssClass = attributes.Classes[i];
|
||||
// if (i > 0)
|
||||
// {
|
||||
// Write(" ");
|
||||
// }
|
||||
// WriteEscape(cssClass);
|
||||
// }
|
||||
// Write("\"");
|
||||
// }
|
||||
|
||||
WriteEscape(content, 0, content.Length);
|
||||
return this;
|
||||
}
|
||||
// if (attributes.Properties != null && attributes.Properties.Count > 0)
|
||||
// {
|
||||
// foreach (var property in attributes.Properties)
|
||||
// {
|
||||
// Write(" ").Write(property.Key);
|
||||
// if (property.Value != null)
|
||||
// {
|
||||
// Write("=").Write("\"");
|
||||
// WriteEscape(property.Value);
|
||||
// Write("\"");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Writes the content escaped for HTML.
|
||||
/// </summary>
|
||||
/// <param name="slice">The slice.</param>
|
||||
/// <returns>This instance</returns>
|
||||
[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
|
||||
public NormalizeRenderer WriteEscape(ref StringSlice slice)
|
||||
{
|
||||
if (slice.Start > slice.End)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
return WriteEscape(slice.Text, slice.Start, slice.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the content escaped for HTML.
|
||||
/// </summary>
|
||||
/// <param name="slice">The slice.</param>
|
||||
/// <returns>This instance</returns>
|
||||
[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
|
||||
public NormalizeRenderer WriteEscape(StringSlice slice)
|
||||
{
|
||||
return WriteEscape(ref slice);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the content escaped for HTML.
|
||||
/// </summary>
|
||||
/// <param name="content">The content.</param>
|
||||
/// <param name="offset">The offset.</param>
|
||||
/// <param name="length">The length.</param>
|
||||
/// <returns>This instance</returns>
|
||||
public NormalizeRenderer WriteEscape(string content, int offset, int length)
|
||||
{
|
||||
if (string.IsNullOrEmpty(content) || length == 0)
|
||||
return this;
|
||||
|
||||
var end = offset + length;
|
||||
int previousOffset = offset;
|
||||
for (;offset < end; offset++)
|
||||
{
|
||||
switch (content[offset])
|
||||
{
|
||||
case '<':
|
||||
Write(content, previousOffset, offset - previousOffset);
|
||||
if (EnableHtmlEscape)
|
||||
{
|
||||
Write("<");
|
||||
}
|
||||
previousOffset = offset + 1;
|
||||
break;
|
||||
case '>':
|
||||
Write(content, previousOffset, offset - previousOffset);
|
||||
if (EnableHtmlEscape)
|
||||
{
|
||||
Write(">");
|
||||
}
|
||||
previousOffset = offset + 1;
|
||||
break;
|
||||
case '&':
|
||||
Write(content, previousOffset, offset - previousOffset);
|
||||
if (EnableHtmlEscape)
|
||||
{
|
||||
Write("&");
|
||||
}
|
||||
previousOffset = offset + 1;
|
||||
break;
|
||||
case '"':
|
||||
Write(content, previousOffset, offset - previousOffset);
|
||||
if (EnableHtmlEscape)
|
||||
{
|
||||
Write(""");
|
||||
}
|
||||
previousOffset = offset + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Write(content, previousOffset, end - previousOffset);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the URL escaped for HTML.
|
||||
/// </summary>
|
||||
/// <param name="content">The content.</param>
|
||||
/// <returns>This instance</returns>
|
||||
public NormalizeRenderer WriteEscapeUrl(string content)
|
||||
{
|
||||
if (content == null)
|
||||
return this;
|
||||
|
||||
int previousPosition = 0;
|
||||
int length = content.Length;
|
||||
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
var c = content[i];
|
||||
|
||||
if (c < 128)
|
||||
{
|
||||
var escape = HtmlHelper.EscapeUrlCharacter(c);
|
||||
if (escape != null)
|
||||
{
|
||||
Write(content, previousPosition, i - previousPosition);
|
||||
previousPosition = i + 1;
|
||||
Write(escape);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Write(content, previousPosition, i - previousPosition);
|
||||
previousPosition = i + 1;
|
||||
|
||||
byte[] bytes;
|
||||
if (c >= '\ud800' && c <= '\udfff' && previousPosition < length)
|
||||
{
|
||||
bytes = Encoding.UTF8.GetBytes(new[] { c, content[previousPosition] });
|
||||
// Skip next char as it is decoded above
|
||||
i++;
|
||||
previousPosition = i + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
bytes = Encoding.UTF8.GetBytes(new[] { c });
|
||||
}
|
||||
|
||||
for (var j = 0; j < bytes.Length; j++)
|
||||
{
|
||||
Write($"%{bytes[j]:X2}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Write(content, previousPosition, length - previousPosition);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the attached <see cref="HtmlAttributes"/> on the specified <see cref="MarkdownObject"/>.
|
||||
/// </summary>
|
||||
/// <param name="obj">The object.</param>
|
||||
/// <returns></returns>
|
||||
public NormalizeRenderer WriteAttributes(MarkdownObject obj)
|
||||
{
|
||||
if (obj == null) throw new ArgumentNullException(nameof(obj));
|
||||
return WriteAttributes(obj.TryGetAttributes());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified <see cref="HtmlAttributes"/>.
|
||||
/// </summary>
|
||||
/// <param name="attributes">The attributes to render.</param>
|
||||
/// <returns>This instance</returns>
|
||||
public NormalizeRenderer WriteAttributes(HtmlAttributes attributes)
|
||||
{
|
||||
if (attributes == null)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
if (attributes.Id != null)
|
||||
{
|
||||
Write(" id=\"").WriteEscape(attributes.Id).Write("\"");
|
||||
}
|
||||
|
||||
if (attributes.Classes != null && attributes.Classes.Count > 0)
|
||||
{
|
||||
Write(" class=\"");
|
||||
for (int i = 0; i < attributes.Classes.Count; i++)
|
||||
{
|
||||
var cssClass = attributes.Classes[i];
|
||||
if (i > 0)
|
||||
{
|
||||
Write(" ");
|
||||
}
|
||||
WriteEscape(cssClass);
|
||||
}
|
||||
Write("\"");
|
||||
}
|
||||
|
||||
if (attributes.Properties != null && attributes.Properties.Count > 0)
|
||||
{
|
||||
foreach (var property in attributes.Properties)
|
||||
{
|
||||
Write(" ").Write(property.Key);
|
||||
if (property.Value != null)
|
||||
{
|
||||
Write("=").Write("\"");
|
||||
WriteEscape(property.Value);
|
||||
Write("\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
// return this;
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the lines of a <see cref="LeafBlock"/>
|
||||
/// </summary>
|
||||
/// <param name="leafBlock">The leaf block.</param>
|
||||
/// <param name="writeEndOfLines">if set to <c>true</c> write end of lines.</param>
|
||||
/// <param name="escape">if set to <c>true</c> escape the content for HTML</param>
|
||||
/// <returns>This instance</returns>
|
||||
public NormalizeRenderer WriteLeafRawLines(LeafBlock leafBlock, bool writeEndOfLines, bool escape, bool indent = false)
|
||||
public NormalizeRenderer WriteLeafRawLines(LeafBlock leafBlock, bool writeEndOfLines, bool indent = false)
|
||||
{
|
||||
if (leafBlock == null) throw new ArgumentNullException(nameof(leafBlock));
|
||||
if (leafBlock.Lines.Lines != null)
|
||||
@@ -303,14 +131,8 @@ namespace Markdig.Renderers.Normalize
|
||||
Write(" ");
|
||||
}
|
||||
|
||||
if (escape)
|
||||
{
|
||||
WriteEscape(ref slices[i].Slice);
|
||||
}
|
||||
else
|
||||
{
|
||||
Write(ref slices[i].Slice);
|
||||
}
|
||||
Write(ref slices[i].Slice);
|
||||
|
||||
if (writeEndOfLines)
|
||||
{
|
||||
WriteLine();
|
||||
|
||||
21
src/Markdig/Renderers/Normalize/ParagraphRenderer.cs
Normal file
21
src/Markdig/Renderers/Normalize/ParagraphRenderer.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
using Markdig.Syntax;
|
||||
|
||||
namespace Markdig.Renderers.Normalize
|
||||
{
|
||||
/// <summary>
|
||||
/// A Normalize renderer for a <see cref="ParagraphBlock"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref="Markdig.Renderers.Normalize.NormalizeObjectRenderer{Markdig.Syntax.ParagraphBlock}" />
|
||||
public class ParagraphRenderer : NormalizeObjectRenderer<ParagraphBlock>
|
||||
{
|
||||
protected override void Write(NormalizeRenderer renderer, ParagraphBlock obj)
|
||||
{
|
||||
renderer.EnsureLine();
|
||||
renderer.WriteLeafInline(obj);
|
||||
renderer.WriteLine().WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
19
src/Markdig/Renderers/Normalize/ThematicBreakRenderer.cs
Normal file
19
src/Markdig/Renderers/Normalize/ThematicBreakRenderer.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
using Markdig.Syntax;
|
||||
|
||||
namespace Markdig.Renderers.Normalize
|
||||
{
|
||||
/// <summary>
|
||||
/// A Normalize renderer for a <see cref="ThematicBreakBlock"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref="Markdig.Renderers.Normalize.NormalizeObjectRenderer{Markdig.Syntax.ThematicBreakBlock}" />
|
||||
public class ThematicBreakRenderer : NormalizeObjectRenderer<ThematicBreakBlock>
|
||||
{
|
||||
protected override void Write(NormalizeRenderer renderer, ThematicBreakBlock obj)
|
||||
{
|
||||
renderer.WriteLine(new string(obj.ThematicChar, obj.ThematicCharCount));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,5 +17,9 @@ namespace Markdig.Syntax
|
||||
public ThematicBreakBlock(BlockParser parser) : base(parser)
|
||||
{
|
||||
}
|
||||
|
||||
public char ThematicChar { get; set; }
|
||||
|
||||
public int ThematicCharCount { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user