FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/markdown/MathJax.cpp at master · UXScripts/rstudio · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
UXScripts
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
markdown
/
MathJax.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
270 lines (224 loc) · 8.04 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
markdown
/
MathJax.cpp
Copy path
File metadata and controls
270 lines (224 loc) · 8.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* MathJax.cpp
*
* Copyright (C) 2009-12 by RStudio, Inc.
*
* This program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
"
MathJax.hpp
"
#
include
<
algorithm
>
#
include
<
boost/bind.hpp
>
#
include
<
boost/function.hpp
>
#
include
<
boost/foreach.hpp
>
#
include
<
boost/algorithm/string.hpp
>
#
include
<
core/system/System.hpp
>
namespace
core
{
namespace
markdown
{
namespace
{
struct
TextRange
{
TextRange
(
bool
process,
const
std::string::const_iterator& begin,
const
std::string::const_iterator& end)
: process(process), begin(begin), end(end)
{
}
bool
process;
std::string::const_iterator begin;
std::string::const_iterator end;
};
TextRange
findClosestRange
(std::string::const_iterator pos,
const
std::vector<TextRange>& ranges)
{
TextRange closestRange = ranges.
front
();
BOOST_FOREACH
(
const
TextRange& range, ranges)
{
if
(
std::abs
(range.
begin
- pos) <
std::abs
(closestRange.
begin
- pos))
closestRange = range;
}
return
closestRange;
}
bool
hasLessThanThreeNewlines
(
const
std::string& str)
{
return
std::count
(str.
begin
(), str.
end
(),
'
\n
'
) <
3
;
}
}
MathJaxFilter::MathJaxFilter
(
const
std::vector<ExcludePattern>& excludePatterns,
std::string* pInput,
std::string* pHTMLOutput)
: pHTMLOutput_(pHTMLOutput)
{
//
divide the document into ranges (some of which will be processed
//
and some of which will not -- we don't process some regions so that
//
we don't need to worry about mathjax ambiguity within code regions)
std::vector<TextRange> ranges;
std::string::const_iterator pos = pInput->
begin
();
std::string::const_iterator inputEnd = pInput->
end
();
while
(pos != inputEnd)
{
//
try all of the exclude patterns
std::vector<TextRange> matchedRanges;
BOOST_FOREACH
(
const
ExcludePattern& pattern, excludePatterns)
{
boost::smatch m;
if
(
boost::regex_search
(pos, inputEnd, m, pattern.
begin
))
{
//
set begin and end (may change if there is an end pattern)
std::string::const_iterator begin = m[
0
].
first
;
std::string::const_iterator end = m[
0
].
second
;
//
check for a second match
if
(!pattern.
end
.
empty
())
{
if
(
boost::regex_search
(end, inputEnd, m, pattern.
end
))
{
//
update end to be the end of the match
end = m[
0
].
second
;
}
else
{
//
didn't find a matching end pattern so set the end to the
//
end of the document -- this will cause us to exclude the
//
rest of the document from processing
end = inputEnd;
}
}
//
add the matched range to our list
matchedRanges.
push_back
(
TextRange
(
false
, begin, end));
}
}
//
if we found at least one matched range then find the closest one,
//
add it to our list, and continue
if
(!matchedRanges.
empty
())
{
//
find the closest range
TextRange range =
findClosestRange
(pos, matchedRanges);
//
mark everything before the match as requiring processing
ranges.
push_back
(
TextRange
(
true
, pos, range.
begin
));
//
add the range
ranges.
push_back
(range);
//
update the position
pos = range.
end
;
}
//
no match -- consume remaining input and tag it for processing
else
{
ranges.
push_back
(
TextRange
(
true
, pos, pInput->
end
()));
pos = pInput->
end
();
}
}
//
now iterate through the ranges and substitute a guid for math blocks
std::string filteredInput;
BOOST_FOREACH
(
const
TextRange& range, ranges)
{
std::string
rangeText
(range.
begin
, range.
end
);
if
(range.
process
)
{
//
native mathjax display equations
filter
(
boost::regex
(
"
\\\\\\
[([
\\
s
\\
S]+?)
\\\\\\
]
"
),
&rangeText,
&displayMathBlocks_);
//
latex display equations (latex designator optional, used for
//
syntactic compatiblity w/ wordpress-style inline equations)
filter
(
boost::regex
(
"
\\
${2}(?:latex
\\
s)?([
\\
s
\\
S]+?)
\\
${2}
"
),
&rangeText,
&displayMathBlocks_);
//
native mathjax inline equations
filter
(
boost::regex
(
"
\\\\\\
(([
\\
s
\\
S]+?)
\\\\\\
)
"
),
&rangeText,
&inlineMathBlocks_);
//
wordpress style inline equations
filter
(
boost::regex
(
"
\\
$latex
\\
s([
\\
s
\\
S]+?)
\\
$
"
),
&rangeText,
&inlineMathBlocks_);
//
Org-mode style inline equations
filter
(
boost::regex
(
"
\\
$((?!
\\
s)[^$]*[^$
\\
s])
\\
$(?![
\\
w
\\
d`])
"
),
&hasLessThanThreeNewlines,
&rangeText,
&inlineMathBlocks_);
}
filteredInput.
append
(rangeText);
}
*pInput = filteredInput;
}
MathJaxFilter::~MathJaxFilter
()
{
try
{
std::for_each
(
displayMathBlocks_.
begin
(),
displayMathBlocks_.
end
(),
boost::bind
(&MathJaxFilter::restore,
this
, _1,
"
\\
[
"
,
"
\\
]
"
));
std::for_each
(
inlineMathBlocks_.
begin
(),
inlineMathBlocks_.
end
(),
boost::bind
(&MathJaxFilter::restore,
this
, _1,
"
\\
(
"
,
"
\\
)
"
));
}
catch
(...)
{
}
}
void
MathJaxFilter::filter
(
const
boost::regex& re,
const
boost::function<
bool
(
const
std::string&)>& condition,
std::string* pInput,
std::map<std::string,MathBlock>* pMathBlocks)
{
//
explicit function type required because the Formatter functor
//
supports 3 distinct signatures
boost::function<
std::string
(
boost::match_results<std::string::const_iterator>)> formatter =
boost::bind
(&MathJaxFilter::substitute,
this
, condition, _1, pMathBlocks);
*pInput =
boost::regex_replace
(*pInput, re, formatter);
}
std::string
MathJaxFilter::substitute
(
const
boost::function<
bool
(
const
std::string&)>& condition,
boost::match_results<std::string::const_iterator> match,
std::map<std::string,MathBlock>* pMathBlocks)
{
//
get the equation
std::string equation = match[
1
];
//
apply additional condition if available
if
(condition && !
condition
(equation))
{
//
don't perform any substitution
return
match[
0
];
}
else
{
std::string guid =
core::system::generateUuid
(
false
);
std::string suffix = (match.
size
() >
2
) ?
std::string
(match[
2
]) :
"
"
;
pMathBlocks->
insert
(
std::make_pair
(guid,
MathBlock
(equation,suffix)));
return
guid;
}
}
void
MathJaxFilter::restore
(
const
std::map<std::string,MathBlock>::value_type& block,
const
std::string& beginDelim,
const
std::string& endDelim)
{
boost::algorithm::replace_first
(
*pHTMLOutput_,
block.
first
,
beginDelim +
"
"
+ block.
second
.
equation
+
"
"
+ endDelim +
block.
second
.
suffix
);
}
bool
requiresMathjax
(
const
std::string& htmlOutput)
{
boost::regex
inlineMathRegex
(
"
\\\\\\
(([
\\
s
\\
S]+?)
\\\\\\
)
"
);
if
(
boost::regex_search
(htmlOutput, inlineMathRegex))
return
true
;
boost::regex
displayMathRegex
(
"
\\\\\\
[([
\\
s
\\
S]+?)
\\\\\\
]
"
);
if
(
boost::regex_search
(htmlOutput, displayMathRegex))
return
true
;
boost::regex
mathmlRegex
(
"
<math[>
\\
s](?s).*?</math>
"
);
if
(
boost::regex_search
(htmlOutput, mathmlRegex))
return
true
;
return
false
;
}
}
//
namespace markdown
}
//
namespace core
Back
|
FazBrowse Home
|
New Git URL