-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
216 lines (187 loc) · 9.48 KB
/
Copy pathProgram.cs
File metadata and controls
216 lines (187 loc) · 9.48 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
namespace HtmlClipboard
{
class Program
{
[STAThread]
static void Main(string[] args)
{
var plainText = string.Format("Thinking Clearly {0:HH:mm:ss}", DateTime.Now);
var htmlFormat = string.Format("<a href=\"http://labreuer.wordpress.com\">{0}</a>", plainText);
//var dataObject = new DataObject();
//dataObject.SetData(DataFormats.Html, htmlFormat);
//dataObject.SetData(DataFormats.Text, plainText);
//dataObject.SetData(DataFormats.UnicodeText, plainText);
//Clipboard.SetDataObject(dataObject);
var html = Clipboard.GetData(DataFormats.Html) as string;
if (args.FirstOrDefault() == "vc") {
Clipboard.SetText(GetStrippedHtmlFragmentInnerContent(html));
}
if (args.FirstOrDefault() == "v") {
Console.InputEncoding = Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine(html);
}
if (args.FirstOrDefault() == "u") {
var text = Clipboard.GetText();
AddBorder(args.Skip(1).ToArray(), html, text, "border-bottom");
}
if (args.FirstOrDefault() == "b")
{
var text = Clipboard.GetText();
// HACK: not having additional style parameter
// (reason being: want a better way of doing padding, which makes hilighting look good)
AddBorder(args.Skip(1).ToArray(), html, text, "padding: 0 2px; border");
} if (args.FirstOrDefault() == "a")
{
const string DisqusUserRegex = @"https://disqus\.com/by/([^/]+)/";
var text = Clipboard.GetText();
text = Regex.Replace(text, "#:~:text=.*", "");
if (Regex.IsMatch(text, @"^https?://en.wikipedia.org/wiki/"))
{
WikipediafyHyperlink(text);
}
else if (Regex.IsMatch(text, DisqusUserRegex))
{
var m = Regex.Match(text, DisqusUserRegex);
var s = string.Format("@{0}:disqus", m.Groups[1].Value);
ClipboardHelper.CopyToClipboard(s, s);
}
else if (html != null &&
Regex.IsMatch(text, @"^([\r\n]*[^\r\n]+){1,2}[\r\n]*$") &&
Regex.IsMatch(html, @"(?m)^SourceURL:(?!https://mail\.google\.com)"))
{
var url = Regex.Match(html, @"(?m)(?<=^SourceURL:)\S+").Value;
var inside = Regex.Replace(text.Trim(), @"\s*[\r\n]+\s*", ": ");
// System.Web.HttpUtility.UrlEncode()
var a = string.Format("<a href=\"{0}\">{1}</a>", url, inside);
ClipboardHelper.CopyToClipboard(a, inside);
}
else if (html != null && html.Contains("text-indent:-48pt"))
{
string s = "";
html = GetStrippedHtmlFragmentInnerContent(html);
html = html
.Replace("“", "“")
.Replace("”", "”")
.Replace("‘", "‘")
.Replace("’", "’")
.Replace("–", "–");
html = Regex.Replace(html, "([Tt]he )?<span style=\"font-variant:small-caps;\">Lord</span>", "YHWH");
html = Regex.Replace(html, "<span lang=\"en-US\">(.*?)</span>", "$1");
foreach (var m in Regex.Matches(html, @"<p(?:\s+style=""\s*(?<s>([^"";]+;?)+)\s*""[^>]+)?>\s*(?<t>.*?)\s*</p>", RegexOptions.Singleline).Cast<Match>())
{
var style = m.Groups["s"];
if (style.Success)
{
var d = Regex.Matches(style.Value, @"(?<k>[^:; ]+)\s*:\s*(?<v>[^:; ]+)").Cast<Match>()
.ToDictionary(mm => mm.Groups["k"].Value, mm => mm.Groups["v"].Value);
Func<string, int> get = key => d.ContainsKey(key) ? int.Parse(Regex.Match(d[key], "[0-9-]+").Value) : 0;
if (get("margin-top") > 0)
s += "\n";
if (get("margin-left") + get("text-indent") > 0)
s += " ";
}
s += m.Groups["t"].Value + "\n";
}
s = "<blockquote>" + s.Trim() + "</blockquote>";
ClipboardHelper.CopyToClipboard(s, s);
}
else
{
// Evernote ~6.7.5 killed this by stripping out <code>...</code>
//text = Regex.Replace(text, @"`(\w+.*?\w+)`", "<code>$1</code>");
text = Regex.Replace(text, @"`(\w.*?\w)`", "<span style='font-family: consolas,monospace;'>$1</span>");
if (Regex.IsMatch(text, @"<a href=""[^""]+"">((?!</a>).)+$"))
text += "</a>";
ClipboardHelper.CopyToClipboard(text, text);
}
}
if (args.FirstOrDefault() == "vv") {
var o = Clipboard.GetDataObject();
foreach (var f in o.GetFormats())
{
if (Array.IndexOf(new string[] { "Locale", "OEMText" }, f) >= 0)
continue;
Console.WriteLine("---------------------\n{0}:\n{1}\n", f, o.GetData(f));
}
}
if (args.FirstOrDefault() == "rtf") {
var rtf = Clipboard.GetData(DataFormats.Rtf) as string;
if (args.Length == 1)
Console.WriteLine(rtf);
else
System.IO.File.WriteAllText(args[1], rtf);
}
}
private static void AddBorder(string[] args, string html, string text, string borderStyleProperty)
{
Func<bool, string, bool> failIfTrue = (b, s) =>
{
if (b)
Console.WriteLine(s);
return b;
};
if (failIfTrue(string.IsNullOrEmpty(html), "No HTML found on clipboard."))
return;
var insideFragment = GetStrippedHtmlFragmentInnerContent(html);
if (failIfTrue(insideFragment == null, "No fragments found."))
return;
//Console.WriteLine(insideFragment);
var underlineOpenFormat = "<span style=\"{2}: {0}px solid {1}\">";
var size = args.Length >= 1 ? args[0] : "1";
var color = args.Length >= 2 ? args[1] : "red";
var underlineOpen = string.Format(underlineOpenFormat, size, color, borderStyleProperty);
var underlineClose = "</span>";
var underlineOpenRegex = string.Format(underlineOpenFormat, @"\d", @"\w+", borderStyleProperty);
var ropts = RegexOptions.IgnoreCase;
var alreadyUnderline = Regex.IsMatch(insideFragment, underlineOpenRegex, ropts);
var newFragment = alreadyUnderline
? Regex.Replace(insideFragment, underlineOpenRegex, underlineOpen, ropts)
: underlineOpen + insideFragment + underlineClose;
Console.WriteLine(newFragment);
if (failIfTrue(insideFragment.Equals(newFragment, StringComparison.OrdinalIgnoreCase), "already contains underline"))
return;
ClipboardHelper.CopyToClipboard(newFragment, text);
}
private static string GetStrippedHtmlFragmentInnerContent(string html)
{
// BUG: <span>foo</span><span>bar</span> -> foo</span><span>bar
Func<string, string> stripEmptySpans = null; stripEmptySpans = s =>
{
var span = Regex.Match(s, "^<span>(.*)</span>$", RegexOptions.Singleline);
return span.Success ? stripEmptySpans(span.Groups[1].Value) : s;
};
Func<string, string> replaceWithHtmlEntities = s =>
{
return s.Replace("\u00a0", " ");
};
var pattern = string.Format("{0}(.*){1}", ClipboardHelper.StartFragment, ClipboardHelper.EndFragment);
var m = Regex.Match(html, pattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);
return m.Success
? replaceWithHtmlEntities(stripEmptySpans(m.Groups[1].Value.Trim()))
: null;
}
private static void WikipediafyHyperlink(string url)
{
var m = Regex.Match(url, @"^https?://en.wikipedia.org/wiki/(.*)");
if (!m.Success)
throw new ArgumentException("Hyperlink was not a proper English wikipedia link.", "url");
url = url.Replace("'", "%27");
var href = string.Format("<a href='{0}'>WP: {1}</a>",
url, // no System.Net.WebUtility.UrlEncode?
//excluding System.Net.WebUtility.HtmlEncode(
System.Net.WebUtility.UrlDecode(
m.Groups[1].Value.Replace("_", " ")).Replace("#", " § "));
//.Replace("§", "§")
ClipboardHelper.CopyToClipboard(href, href);
}
}
}