forked from CarGuo/gsy_github_app_flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlUtils.dart
More file actions
267 lines (251 loc) · 8.35 KB
/
HtmlUtils.dart
File metadata and controls
267 lines (251 loc) · 8.35 KB
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
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
/**
* Created by guoshuyu
* on 2018/7/27.
*/
class HtmlUtils {
static generateCode2HTml(String mdData, {String backgroundColor = GSYColors.miWhiteString, String lang = 'java', userBR = true}) {
String currentData = (mdData != null && mdData.indexOf("<code>") == -1)
? "<body>\n" + "<pre class=\"pre\">\n" + "<code lang='$lang'>\n" + mdData + "</code>\n" + "</pre>\n" + "</body>\n"
: "<body>\n" + "<pre class=\"pre\">\n" + mdData + "</pre>\n" + "</body>\n";
return generateHtml(currentData, backgroundColor: backgroundColor, userBR: userBR);
}
static generateHtml(String mdData, {String backgroundColor = GSYColors.webDraculaBackgroundColorString, userBR = true}) {
if (mdData == null) {
return "";
}
String mdDataCode = mdData;
String regExCode = "<[\\s]*?code[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?code[\\s]*?>";
String regExPre = "<[\\s]*?pre[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?pre[\\s]*?>";
try {
RegExp exp = new RegExp(regExCode);
Iterable<Match> tags = exp.allMatches(mdData);
for (Match m in tags) {
String match = m.group(0).replaceAll(new RegExp("\n"), "\n\r<br>");
mdDataCode = mdDataCode.replaceAll(m.group(0), match);
}
} catch (e) {
print(e);
}
try {
RegExp exp = new RegExp(regExPre);
Iterable<Match> tags = exp.allMatches(mdDataCode);
for (Match m in tags) {
if (m.group(0).indexOf("<code>") < 0) {
String match = m.group(0).replaceAll(new RegExp("\n"), "\n\r<br>");
mdDataCode = mdDataCode.replaceAll(m.group(0), match);
}
}
} catch (e) {
print(e);
}
try {
RegExp exp = new RegExp("<pre>(([\\s\\S])*?)<\/pre>");
Iterable<Match> tags = exp.allMatches(mdDataCode);
for (Match m in tags) {
if (m.group(0).indexOf("<code>") < 0) {
String match = m.group(0).replaceAll(new RegExp("\n"), "\n\r<br>");
mdDataCode = mdDataCode.replaceAll(m.group(0), match);
}
}
} catch (e) {
print(e);
}
try {
RegExp exp = new RegExp("href=\"(.*?)\"");
Iterable<Match> tags = exp.allMatches(mdDataCode);
for (Match m in tags) {
String capture = m.group(0);
if (capture.indexOf("http://") < 0 && capture.indexOf("https://") < 0 && capture.indexOf("#") != 0) {
mdDataCode = mdDataCode.replaceAll(m.group(0), "gsygithub://" + capture);
}
}
} catch (e) {
print(e);
}
return generateCodeHtml(mdDataCode, false, backgroundColor: backgroundColor, actionColor: GSYColors.actionBlueString, userBR: userBR);
}
/**
* style for mdHTml
*/
static generateCodeHtml(mdHTML, wrap, {backgroundColor = GSYColors.white, String actionColor = GSYColors.actionBlueString, userBR = true}) {
return "<html>\n" +
"<head>\n" +
"<meta charset=\"utf-8\" />\n" +
"<title></title>\n" +
"<meta name=\"viewport\" content=\"width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;\"/>" +
"<link href=\"https:\/\/cdn.bootcss.com/highlight.js/9.12.0/styles/dracula.min.css\" rel=\"stylesheet\">\n" +
"<script src=\"https:\/\/cdn.bootcss.com/highlight.js/9.12.0/highlight.min.js\"></script> " +
"<script>hljs.configure({'useBR': " +
userBR.toString() +
"});hljs.initHighlightingOnLoad();</script> " +
"<style>" +
"body{background: " +
backgroundColor +
";}" +
"a {color:" +
actionColor +
" !important;}" +
".highlight pre, pre {" +
" word-wrap: " +
(wrap ? "break-word" : "normal") +
"; " +
" white-space: " +
(wrap ? "pre-wrap" : "pre") +
"; " +
"}" +
"thead, tr {" +
"background:" +
GSYColors.miWhiteString +
";}" +
"td, th {" +
"padding: 5px 10px;" +
"font-size: 12px;" +
"direction:hor" +
"}" +
".highlight {overflow: scroll; background: " +
GSYColors.miWhiteString +
"}" +
"tr:nth-child(even) {" +
"background:" +
GSYColors.primaryLightValueString +
";" +
"color:" +
GSYColors.miWhiteString +
";" +
"}" +
"tr:nth-child(odd) {" +
"background: " +
GSYColors.miWhiteString +
";" +
"color:" +
GSYColors.primaryLightValueString +
";" +
"}" +
"th {" +
"font-size: 14px;" +
"color:" +
GSYColors.miWhiteString +
";" +
"background:" +
GSYColors.primaryLightValueString +
";" +
"}" +
"</style>" +
"</head>\n" +
"<body>\n" +
mdHTML +
"</body>\n" +
"</html>";
}
static parseDiffSource(String diffSource, bool wrap) {
if (diffSource == null) {
return "";
}
List<String> lines = diffSource.split("\n");
String source = "";
int addStartLine = -1;
int removeStartLine = -1;
int addLineNum = 0;
int removeLineNum = 0;
int normalLineNum = 0;
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
String lineNumberStr = "";
String classStr = "";
int curAddNumber = -1;
int curRemoveNumber = -1;
if (line.indexOf("+") == 0) {
classStr = "class=\"hljs-addition\";";
curAddNumber = addStartLine + normalLineNum + addLineNum;
addLineNum++;
} else if (line.indexOf("-") == 0) {
classStr = "class=\"hljs-deletion\";";
curRemoveNumber = removeStartLine + normalLineNum + removeLineNum;
removeLineNum++;
} else if (line.indexOf("@@") == 0) {
classStr = "class=\"hljs-literal\";";
removeStartLine = getRemoveStartLine(line);
addStartLine = getAddStartLine(line);
addLineNum = 0;
removeLineNum = 0;
normalLineNum = 0;
} else if (!(line.indexOf("\\") == 0)) {
curAddNumber = addStartLine + normalLineNum + addLineNum;
curRemoveNumber = removeStartLine + normalLineNum + removeLineNum;
normalLineNum++;
}
lineNumberStr =
getDiffLineNumber(curRemoveNumber == -1 ? "" : (curRemoveNumber.toString() + ""), curAddNumber == -1 ? "" : (curAddNumber.toString() + ""));
source = source + "\n" + "<div " + classStr + ">" + (wrap ? "" : lineNumberStr + getBlank(1)) + line + "</div>";
}
return source;
}
static getRemoveStartLine(line) {
try {
return int.parse(line.substring(line.indexOf("-") + 1, line.indexOf(",")));
} catch (e) {
return 1;
}
}
static getAddStartLine(line) {
try {
return int.parse(line.substring(line.indexOf("+") + 1, line.indexOf(",", line.indexOf("+"))));
} catch (e) {
return 1;
}
}
static getDiffLineNumber(String removeNumber, String addNumber) {
int minLength = 4;
return getBlank(minLength - removeNumber.length) + removeNumber + getBlank(1) + getBlank(minLength - addNumber.length) + addNumber;
}
static getBlank(num) {
String builder = "";
for (int i = 0; i < num; i++) {
builder += " ";
}
return builder;
}
static resolveHtmlFile(var res, String defaultLang) {
if (res != null && res.result) {
String startTag = "class=\"instapaper_body ";
int startLang = res.data.indexOf(startTag);
int endLang = res.data.indexOf("\" data-path=\"");
String lang;
if (startLang >= 0 && endLang >= 0) {
String tmpLang = res.data.substring(startLang + startTag.length, endLang);
if (tmpLang != null) {
lang = formName(tmpLang.toLowerCase());
}
}
if (lang == null) {
lang = defaultLang;
}
if ('markdown' == lang) {
return generateHtml(res.data, backgroundColor: GSYColors.miWhiteString);
} else {
return generateCode2HTml(res.data, backgroundColor: GSYColors.webDraculaBackgroundColorString, lang: lang);
}
} else {
return "<h1>" + "Not Support" + "</h1>";
}
}
static formName(name) {
switch (name) {
case 'sh':
return 'shell';
case 'js':
return 'javascript';
case 'kt':
return 'kotlin';
case 'c':
case 'cpp':
return 'cpp';
case 'md':
return 'markdown';
case 'html':
return 'xml';
}
return name;
}
}