-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMarkdownTransformRenderer.cs
77 lines (70 loc) · 2.65 KB
/
MarkdownTransformRenderer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Licensed to DocFX Companion Tools and contributors under one or more agreements.
// DocFX Companion Tools and contributors licenses this file to you under the MIT license.
using Markdig.Renderers;
using Markdig.Syntax;
namespace DocFXLanguageGenerator
{
/// <summary>
/// A Text Renderer MArkdown class used to replace the original text with the translated text.
/// </summary>
internal class MarkdownTransformRenderer : TextRendererBase<MarkdownTransformRenderer>
{
/// <summary>
/// Initializes a new instance of the <see cref="MarkdownTransformRenderer"/> class.
/// </summary>
/// <param name="writer">The text writer.</param>
/// <param name="originalMarkdown">The original Markdown string.</param>
public MarkdownTransformRenderer(TextWriter writer, string originalMarkdown)
: base(writer)
{
this.OriginalMarkdown = originalMarkdown;
this.ObjectRenderers.Add(new ContainerBlockRenderer());
this.ObjectRenderers.Add(new LeafBlockRenderer());
}
/// <summary>
/// Gets or sets the original Markdown.
/// </summary>
public string OriginalMarkdown { get; internal set; }
/// <summary>
/// Gets or sets where we are in terms of writting.
/// </summary>
public int LastWrittenIndex { get; set; }
/// <summary>
/// Take the next block.
/// </summary>
/// <param name="length">The length.</param>
/// <returns>The block text.</returns>
public string TakeNext(int length)
{
if (length <= 0)
{
return null;
}
var result = this.OriginalMarkdown.Substring(this.LastWrittenIndex, length);
this.LastWrittenIndex += length;
return result;
}
/// <summary>
/// Container Block Renderer.
/// </summary>
internal class ContainerBlockRenderer : MarkdownObjectRenderer<MarkdownTransformRenderer, ContainerBlock>
{
/// <inheritdoc/>
protected override void Write(MarkdownTransformRenderer renderer, ContainerBlock obj)
{
renderer.WriteChildren(obj);
}
}
/// <summary>
/// Leaf Block Renderer.
/// </summary>
internal class LeafBlockRenderer : MarkdownObjectRenderer<MarkdownTransformRenderer, LeafBlock>
{
/// <inheritdoc/>
protected override void Write(MarkdownTransformRenderer renderer, LeafBlock obj)
{
renderer.WriteLeafInline(obj);
}
}
}
}