This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
WpfRenderer.cs
190 lines (166 loc) · 5.83 KB
/
WpfRenderer.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) Nicolas Musset. All rights reserved.
// This file is licensed under the MIT license.
// See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Markup;
using Markdig.Helpers;
using Markdig.Renderers.Wpf;
using Markdig.Renderers.Wpf.Extensions;
using Markdig.Renderers.Wpf.Inlines;
using Markdig.Syntax;
using Markdig.Wpf;
using Block = System.Windows.Documents.Block;
namespace Markdig.Renderers
{
/// <summary>
/// WPF renderer for a Markdown <see cref="MarkdownDocument"/> object.
/// </summary>
/// <seealso cref="RendererBase" />
public class WpfRenderer : RendererBase
{
private readonly Stack<IAddChild> stack = new Stack<IAddChild>();
private char[] buffer;
public WpfRenderer()
{
buffer = new char[1024];
}
public WpfRenderer(FlowDocument document)
{
buffer = new char[1024];
LoadDocument(document);
}
public virtual void LoadDocument(FlowDocument document)
{
Document = document ?? throw new ArgumentNullException(nameof(document));
document.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.DocumentStyleKey);
stack.Push(document);
LoadRenderers();
}
public FlowDocument? Document { get; protected set; }
/// <inheritdoc/>
public override object? Render(MarkdownObject markdownObject)
{
Write(markdownObject);
return Document;
}
/// <summary>
/// Writes the inlines of a leaf inline.
/// </summary>
/// <param name="leafBlock">The leaf block.</param>
/// <returns>This instance</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteLeafInline(LeafBlock leafBlock)
{
if (leafBlock == null) throw new ArgumentNullException(nameof(leafBlock));
var inline = (Syntax.Inlines.Inline)leafBlock.Inline;
while (inline != null)
{
Write(inline);
inline = inline.NextSibling;
}
}
/// <summary>
/// Writes the lines of a <see cref="LeafBlock"/>
/// </summary>
/// <param name="leafBlock">The leaf block.</param>
public void WriteLeafRawLines(LeafBlock leafBlock)
{
if (leafBlock == null) throw new ArgumentNullException(nameof(leafBlock));
if (leafBlock.Lines.Lines != null)
{
var lines = leafBlock.Lines;
var slices = lines.Lines;
for (var i = 0; i < lines.Count; i++)
{
if (i != 0)
WriteInline(new LineBreak());
WriteText(ref slices[i].Slice);
}
}
}
public void Push(IAddChild o)
{
stack.Push(o);
}
public void Pop()
{
var popped = stack.Pop();
stack.Peek().AddChild(popped);
}
public void WriteBlock(Block block)
{
stack.Peek().AddChild(block);
}
public void WriteInline(Inline inline)
{
AddInline(stack.Peek(), inline);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteText(ref StringSlice slice)
{
if (slice.Start > slice.End)
return;
WriteText(slice.Text, slice.Start, slice.Length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteText(string? text)
{
WriteInline(new Run(text));
}
public void WriteText(string? text, int offset, int length)
{
if (text == null)
return;
if (offset == 0 && text.Length == length)
{
WriteText(text);
}
else
{
if (length > buffer.Length)
{
buffer = text.ToCharArray();
WriteText(new string(buffer, offset, length));
}
else
{
text.CopyTo(offset, buffer, 0, length);
WriteText(new string(buffer, 0, length));
}
}
}
/// <summary>
/// Loads the renderer used for render WPF
/// </summary>
protected virtual void LoadRenderers()
{
// Default block renderers
ObjectRenderers.Add(new CodeBlockRenderer());
ObjectRenderers.Add(new ListRenderer());
ObjectRenderers.Add(new HeadingRenderer());
ObjectRenderers.Add(new ParagraphRenderer());
ObjectRenderers.Add(new QuoteBlockRenderer());
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 HtmlEntityInlineRenderer());
ObjectRenderers.Add(new LineBreakInlineRenderer());
ObjectRenderers.Add(new LinkInlineRenderer());
ObjectRenderers.Add(new LiteralInlineRenderer());
// Extension renderers
ObjectRenderers.Add(new TableRenderer());
ObjectRenderers.Add(new TaskListRenderer());
}
private static void AddInline(IAddChild parent, Inline inline)
{
parent.AddChild(inline);
}
}
}