forked from Knagis/CommonMark.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonMarkConverter.cs
326 lines (294 loc) · 14.3 KB
/
CommonMarkConverter.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
using CommonMark.Formatters;
using CommonMark.Parser;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
namespace CommonMark
{
/// <summary>
/// Contains methods for parsing and formatting CommonMark data.
/// </summary>
public static class CommonMarkConverter
{
private static Version _version = new Version(0, 0);
#if NETCore
/// <summary>
/// Gets the CommonMark package version number.
/// </summary>
public static Version Version
{
get
{
if (_version.Major != 0 || _version.Minor != 0)
return _version;
var assembly = System.Reflection.IntrospectionExtensions.GetTypeInfo(typeof(CommonMarkConverter)).Assembly;
_version = new System.Reflection.AssemblyName(assembly.FullName).Version;
return _version;
}
}
#endif
#if !NETCore
/// <summary>
/// Gets the CommonMark package version number.
/// Note that this might differ from the actual assembly version which is updated less often to
/// reduce problems when upgrading the nuget package.
/// </summary>
public static Version Version
{
get
{
if (_version.Major != 0 || _version.Minor != 0)
return _version;
var assembly = typeof(CommonMarkConverter).Assembly;
// System.Xml is not available so resort to string parsing.
using (var stream = assembly.GetManifestResourceStream("CommonMark.Properties.CommonMark.NET.nuspec"))
using (var reader = new System.IO.StreamReader(stream, Encoding.UTF8))
{
string line;
while ((line = reader.ReadLine()) != null)
{
var i = line.IndexOf("<version>", StringComparison.Ordinal);
if (i == -1)
continue;
i += 9;
return _version = new Version(line.Substring(i, line.IndexOf("</version>", StringComparison.Ordinal) - i));
}
}
return _version;
}
}
#endif
/// <summary>
/// Gets the CommonMark assembly version number. Note that might differ from the actual release version
/// since the assembly version is not always incremented to reduce possible reference errors when updating.
/// </summary>
[Obsolete("Use Version property instead.", false)]
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static Version AssemblyVersion
{
get
{
#if NETCore
var assembly = System.Reflection.IntrospectionExtensions.GetTypeInfo(typeof(CommonMarkConverter)).Assembly;
#else
var assembly = typeof(CommonMarkConverter).Assembly;
#endif
var aName = new System.Reflection.AssemblyName(assembly.FullName);
return aName.Version;
}
}
/// <summary>
/// Performs the first stage of the conversion - parses block elements from the source and created the syntax tree.
/// </summary>
/// <param name="source">The reader that contains the source data.</param>
/// <param name="settings">The object containing settings for the parsing process.</param>
/// <returns>The block element that represents the document.</returns>
/// <exception cref="ArgumentNullException">when <paramref name="source"/> is <c>null</c></exception>
/// <exception cref="CommonMarkException">when errors occur during block parsing.</exception>
/// <exception cref="IOException">when error occur while reading the data.</exception>
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
public static Syntax.Block ProcessStage1(TextReader source, CommonMarkSettings settings = null)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (settings == null)
settings = CommonMarkSettings.Default;
var cur = Syntax.Block.CreateDocument();
var doc = cur;
var line = new LineInfo(settings.TrackSourcePosition);
try
{
var reader = new TabTextReader(source);
reader.ReadLine(line);
while (line.Line != null)
{
BlockMethods.IncorporateLine(line, ref cur);
reader.ReadLine(line);
}
}
catch(IOException)
{
throw;
}
catch(CommonMarkException)
{
throw;
}
catch(Exception ex)
{
throw new CommonMarkException("An error occurred while parsing line " + line.ToString(), cur, ex);
}
try
{
do
{
BlockMethods.Finalize(cur, line);
cur = cur.Parent;
} while (cur != null);
}
catch (CommonMarkException)
{
throw;
}
catch (Exception ex)
{
throw new CommonMarkException("An error occurred while finalizing open containers.", cur, ex);
}
return doc;
}
/// <summary>
/// Performs the second stage of the conversion - parses block element contents into inline elements.
/// </summary>
/// <param name="document">The top level document element.</param>
/// <param name="settings">The object containing settings for the parsing process.</param>
/// <exception cref="ArgumentException">when <paramref name="document"/> does not represent a top level document.</exception>
/// <exception cref="ArgumentNullException">when <paramref name="document"/> is <c>null</c></exception>
/// <exception cref="CommonMarkException">when errors occur during inline parsing.</exception>
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
public static void ProcessStage2(Syntax.Block document, CommonMarkSettings settings = null)
{
if (document == null)
throw new ArgumentNullException(nameof(document));
if (document.Tag != Syntax.BlockTag.Document)
throw new ArgumentException("The block element passed to this method must represent a top level document.", nameof(document));
if (settings == null)
settings = CommonMarkSettings.Default;
try
{
BlockMethods.ProcessInlines(document, document.ReferenceMap, settings);
}
catch(CommonMarkException)
{
throw;
}
catch(Exception ex)
{
throw new CommonMarkException("An error occurred during inline parsing.", ex);
}
}
/// <summary>
/// Performs the last stage of the conversion - converts the syntax tree to HTML representation.
/// </summary>
/// <param name="document">The top level document element.</param>
/// <param name="target">The target text writer where the result will be written to.</param>
/// <param name="settings">The object containing settings for the formatting process.</param>
/// <exception cref="ArgumentException">when <paramref name="document"/> does not represent a top level document.</exception>
/// <exception cref="ArgumentNullException">when <paramref name="document"/> or <paramref name="target"/> is <c>null</c></exception>
/// <exception cref="CommonMarkException">when errors occur during formatting.</exception>
/// <exception cref="IOException">when error occur while writing the data to the target.</exception>
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
public static void ProcessStage3(Syntax.Block document, TextWriter target, CommonMarkSettings settings = null)
{
if (document == null)
throw new ArgumentNullException(nameof(document));
if (target == null)
throw new ArgumentNullException(nameof(target));
if (document.Tag != Syntax.BlockTag.Document)
throw new ArgumentException("The block element passed to this method must represent a top level document.", nameof(document));
if (settings == null)
settings = CommonMarkSettings.Default;
try
{
switch (settings.OutputFormat)
{
case OutputFormat.Html:
HtmlFormatterSlim.BlocksToHtml(target, document, settings);
break;
case OutputFormat.SyntaxTree:
Printer.PrintBlocks(target, document, settings);
break;
case OutputFormat.CustomDelegate:
if (settings.OutputDelegate == null)
throw new CommonMarkException("If `settings.OutputFormat` is set to `CustomDelegate`, the `settings.OutputDelegate` property must be populated.");
settings.OutputDelegate(document, target, settings);
break;
default:
throw new CommonMarkException("Unsupported value '" + settings.OutputFormat + "' in `settings.OutputFormat`.");
}
}
catch (CommonMarkException)
{
throw;
}
catch(IOException)
{
throw;
}
catch(Exception ex)
{
throw new CommonMarkException("An error occurred during formatting of the document.", ex);
}
}
/// <summary>
/// Parses the given source data and returns the document syntax tree. Use <see cref="ProcessStage3"/> to
/// convert the document to HTML using the built-in converter.
/// </summary>
/// <param name="source">The reader that contains the source data.</param>
/// <param name="settings">The object containing settings for the parsing and formatting process.</param>
/// <exception cref="ArgumentNullException">when <paramref name="source"/> is <c>null</c></exception>
/// <exception cref="CommonMarkException">when errors occur during parsing.</exception>
/// <exception cref="IOException">when error occur while reading or writing the data.</exception>
public static Syntax.Block Parse(TextReader source, CommonMarkSettings settings = null)
{
if (settings == null)
settings = CommonMarkSettings.Default;
var document = ProcessStage1(source, settings);
ProcessStage2(document, settings);
return document;
}
/// <summary>
/// Parses the given source data and returns the document syntax tree. Use <see cref="ProcessStage3"/> to
/// convert the document to HTML using the built-in converter.
/// </summary>
/// <param name="source">The source data.</param>
/// <param name="settings">The object containing settings for the parsing and formatting process.</param>
/// <exception cref="ArgumentNullException">when <paramref name="source"/> is <c>null</c></exception>
/// <exception cref="CommonMarkException">when errors occur during parsing.</exception>
/// <exception cref="IOException">when error occur while reading or writing the data.</exception>
public static Syntax.Block Parse(string source, CommonMarkSettings settings = null)
{
if (source == null)
return null;
using (var reader = new System.IO.StringReader(source))
return Parse(reader, settings);
}
/// <summary>
/// Converts the given source data and writes the result directly to the target.
/// </summary>
/// <param name="source">The reader that contains the source data.</param>
/// <param name="target">The target text writer where the result will be written to.</param>
/// <param name="settings">The object containing settings for the parsing and formatting process.</param>
/// <exception cref="ArgumentNullException">when <paramref name="source"/> or <paramref name="target"/> is <c>null</c></exception>
/// <exception cref="CommonMarkException">when errors occur during parsing or formatting.</exception>
/// <exception cref="IOException">when error occur while reading or writing the data.</exception>
public static void Convert(TextReader source, TextWriter target, CommonMarkSettings settings = null)
{
if (settings == null)
settings = CommonMarkSettings.Default;
var document = ProcessStage1(source, settings);
ProcessStage2(document, settings);
ProcessStage3(document, target, settings);
}
/// <summary>
/// Converts the given source data and returns the result as a string.
/// </summary>
/// <param name="source">The source data.</param>
/// <param name="settings">The object containing settings for the parsing and formatting process.</param>
/// <exception cref="CommonMarkException">when errors occur during parsing or formatting.</exception>
/// <returns>The converted data.</returns>
public static string Convert(string source, CommonMarkSettings settings = null)
{
if (source == null)
return null;
using (var reader = new System.IO.StringReader(source))
using (var writer = new System.IO.StringWriter(System.Globalization.CultureInfo.CurrentCulture))
{
Convert(reader, writer, settings);
return writer.ToString();
}
}
}
}