-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathUtils.cs
184 lines (167 loc) · 5.76 KB
/
Utils.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
/*
* 由SharpDevelop创建。
* 用户: dodola
* 日期: 2012/10/22
* 时间: 17:20
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
using System.Collections;
using System.IO;
using System.Security;
using System.Text;
using System.Windows;
using MarkdownSharp;
using MEditor.Properties;
namespace MEditor
{
/// <summary>
/// Description of Utils.
/// </summary>
internal static class Utils
{
// Fields
private static Settings _settings = Settings.Default;
// Methods
public static string AppendValidHTMLTags(string input, string fileName, bool isLivePreview)
{
StringBuilder builder = new StringBuilder();
if (!isLivePreview)
{
builder.AppendLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
}
builder.AppendLine("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">");
builder.AppendLine("<head>");
if (string.IsNullOrEmpty(fileName))
{
fileName = "Markdown";
}
builder.AppendLine("<title>" + Path.GetFileName(fileName) + "</title>");
builder.AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
builder.AppendLine("<style type=\"text/css\">");
if (_settings.HTML_UseCustomStylesheet)
{
builder.AppendLine(_settings.HTML_CustomStylesheetSource);
}
else
{
builder.AppendLine(_settings.HTML_StylesheetSource);
}
builder.AppendLine("</style>");
if (isLivePreview)
{
if (_settings.HTML_EnableRelativeImagePaths && (fileName != "Markdown"))
{
builder.AppendLine(@"<base href='file:\\\" + Path.GetDirectoryName(fileName) + @"\'/>");
}
builder.AppendLine("<script type=\"text/javascript\">function loadMarkdown(input) { document.body.innerHTML = input; } </script>");
builder.AppendLine("<script type=\"text/javascript\">function scroller(input) {window.scrollTo(0, input * (document.body.scrollHeight - document.body.clientHeight)); } </script>");
}
builder.AppendLine("</head>");
builder.AppendLine("<body>");
builder.AppendLine(input);
builder.AppendLine("</body>");
builder.AppendLine("</html>");
builder.Append("<!-- This document was created with MarkdownPad, the Markdown editor for Windows (http://markdownpad.com) -->");
return builder.ToString();
}
public static bool Contains(this ArrayList input, string stringToCheck, StringComparison comparison)
{
foreach (string str in input)
{
if (str.IndexOf(stringToCheck, comparison) >= 0)
{
return true;
}
}
return false;
}
public static bool Contains(this string input, string stringToCheck, StringComparison comparison)
{
if (string.IsNullOrEmpty(input))
{
return false;
}
return (input.IndexOf(stringToCheck, comparison) >= 0);
}
public static string ConvertTextToHTML(string plainText)
{
MarkdownOptions options = new MarkdownOptions {
AutoHyperlink = _settings.Markdown_AutoHyperlink,
// AutoNewLines = _settings.Markdown_AutoNewLines,
LinkEmails = _settings.Markdown_LinkEmails,
EncodeProblemUrlCharacters = _settings.Markdown_EncodeProblemUrlCharacters
};
Markdown markdown = new Markdown(options);
markdown.AutoNewLines=_settings.Markdown_AutoNewLines;
string str = string.Empty;
try
{
str = markdown.Transform(plainText);
}
catch (OutOfMemoryException)
{
throw;
}
catch (Exception)
{
throw;
}
return str;
}
// public static string GenerateBugReportURL(string formUrl)
// {
// string version = Version;
// string versionString = Environment.OSVersion.VersionString;
// if (Environment.Is64BitOperatingSystem)
// {
// versionString = versionString + " x64";
// }
// else
// {
// versionString = versionString + " x86";
// }
// return (formUrl + "&entry_0=" + SecurityElement.Escape(version) + "&entry_4=" + SecurityElement.Escape(versionString));
// }
// public static void RenderHTMLinBrowser(string markdown, string fileName)
// {
// string path = string.Empty;
// if (string.IsNullOrEmpty(fileName))
// {
// path = Path.GetTempPath() + "MarkdownPadPreview.html";
// }
// else
// {
// string directoryName = Path.GetDirectoryName(fileName);
// string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
// path = directoryName + @"\" + fileNameWithoutExtension + "-MarkdownPadPreview.html";
// }
// try
// {
// File.WriteAllText(path, AppendValidHTMLTags(markdown, fileName, false));
// }
// catch (Exception exception)
// {
// MessageBox.Show("An error occurred while trying to save the preview document:\n\n" + exception.Message + "\n\nPlease try again. If the problem persists, please report it under Help --> Report a Bug.", "Error Saving Preview Document", MessageBoxButton.OK, MessageBoxImage.Exclamation);
// }
// path.StartProcess();
// }
public static string WriteDocumentToLocalFile(string fileName, string document, string path)
{
string str = DateTime.Now.ToString("yyyyMMddHmmss");
if (string.IsNullOrEmpty(fileName))
{
fileName = "MarkdownPad.txt";
}
string str2 = path;
path = str2 + @"\" + Path.GetFileNameWithoutExtension(fileName) + "_backup_" + str + Path.GetExtension(fileName);
StreamWriter writer = new StreamWriter(path, true);
writer.WriteLine(string.Concat(new object[] { "MarkdownPad Document Backup: ", fileName, " at ", DateTime.Now }));
writer.WriteLine("-------------------------------" + Environment.NewLine);
writer.Write(document);
writer.Close();
return path;
}
}
}