FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/HtmlUtils.cpp at master · pearsonca/rstudio · GitHub
pearsonca
/
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
/
HtmlUtils.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
239 lines (199 loc) · 6.19 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
HtmlUtils.cpp
Copy path
File metadata and controls
239 lines (199 loc) · 6.19 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
/*
* HtmlUtils.cpp
*
* Copyright (C) 2009-12 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* 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
<
core/HtmlUtils.hpp
>
#
include
<
core/system/System.hpp
>
#
include
<
boost/format.hpp
>
#
include
<
boost/algorithm/string/predicate.hpp
>
#
include
<
core/Base64.hpp
>
#
include
<
core/FileSerializer.hpp
>
#
include
<
core/RegexUtils.hpp
>
#
include
<
core/http/Util.hpp
>
namespace
rstudio
{
namespace
core
{
namespace
html_utils
{
HTML::HTML
(
const
std::string& text,
bool
isHTML)
{
if
(!isHTML)
text_ =
string_utils::htmlEscape
(text);
else
text_ = text;
}
std::string
defaultTitle
(
const
std::string& htmlContent)
{
boost::regex
re
(
"
<[Hh]([1-6]).*?>(.*?)</[Hh]
\\
1>
"
);
boost::smatch match;
if
(
regex_utils::search
(htmlContent, match, re))
return
match[
2
];
else
return
"
"
;
}
Base64ImageFilter::Base64ImageFilter
(
const
FilePath& basePath)
: boost::iostreams::regex_filter(
boost::regex
(
R"(
(<\s*[Ii][Mm][Gg] [^\>]*[Ss][Rr][Cc]\s*=\s*)(["'])(.*?)(\2)
)"
),
boost::bind(&Base64ImageFilter::toBase64Image,
this
, _1)),
basePath_(basePath)
{
}
std::string
Base64ImageFilter::toBase64Image
(
const
boost::cmatch& match)
{
//
extract image reference
std::string imgRef = match[
3
];
//
url decode it
imgRef =
http::util::urlDecode
(imgRef);
//
see if this is an image within the base directory. if it is then
//
base64 encode it
FilePath imagePath = basePath_.
completeChildPath
(imgRef);
if
(imagePath.
exists
() &&
boost::algorithm::starts_with
(imagePath.
getMimeContentType
(),
"
image/
"
))
{
std::string imageBase64;
Error error =
core::base64::encode
(imagePath, &imageBase64);
if
(!error)
{
imgRef =
"
data:
"
+ imagePath.
getMimeContentType
() +
"
;base64,
"
;
imgRef.
append
(imageBase64);
}
else
{
LOG_ERROR
(error);
}
}
//
return the filtered result
return
match[
1
] + match[
2
] + imgRef + match[
4
];
}
//
convert fonts to base64
CssUrlFilter::CssUrlFilter
(
const
FilePath& basePath)
: boost::iostreams::regex_filter(
boost::regex
(
"
url
\\
('([^'']+)'
\\
)
"
),
boost::bind(&CssUrlFilter::toBase64Url,
this
, _1)),
basePath_(basePath)
{
}
std::string
CssUrlFilter::toBase64Url
(
const
boost::cmatch& match)
{
//
is this a local file?
std::string urlRef = match[
1
];
FilePath urlPath = basePath_.
completeChildPath
(urlRef);
std::string ext = urlPath.
getExtensionLowerCase
();
if
(urlPath.
exists
() && (ext ==
"
.ttf
"
|| ext ==
"
.otf
"
))
{
std::string fontBase64;
Error error =
core::base64::encode
(urlPath, &fontBase64);
if
(!error)
{
//
return base64 encoded font
std::string type = (ext ==
"
.ttf
"
) ?
"
truetype
"
:
"
opentype
"
;
boost::format
fmt
(
"
url(data:font/%1%;base64,%2%)
"
);
return
boost::str
(fmt % type % fontBase64);
}
else
{
LOG_ERROR
(error);
return
match[
0
];
}
}
else
{
return
match[
0
];
}
}
TextRange
findClosestRange
(std::string::const_iterator pos,
const
std::vector<TextRange>& ranges)
{
TextRange closestRange = ranges.
front
();
for
(
const
auto
& range : ranges)
{
if
(
std::abs
(range.
begin
- pos) <
std::abs
(closestRange.
begin
- pos))
closestRange = range;
}
return
closestRange;
}
void
HtmlPreserver::preserve
(std::string* pInput)
{
//
begin and end regexes
boost::regex
beginPreserve
(
"
<!--html_preserve-->
"
);
boost::regex
endPreserve
(
"
<!--
\\
/html_preserve-->
"
);
//
discover ranges we need to preserve
std::vector<TextRange> ranges;
std::string::const_iterator pos = pInput->
begin
();
std::string::const_iterator inputEnd = pInput->
end
();
while
(pos != inputEnd)
{
//
look for begin marker
boost::smatch m;
if
(
regex_utils::search
(pos, inputEnd, m, beginPreserve))
{
//
set begin iterator
std::string::const_iterator begin = m[
0
].
first
;
std::string::const_iterator end = m[
0
].
second
;
//
look for end marker
if
(
regex_utils::search
(end, inputEnd, m, endPreserve))
{
//
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;
}
//
mark everything before the match as requiring processing
ranges.
push_back
(
TextRange
(
true
, pos, begin));
//
add the matched range to our list
ranges.
push_back
(
TextRange
(
false
, begin, end));
//
update the position
pos = end +
1
;
}
else
{
//
no more preserve regions, set termination condition
ranges.
push_back
(
TextRange
(
true
, pos, pInput->
end
()));
pos = inputEnd;
}
}
//
substitute guids for all of the matched ranges
std::string modifiedInput;
for
(
auto
& range : ranges)
{
if
(range.
process
)
{
modifiedInput +=
std::string
(range.
begin
, range.
end
);
}
else
{
std::string guid =
core::system::generateUuid
();
std::string html =
std::string
(range.
begin
, range.
end
);
preserved_[guid] = html;
modifiedInput += guid;
}
}
//
return the modified input
*pInput = modifiedInput;
}
void
HtmlPreserver::restore
(std::string* pOutput)
{
for
(
const
auto
& preserve : preserved_)
{
boost::algorithm::replace_first
(*pOutput,
preserve.
first
,
preserve.
second
);
}
}
}
//
namespace html_utils
}
//
namespace core
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL