-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtensibleHtmlFormatter.cs
302 lines (250 loc) · 11.3 KB
/
ExtensibleHtmlFormatter.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Collections.Generic;
using System.IO;
using CommonMark;
using CommonMark.Formatters;
using CommonMark.Syntax;
namespace BlazorSite.Markdown
{
public class ExtensibleHtmlFormatter : HtmlFormatter
{
#region InlineFormatter internal class
private abstract class InlineFormatter
{
public Func<Inline, bool> CanFormat { get; }
public InlineFormatter(Func<Inline, bool> canFormat)
{
CanFormat = canFormat;
}
public class SimpleInlineFormatter : InlineFormatter
{
public Func<Inline, string> Format { get; }
public SimpleInlineFormatter(Func<Inline, bool> canFormat, Func<Inline, string> format) : base(canFormat)
{
Format = format;
}
}
public class OpenCloseInlineFormatter : InlineFormatter
{
public Func<Inline, string> FormatOpening { get; }
public Func<Inline, string> FormatClosing { get; }
public OpenCloseInlineFormatter(Func<Inline, bool> canFormat, Func<Inline, string> formatOpening, Func<Inline, string> formatClosing) : base(canFormat)
{
FormatOpening = formatOpening;
FormatClosing = formatClosing;
}
}
public static SimpleInlineFormatter Create(Func<Inline, string> formatter)
{
return new SimpleInlineFormatter
(
canFormat: _ => true,
format: formatter
);
}
public static SimpleInlineFormatter Create(Func<Inline, bool> canFormat, Func<Inline, string> formatter)
{
return new SimpleInlineFormatter
(
canFormat: canFormat,
format: formatter
);
}
public static OpenCloseInlineFormatter Create(Func<Inline, string> openingFormatter, Func<Inline, string> closingFormatter)
{
return new OpenCloseInlineFormatter
(
canFormat: _ => true,
formatOpening: openingFormatter,
formatClosing: closingFormatter
);
}
public static OpenCloseInlineFormatter Create(Func<Inline, bool> canFormat, Func<Inline, string> openingFormatter, Func<Inline, string> closingFormatter)
{
return new OpenCloseInlineFormatter
(
canFormat: canFormat,
formatOpening: openingFormatter,
formatClosing: closingFormatter
);
}
}
#endregion
#region BlockFormatter internal class
public abstract class BlockFormatter
{
public Func<Block, bool> CanFormat { get; }
public BlockFormatter(Func<Block, bool> canFormat)
{
CanFormat = canFormat;
}
public class SimpleBlockFormatter : BlockFormatter
{
public Func<Block, string> Format { get; }
public SimpleBlockFormatter(Func<Block, bool> canFormat, Func<Block, string> format) : base(canFormat)
{
Format = format;
}
}
public class OpenCloseBlockFormatter : BlockFormatter
{
public Func<Block, string> FormatOpening { get; }
public Func<Block, string> FormatClosing { get; }
public OpenCloseBlockFormatter(Func<Block, bool> canFormat, Func<Block, string> formatOpening, Func<Block, string> formatClosing) : base(canFormat)
{
FormatOpening = formatOpening;
FormatClosing = formatClosing;
}
}
public static SimpleBlockFormatter Create(Func<Block, string> formatter)
{
return new SimpleBlockFormatter
(
canFormat: _ => true,
format: formatter
);
}
public static SimpleBlockFormatter Create(Func<Block, bool> canFormat, Func<Block, string> formatter)
{
return new SimpleBlockFormatter
(
canFormat: canFormat,
format: formatter
);
}
public static OpenCloseBlockFormatter Create(Func<Block, string> openingFormatter, Func<Block, string> closingFormatter)
{
return new OpenCloseBlockFormatter
(
canFormat: _ => true,
formatOpening: openingFormatter,
formatClosing: closingFormatter
);
}
public static OpenCloseBlockFormatter Create(Func<Block, bool> canFormat, Func<Block, string> openingFormatter, Func<Block, string> closingFormatter)
{
return new OpenCloseBlockFormatter
(
canFormat: canFormat,
formatOpening: openingFormatter,
formatClosing: closingFormatter
);
}
}
#endregion
private readonly Dictionary<InlineTag, InlineFormatter> inlineFormatters = new Dictionary<InlineTag, InlineFormatter>();
private readonly Dictionary<BlockTag, BlockFormatter> blockFormatters = new Dictionary<BlockTag, BlockFormatter>();
public ExtensibleHtmlFormatter(TextWriter target, CommonMarkSettings settings)
: base(target, settings)
{
}
#region Inline Writer
protected override void WriteInline(Inline inline, bool isOpening, bool isClosing, out bool ignoreChildNodes)
{
if (inlineFormatters.TryGetValue(inline.Tag, out var formatter) && formatter.CanFormat(inline))
{
switch (formatter)
{
case InlineFormatter.SimpleInlineFormatter simpleFormatter:
this.WriteInline(simpleFormatter, inline, isOpening, isClosing, out ignoreChildNodes);
return;
case InlineFormatter.OpenCloseInlineFormatter openCloseFormatter:
this.WriteInline(openCloseFormatter, inline, isOpening, isClosing, out ignoreChildNodes);
return;
}
}
base.WriteInline(inline, isOpening, isClosing, out ignoreChildNodes);
}
private void WriteInline(InlineFormatter.SimpleInlineFormatter formatter, Inline inline, bool isOpening, bool isClosing, out bool ignoreChildNodes)
{
ignoreChildNodes = true;
if (isOpening)
this.Write(formatter.Format(inline));
}
private void WriteInline(InlineFormatter.OpenCloseInlineFormatter formatter, Inline inline, bool isOpening, bool isClosing, out bool ignoreChildNodes)
{
ignoreChildNodes = false;
if (isOpening)
this.Write(formatter.FormatOpening(inline));
if (isClosing)
this.Write(formatter.FormatClosing(inline));
}
#endregion
#region Block Writer
protected override void WriteBlock(Block block, bool isOpening, bool isClosing, out bool ignoreChildNodes)
{
if (blockFormatters.TryGetValue(block.Tag, out var formatter) && formatter.CanFormat(block))
{
switch (formatter)
{
case BlockFormatter.SimpleBlockFormatter simpleFormatter:
this.WriteBlock(simpleFormatter, block, isOpening, isClosing, out ignoreChildNodes);
return;
case BlockFormatter.OpenCloseBlockFormatter openCloseFormatter:
this.WriteBlock(openCloseFormatter, block, isOpening, isClosing, out ignoreChildNodes);
return;
}
}
base.WriteBlock(block, isOpening, isClosing, out ignoreChildNodes);
}
private void WriteBlock(BlockFormatter.SimpleBlockFormatter formatter, Block block, bool isOpening, bool isClosing, out bool ignoreChildNodes)
{
ignoreChildNodes = true;
if (isOpening)
this.Write(formatter.Format(block));
}
private void WriteBlock(BlockFormatter.OpenCloseBlockFormatter formatter, Block block, bool isOpening, bool isClosing, out bool ignoreChildNodes)
{
ignoreChildNodes = false;
if (isOpening)
this.Write(formatter.FormatOpening(block));
if (isClosing)
this.Write(formatter.FormatClosing(block));
}
#endregion
#region Inline Formatter Builder Methods
public ExtensibleHtmlFormatter WithFormatter(InlineTag inlineTag, Func<Inline, string> formatter)
{
this.inlineFormatters.Add(inlineTag, InlineFormatter.Create(formatter));
return this;
}
public ExtensibleHtmlFormatter WithFormatter(InlineTag inlineTag, Func<Inline, bool> canFormat, Func<Inline, string> formatter)
{
this.inlineFormatters.Add(inlineTag, InlineFormatter.Create(canFormat, formatter));
return this;
}
public ExtensibleHtmlFormatter WithFormatter(InlineTag inlineTag, Func<Inline, string> openingFormatter, Func<Inline, string> closingFormatter)
{
this.inlineFormatters.Add(inlineTag, InlineFormatter.Create(openingFormatter, closingFormatter));
return this;
}
public ExtensibleHtmlFormatter WithFormatter(InlineTag inlineTag, Func<Inline, bool> canFormat, Func<Inline, string> openingFormatter, Func<Inline, string> closingFormatter)
{
this.inlineFormatters.Add(inlineTag, InlineFormatter.Create(canFormat, openingFormatter, closingFormatter));
return this;
}
#endregion
#region Block Formatter Builder Methods
public ExtensibleHtmlFormatter WithFormatter(BlockTag blockTag, Func<Block, string> formatter)
{
this.blockFormatters.Add(blockTag, BlockFormatter.Create(formatter));
return this;
}
public ExtensibleHtmlFormatter WithFormatter(BlockTag blockTag, Func<Block, bool> canFormat, Func<Block, string> formatter)
{
this.blockFormatters.Add(blockTag, BlockFormatter.Create(canFormat, formatter));
return this;
}
public ExtensibleHtmlFormatter WithFormatter(BlockTag blockTag, Func<Block, string> openingFormatter, Func<Block, string> closingFormatter)
{
this.blockFormatters.Add(blockTag, BlockFormatter.Create(openingFormatter, closingFormatter));
return this;
}
public ExtensibleHtmlFormatter WithFormatter(BlockTag blockTag, Func<Block, bool> canFormat, Func<Block, string> openingFormatter, Func<Block, string> closingFormatter)
{
this.blockFormatters.Add(blockTag, BlockFormatter.Create(canFormat, openingFormatter, closingFormatter));
return this;
}
#endregion
}
}