forked from Knagis/CommonMark.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonMarkSettings.cs
170 lines (149 loc) · 6.66 KB
/
CommonMarkSettings.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
using System;
using System.Collections.Generic;
using System.Text;
namespace CommonMark
{
/// <summary>
/// Class used to configure the behavior of <see cref="CommonMarkConverter"/>.
/// </summary>
/// <remarks>This class is not thread-safe so any changes to a instance that is reused (for example, the
/// <see cref="CommonMarkSettings.Default"/>) has to be updated while it is not in use otherwise the
/// behaviour is undefined.</remarks>
public sealed class CommonMarkSettings
{
/// <summary>Initializes a new instance of the <see cref="CommonMarkSettings" /> class.</summary>
[Obsolete("Use CommonMarkSettings.Default.Clone() instead", false)]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public CommonMarkSettings()
{ }
/// <summary>
/// Gets or sets the output format used by the last stage of conversion.
/// </summary>
public OutputFormat OutputFormat { get; set; }
private Action<Syntax.Block, System.IO.TextWriter, CommonMarkSettings> _outputDelegate;
/// <summary>
/// Gets or sets the custom output delegate function used for formatting CommonMark output.
/// Setting this to a non-null value will also set <see cref="OutputFormat"/> to <see cref="OutputFormat.CustomDelegate"/>.
/// </summary>
public Action<Syntax.Block, System.IO.TextWriter, CommonMarkSettings> OutputDelegate
{
get { return this._outputDelegate; }
set
{
if (this._outputDelegate != value)
{
this._outputDelegate = value;
this.OutputFormat = value == null ? default(OutputFormat) : OutputFormat.CustomDelegate;
}
}
}
/// <summary>
/// Gets or sets a value indicating whether soft line breaks should be rendered as hard line breaks.
/// </summary>
public bool RenderSoftLineBreaksAsLineBreaks { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the parser tracks precise positions in the source data for
/// block and inline elements. This is disabled by default because it incurs an additional performance cost to
/// keep track of the original position.
/// Setting this to <c>true</c> will populate <see cref="Syntax.Inline.SourcePosition"/>,
/// <see cref="Syntax.Inline.SourceLength"/>, <see cref="Syntax.Block.SourcePosition"/> and
/// <see cref="Syntax.Block.SourceLength"/> properties with correct information, otherwise the values
/// of these properties are undefined.
/// This also controls if these values will be written to the output.
/// </summary>
public bool TrackSourcePosition { get; set; }
private CommonMarkAdditionalFeatures _additionalFeatures;
/// <summary>
/// Gets or sets any additional features (that are not present in the current CommonMark specification) that
/// the parser and/or formatter will recognize.
/// </summary>
public CommonMarkAdditionalFeatures AdditionalFeatures
{
get { return this._additionalFeatures; }
set { this._additionalFeatures = value; this._inlineParsers = null; this._inlineParserSpecialCharacters = null; }
}
private Func<string, string> _uriResolver;
/// <summary>
/// Gets or sets the delegate that is used to resolve addresses during rendering process. Can be used to process application relative URLs (<c>~/foo/bar</c>).
/// </summary>
/// <example><code>CommonMarkSettings.Default.UriResolver = VirtualPathUtility.ToAbsolute;</code></example>
public Func<string, string> UriResolver
{
get { return this._uriResolver; }
set
{
if (value != null)
{
var orig = value;
value = x =>
{
try
{
return orig(x);
}
catch (Exception ex)
{
throw new CommonMarkException("An error occurred while executing the CommonMarkSettings.UriResolver delegate. View inner exception for details.", ex);
}
};
}
this._uriResolver = value;
}
}
#pragma warning disable 0618
private static readonly CommonMarkSettings _default = new CommonMarkSettings();
#pragma warning restore 0618
/// <summary>
/// The default settings for the converter. If the properties of this instance are modified, the changes will be applied to all
/// future conversions that do not specify their own settings.
/// </summary>
public static CommonMarkSettings Default { get { return _default; } }
/// <summary>
/// Creates a copy of this configuration object.
/// </summary>
public CommonMarkSettings Clone()
{
return (CommonMarkSettings)this.MemberwiseClone();
}
#region [ Properties that cache structures used in the parsers ]
private Func<Parser.Subject, Syntax.Inline>[] _inlineParsers;
/// <summary>
/// Gets the delegates that parse inline elements according to these settings.
/// </summary>
internal Func<Parser.Subject, Syntax.Inline>[] InlineParsers
{
get
{
var p = this._inlineParsers;
if (p == null)
{
p = Parser.InlineMethods.InitializeParsers(this);
this._inlineParsers = p;
}
return p;
}
}
private char[] _inlineParserSpecialCharacters;
/// <summary>
/// Gets the characters that have special meaning for inline element parsers according to these settings.
/// </summary>
internal char[] InlineParserSpecialCharacters
{
get
{
var v = this._inlineParserSpecialCharacters;
if (v == null)
{
var p = this.InlineParsers;
var vs = new List<char>(20);
for (var i = 0; i < p.Length; i++)
if (p[i] != null)
vs.Add((char)i);
v = this._inlineParserSpecialCharacters = vs.ToArray();
}
return v;
}
}
#endregion
}
}