-
-
Notifications
You must be signed in to change notification settings - Fork 437
/
MathInlineParser.cs
177 lines (154 loc) · 5.38 KB
/
MathInlineParser.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
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.
using Markdig.Helpers;
using Markdig.Parsers;
using Markdig.Renderers.Html;
using Markdig.Syntax;
namespace Markdig.Extensions.Mathematics;
/// <summary>
/// An inline parser for <see cref="MathInline"/>.
/// </summary>
/// <seealso cref="InlineParser" />
/// <seealso cref="IPostInlineProcessor" />
public class MathInlineParser : InlineParser
{
/// <summary>
/// Initializes a new instance of the <see cref="MathInlineParser"/> class.
/// </summary>
public MathInlineParser()
{
OpeningCharacters = ['$'];
DefaultClass = "math";
}
/// <summary>
/// Gets or sets the default class to use when creating a math inline block.
/// </summary>
public string DefaultClass { get; set; }
public override bool Match(InlineProcessor processor, ref StringSlice slice)
{
var match = slice.CurrentChar;
var pc = slice.PeekCharExtra(-1);
if (pc == match)
{
return false;
}
var startPosition = slice.Start;
// Match the opened $ or $$
int openDollars = 1; // we have at least a $
var c = slice.NextChar();
if (c == match)
{
openDollars++;
c = slice.NextChar();
}
pc.CheckUnicodeCategory(out bool openPrevIsWhiteSpace, out bool openPrevIsPunctuation);
c.CheckUnicodeCategory(out bool openNextIsWhiteSpace, out _);
// Check that opening $/$$ is correct, using the different heuristics than for emphasis delimiters
// If a $/$$ is not preceded by a whitespace or punctuation, this is a not a math block
if ((!openPrevIsWhiteSpace && !openPrevIsPunctuation))
{
return false;
}
bool isMatching = false;
int closeDollars = 0;
// Eat any leading spaces
while (c.IsSpaceOrTab())
{
c = slice.NextChar();
}
var start = slice.Start;
var end = 0;
pc = match;
var lastWhiteSpace = -1;
while (c != '\0')
{
// Don't allow newline in an inline math expression
if (c == '\r' || c == '\n')
{
return false;
}
// Don't process sticks if we have a '\' as a previous char
if (pc != '\\')
{
// Record continuous whitespaces at the end
if (c.IsSpaceOrTab())
{
if (lastWhiteSpace < 0)
{
lastWhiteSpace = slice.Start;
}
}
else
{
bool hasClosingDollars = c == match;
if (hasClosingDollars)
{
closeDollars += slice.CountAndSkipChar(match);
c = slice.CurrentChar;
}
if (closeDollars >= openDollars)
{
break;
}
lastWhiteSpace = -1;
if (hasClosingDollars)
{
pc = match;
continue;
}
}
}
if (closeDollars > 0)
{
closeDollars = 0;
}
else
{
pc = c;
c = slice.NextChar();
}
}
if (closeDollars >= openDollars)
{
pc.CheckUnicodeCategory(out bool closePrevIsWhiteSpace, out _);
c.CheckUnicodeCategory(out bool closeNextIsWhiteSpace, out bool closeNextIsPunctuation);
// A closing $/$$ should be followed by at least a punctuation or a whitespace
// and if the character after an opening $/$$ was a whitespace, it should be
// a whitespace as well for the character preceding the closing of $/$$
if ((!closeNextIsPunctuation && !closeNextIsWhiteSpace) || (openNextIsWhiteSpace != closePrevIsWhiteSpace))
{
return false;
}
if (closePrevIsWhiteSpace && lastWhiteSpace > 0)
{
end = lastWhiteSpace + openDollars - 1;
}
else
{
end = slice.Start - 1;
}
// Create a new MathInline
var inline = new MathInline()
{
Span = new SourceSpan(processor.GetSourcePosition(startPosition, out int line, out int column), processor.GetSourcePosition(slice.Start - 1)),
Line = line,
Column = column,
Delimiter = match,
DelimiterCount = openDollars,
Content = slice
};
inline.Content.Start = start;
// We substract the end to the number of opening $ to keep inside the block the additionals $
inline.Content.End = end - openDollars;
// Add the default class if necessary
if (DefaultClass != null)
{
inline.GetAttributes().AddClass(DefaultClass);
}
processor.Inline = inline;
isMatching = true;
}
return isMatching;
}
}