This repository has been archived by the owner on Jun 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCodePreviewHandlerControl.cs
193 lines (153 loc) · 5.59 KB
/
CodePreviewHandlerControl.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using Common;
using Microsoft.Win32;
using PreviewHandlerCommon;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CodePreviewHandler
{
class CodePreviewHandlerControl : FormHandlerControl
{
private readonly string configKey = "SOFTWARE\\CodePreviewHandler";
private string highLighter = "highlight.exe";
private string highLighterArgs = "-l -k \"JetBrains Mono\" --inline-css {file}";
private long maxFileSize = 200 * 1024;
/// <summary>
/// Extended Browser Control to display source code html.
/// </summary>
private WebBrowserExt browser;
/// <summary>
/// Start the preview on the Control.
/// </summary>
/// <param name="dataSource">Path to the file.</param>
public override void DoPreview<T>(T dataSource)
{
this.InvokeOnControlThread(() =>
{
try
{
LoadConfig();
string filePath = dataSource as string;
FileInfo fileInfo = new FileInfo(filePath);
string html = "";
if (fileInfo.Length > maxFileSize)
{
html = GenerateBigFileHTML(filePath, fileInfo.Length);
}
else
{
html = GenerateHighlightHTML(filePath);
}
//string html = String.Format("<html><body>{0}, {1}, {2}</body></html>", highLighter, highLighterArgs, filePath);
//CodeTelemetry.Info(String.Format("html: %s", html));
this.browser = new WebBrowserExt
{
DocumentText = html,
Dock = DockStyle.Fill,
IsWebBrowserContextMenuEnabled = true,
ScriptErrorsSuppressed = true,
ScrollBarsEnabled = true,
AllowNavigation = false,
};
this.Controls.Add(this.browser);
base.DoPreview(dataSource);
}
catch (Exception e)
{
CodeTelemetry.Info(e.Message);
base.DoPreview(dataSource);
}
});
}
private string GenerateBigFileHTML(string filePath, long fileSize)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<html><body>");
sb.AppendFormat("<div style=\"background-color:lightblue\">big file {0} {1}</div><pre>", fileSize, filePath);
try
{
byte[] head = new byte[maxFileSize / 2];
byte[] tail = new byte[maxFileSize / 2];
FileStream fp = File.OpenRead(filePath);
fp.Read(head, 0, head.Length);
fp.Seek(fileSize - tail.Length, SeekOrigin.Begin);
fp.Read(tail, 0, tail.Length);
fp.Close();
sb.Append(EncodeString(head));
sb.AppendLine("</pre>");
sb.AppendFormat("<div style=\"background-color:lightblue\"> omit {0} bytes</div>", fileSize - maxFileSize);
sb.AppendLine("<pre>");
sb.Append(EncodeString(tail));
}
catch(Exception e)
{
sb.AppendLine(e.ToString());
}
sb.AppendLine("</pre></body></html>");
return sb.ToString();
}
private string GenerateHighlightHTML(string src)
{
StringBuilder output = new StringBuilder();
Process process = new Process();
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = highLighter;
process.StartInfo.Arguments = highLighterArgs.Replace("{file}", src);
process.OutputDataReceived += (sender, args) => output.AppendLine(args.Data);
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
return output.ToString();
}
private string EncodeString(byte[] buff)
{
string text = "";
try
{
text = Encoding.UTF8.GetString(buff);
}
catch (Exception)
{
}
if (text.Length == 0)
{
try
{
text = Encoding.GetEncoding("gbk").GetString(buff);
}
catch (Exception)
{
}
}
if (text.Length == 0)
{
try
{
text = Encoding.ASCII.GetString(buff);
}
catch (Exception)
{
}
}
return text;
}
private void LoadConfig()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(configKey);
if (key != null)
{
if (key.GetValue("HighLighter") is string exe && exe.Length > 0)
highLighter = exe;
if (key.GetValue("HighLighterArgs") is string arg && arg.Length > 0)
highLighterArgs = arg;
}
}
}
}