Skip to content

Commit bc6840b

Browse files
committed
替换code view
1 parent 437936f commit bc6840b

File tree

6 files changed

+72
-20
lines changed

6 files changed

+72
-20
lines changed

lib/common/dao/ReposDao.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ class ReposDao {
133133
var res = await HttpManager.netFetch(
134134
url,
135135
null,
136-
text ? {"Accept": 'application/vnd.github.VERSION.raw'} : {"Accept": 'application/vnd.github.html'},
137-
new Options(contentType: text ? ContentType.TEXT : ContentType.JSON),
136+
//text ? {"Accept": 'application/vnd.github.VERSION.raw'} : {"Accept": 'application/vnd.github.html'},
137+
text ? {"Accept": 'application/vnd.github.html'} : {"Accept": 'application/vnd.github.VERSION.raw'},
138+
new Options(contentType: text ? ContentType.text : ContentType.json),
138139
);
139140
if (res != null && res.result) {
140141
if (text) {

lib/common/utils/CommonUtils.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ class CommonUtils {
104104

105105
static void launchWebView(BuildContext context, String title, String url) {
106106
if (url.startsWith("http")) {
107-
NavigatorUtils.goCodeDetailPageWeb(context, url, title);
107+
NavigatorUtils.goGSYWebView(context, url, title);
108108
} else {
109-
NavigatorUtils.goCodeDetailPageWeb(context, new Uri.dataFromString(url, mimeType: 'text/html', encoding: Encoding.getByName("utf-8")).toString(), title);
109+
NavigatorUtils.goGSYWebView(context, new Uri.dataFromString(url, mimeType: 'text/html', encoding: Encoding.getByName("utf-8")).toString(), title);
110110
}
111111
}
112112

lib/common/utils/HtmlUtils.dart

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ class HtmlUtils {
1717
if (mdData == null) {
1818
return "";
1919
}
20-
RegExp exp = new RegExp("<code(([\s\S])*?)<\/code>");
20+
RegExp exp = new RegExp("<code(([\\s\\S])*?)<\/code>");
2121
Iterable<Match> tags = exp.allMatches(mdData);
2222
String mdDataCode = mdData;
23+
print("---------------------`--------------------------");
24+
print(tags);
2325
for (Match m in tags) {
26+
print(m.group(0));
2427
String match = m.group(0).replaceAll(new RegExp("\n"), "\n\r<br>");
28+
print(match);
2529
mdDataCode = mdDataCode.replaceAll(m.group(0), match);
2630
}
2731

28-
exp = new RegExp("<pre(([\s\S])*?)<\/pre>");
32+
exp = new RegExp("<pre(([\\s\\S])*?)<\/pre>");
2933
tags = exp.allMatches(mdDataCode);
3034
for (Match m in tags) {
3135
if (m.group(0).indexOf("<code>") < 0) {
@@ -34,7 +38,7 @@ class HtmlUtils {
3438
}
3539
}
3640

37-
exp = new RegExp("<pre>(([\s\S])*?)<\/pre>");
41+
exp = new RegExp("<pre>(([\\s\\S])*?)<\/pre>");
3842
tags = exp.allMatches(mdDataCode);
3943
for (Match m in tags) {
4044
if (m.group(0).indexOf("<code>") < 0) {
@@ -202,4 +206,49 @@ class HtmlUtils {
202206
}
203207
return builder;
204208
}
209+
210+
211+
static resolveHtmlFile(var res, String defaultLang) {
212+
if (res != null && res.result) {
213+
String startTag = "class=\"instapaper_body ";
214+
int startLang = res.data.indexOf(startTag);
215+
int endLang = res.data.indexOf("\" data-path=\"");
216+
String lang;
217+
if (startLang >= 0 && endLang >= 0) {
218+
String tmpLang = res.data.substring(startLang + startTag.length, endLang);
219+
if (tmpLang != null) {
220+
lang = formName(tmpLang.toLowerCase());
221+
}
222+
}
223+
if (lang == null) {
224+
lang = defaultLang;
225+
}
226+
if ('markdown' == lang) {
227+
return generateHtml(res.data, backgroundColor: GSYColors.miWhiteString);
228+
} else {
229+
return generateCode2HTml(res.data, backgroundColor: GSYColors.webDraculaBackgroundColorString, lang: lang);
230+
}
231+
} else {
232+
return "<h1>" + "Not Support" + "</h1>";
233+
}
234+
}
235+
236+
static formName(name) {
237+
switch (name) {
238+
case 'sh':
239+
return 'shell';
240+
case 'js':
241+
return 'javascript';
242+
case 'kt':
243+
return 'kotlin';
244+
case 'c':
245+
case 'cpp':
246+
return 'cpp';
247+
case 'md':
248+
return 'markdown';
249+
case 'html':
250+
return 'xml';
251+
}
252+
return name;
253+
}
205254
}

lib/common/utils/NavigatorUtils.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import 'dart:async';
22

33
import 'package:flutter/material.dart';
44
import 'package:gsy_github_app_flutter/page/CodeDetailPage.dart';
5-
import 'package:gsy_github_app_flutter/page/CodeDetailPageWeb.dart';
65
import 'package:gsy_github_app_flutter/page/CommonListPage.dart';
6+
import 'package:gsy_github_app_flutter/page/GSYWebView.dart';
77
import 'package:gsy_github_app_flutter/page/HomePage.dart';
88
import 'package:gsy_github_app_flutter/page/IssueDetailPage.dart';
99
import 'package:gsy_github_app_flutter/page/LoginPage.dart';
@@ -115,12 +115,13 @@ class NavigatorUtils {
115115
)));
116116
}
117117

118-
static Future<Null> goCodeDetailPageWeb(BuildContext context, String url, String title) {
118+
static Future<Null> goGSYWebView(BuildContext context, String url, String title) {
119119
return Navigator.push(
120120
context,
121121
new MaterialPageRoute(
122-
builder: (context) => new CodeDetailPageWeb(url, title),
122+
builder: (context) => new GSYWebView(url, title),
123123
),
124124
);
125125
}
126+
126127
}

lib/page/CodeDetailPageWeb.dart renamed to lib/page/GSYWebView.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
77
* Created by guoshuyu
88
* on 2018/7/27.
99
*/
10-
class CodeDetailPageWeb extends StatelessWidget {
10+
class GSYWebView extends StatelessWidget {
1111
final String url;
1212
final String title;
1313

14-
CodeDetailPageWeb(this.url, this.title);
14+
GSYWebView(this.url, this.title);
1515

1616
@override
1717
Widget build(BuildContext context) {

lib/page/RepositoryFileListPage.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
22
import 'package:gsy_github_app_flutter/common/dao/ReposDao.dart';
33
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
44
import 'package:gsy_github_app_flutter/common/utils/CommonUtils.dart';
5+
import 'package:gsy_github_app_flutter/common/utils/HtmlUtils.dart';
56
import 'package:gsy_github_app_flutter/common/utils/NavigatorUtils.dart';
67
import 'package:gsy_github_app_flutter/page/RepositoryDetailPage.dart';
78
import 'package:gsy_github_app_flutter/widget/GSYCardItem.dart';
@@ -121,14 +122,14 @@ class RepositoryDetailFileListPageState extends GSYListState<RepositoryDetailFil
121122
if (CommonUtils.isImageEnd(fileItemViewModel.name)) {
122123
//todo 图片
123124
} else {
124-
NavigatorUtils.gotoCodeDetailPage(
125-
context,
126-
title: fileItemViewModel.name,
127-
reposName: reposName,
128-
userName: userName,
129-
path: path,
130-
branch: branchControl.currentBranch,
131-
);
125+
CommonUtils.showLoadingDialog(context);
126+
ReposDao.getReposFileDirDao(userName, reposName, path: path, branch: branchControl.currentBranch, text: true).then((res) {
127+
if (res != null && res.result) {
128+
Navigator.pop(context);
129+
String data = HtmlUtils.resolveHtmlFile(res, "java");
130+
CommonUtils.launchWebView(context, fileItemViewModel.name, data);
131+
}
132+
});
132133
}
133134
}
134135
}

0 commit comments

Comments
 (0)