Repository navigation
Expand file tree
/
Copy pathDateFormatType.cs
More file actions
283 lines (231 loc) · 10.4 KB
/
Copy pathDateFormatType.cs
File metadata and controls
283 lines (231 loc) · 10.4 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace DateFormatGenerator
{
using static Program;
using static DateFormat;
public enum EscapeBehavior { EscapeFormats, EscapeWords, EscapeAllText}
public abstract class DateFormatType
{
public Dictionary<DateFormat, string> Formats;
public virtual string CompleteCodeSample (string format) => null;
public virtual IEnumerable<string> CodeSamples(string format) { yield break; }
// Escaping of raw text before checking if it matches other formats or needs to be escaped further.
public virtual string InitialEscape (string rawText) => rawText;
public virtual bool MustEscapeWord (string word) => Formats.Values.Any(f => word.Contains(f));
public abstract string EscapeFormatPart(string formatPart);
public abstract EscapeBehavior EscapeBehavior { get; }
public virtual string GenerateFormatString (string input)
{
Regex formatSpecifier = new Regex(@"(\[\[[a-zA-Z]+\]\])");
string[] split = formatSpecifier.Split(input);
List<(string str, bool isRaw)> segments = split.Select(v =>
formatSpecifier.IsMatch(v) ? (Formats[Enum.Parse<DateFormat>(Regex.Replace(v, @"\[\[([a-zA-Z]+)\]\]", "$1"))], isRaw: false) : (v, isRaw: true)
).ToList();
string outP = "";
string splitRegex = EscapeBehavior == EscapeBehavior.EscapeWords ?
"([^a-zA-Z" +
string.Concat(
(from format in Formats.Values
from @char in format
where !((@char >= 'a' && @char <= 'z') || (@char >= 'A' && @char <= 'Z')) // where it's not an ascii letter
orderby @char ascending
select @char
).Distinct().Select(c => (c == '-' || c == '^' || c == '\\') ? ("\\" + c) : c.ToString())
) + "]+)" : null;
foreach (var (str, isRaw) in segments)
{
if (!isRaw) { outP += str; continue; }
switch (EscapeBehavior)
{
case EscapeBehavior.EscapeFormats:
string finalStr = InitialEscape(str);
foreach (var format in Formats.Values)
finalStr = finalStr.Replace(format, EscapeFormatPart(format));
outP += finalStr;
break;
case EscapeBehavior.EscapeWords:
foreach (string splitPart in Regex.Split(str, splitRegex))
{
string sPart = InitialEscape(splitPart);
if (MustEscapeWord(sPart))
sPart = EscapeFormatPart(sPart);
outP += sPart;
}
break;
case EscapeBehavior.EscapeAllText:
outP += EscapeFormatPart(str);
break;
}
}
return outP;
}
}
public class CSDateFormat : DateFormatType
{
public override IEnumerable<string> CodeSamples(string format)
{
yield return $"date.ToString({ToQuotedString(format)})";
yield return format.Contains('\\') ? $"date.ToString({ToVerbatimString(format)})" : "";
}
public override string InitialEscape(string rawText) => rawText.Replace(@"\", @"\\");
public override bool MustEscapeWord(string word) =>
Regex.IsMatch(word, @"[a-zA-Z""']");
public override string CompleteCodeSample(string format) =>
$@"using System;
DateTime date = DateTime.Now;
Console.WriteLine({CodeSamples(format).Where(s => s != "").Last()});";
// Escape with '' or "" or \
public override EscapeBehavior EscapeBehavior => EscapeBehavior.EscapeWords;
public override string EscapeFormatPart(string formatPart) => formatPart.Length == 1 ? ("\\" + formatPart) : ("'" + formatPart + "'");
}
public class JavaDateFormat : DateFormatType
{
// Escape with ''
public override IEnumerable<string> CodeSamples(string format)
{
yield return $"date.format(DateTimeFormatter.ofPattern({ToQuotedString(format)}))";
}
public override bool MustEscapeWord(string word) =>
Regex.IsMatch(word, "[a-zA-Z']");
public override string CompleteCodeSample(string format) =>
$@"import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {{
public static void main(String[] args) {{
LocalDateTime date = LocalDateTime.now();
System.out.println({CodeSamples(format).Where(s => s != "").Last()});
}}
}}";
public override EscapeBehavior EscapeBehavior => EscapeBehavior.EscapeWords;
public override string EscapeFormatPart(string formatPart) => "'" + formatPart + "'";
}
public class PHPDateFormat : DateFormatType
{
// Escape with \
public override IEnumerable<string> CodeSamples(string format)
{
yield return $"$date->format({ToQuotedString(format)})";
}
public override string CompleteCodeSample(string format) =>
$@"<?php
$date = new DateTime();
echo {CodeSamples(format).Where(s => s != "").Last()};
?>";
public override EscapeBehavior EscapeBehavior => EscapeBehavior.EscapeAllText;
public override string EscapeFormatPart(string formatPart) =>
string.Concat(
formatPart.Select(
c => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '\\' ? ("\\" + c) : c.ToString()
)
);
}
public class PythonDateFormat : DateFormatType
{
// Escape % with %%
public override IEnumerable<string> CodeSamples(string format)
{
yield return $"date.strftime({ToQuotedString(format)})";
}
public override string CompleteCodeSample(string format) =>
$@"from datetime import datetime;
date = datetime.now()
print({CodeSamples(format).Where(s => s != "").Last()})";
public override EscapeBehavior EscapeBehavior => EscapeBehavior.EscapeAllText;
public override string EscapeFormatPart(string formatPart) => formatPart.Replace("%", "%%");
}
public class LuxonDateFormat : DateFormatType
{
// Escape with ''
public override IEnumerable<string> CodeSamples(string format)
{
yield return $"date.toFormat({ToQuotedString(format)})";
yield return format.Contains("\"") ? $"date.toFormat({ToJSInterpolatedString(format)})" : "";
}
public override bool MustEscapeWord(string word) =>
Regex.IsMatch(word, @"[a-zA-Z']");
public override string CompleteCodeSample(string format) =>
$@"<script
src=""https://cdn.jsdelivr.net/npm/luxon@2.1.1/build/global/luxon.min.js""
integrity=""sha256-vSborW7X9FSJg4XLi1TpwZKTlbN2nP6lHhC0XCNrVwU=""
crossorigin=""anonymous""
></script>
<script>
const date = luxon.DateTime.now();
console.log({CodeSamples(format).Where(s => s != "").Last()});
</script>";
public override EscapeBehavior EscapeBehavior => EscapeBehavior.EscapeWords;
public override string EscapeFormatPart(string formatPart) => "'" + formatPart + "'";
}
public class CPPDateFormat : DateFormatType
{
// Escape % with %%
public override string CompleteCodeSample(string format) =>
$@"#include <iostream>
#include <iomanip>
#include <ctime>
int main()
{{
std::time_t now = std::time(nullptr);
std::tm date = *std::localtime(&now);
std::cout << {CodeSamples(format).Where(s => s != "").Last()};
}}";
public override IEnumerable<string> CodeSamples(string format)
{
yield return $"std::put_time(&date, {ToQuotedString(format)})";
yield return (format.Contains('\\') || format.Contains('"')) && !format.Contains('(') && !format.Contains(')') ? $@"std::put_time(&date, R""({format})""))" : "";
}
public override EscapeBehavior EscapeBehavior => EscapeBehavior.EscapeAllText;
public override string EscapeFormatPart(string formatPart) => formatPart.Replace("%", "%%");
}
public class MySQLDateFormat : DateFormatType
{
// Escape % with %%
public override string CompleteCodeSample(string format) =>
$"SELECT {SimpleDateFormat("NOW()", format)};";
public override IEnumerable<string> CodeSamples(string format)
{
yield return SimpleDateFormat("date", format);
}
static string SimpleDateFormat (string dateExpression, string format) => $"DATE_FORMAT({dateExpression}, {ToQuotedString(format)})";
public override EscapeBehavior EscapeBehavior => EscapeBehavior.EscapeAllText;
public override string EscapeFormatPart(string formatPart) => formatPart.Replace("%", "%%");
}
public class RubyDateFormat : DateFormatType
{
// Escape % with %%
public override string CompleteCodeSample(string format) =>
$@"require 'date'
date = DateTime.now()
puts {CodeSamples(format).Where(s => s != "").Last()}";
public override IEnumerable<string> CodeSamples(string format)
{
yield return $"date.strftime({ToQuotedString(format)})";
}
public override EscapeBehavior EscapeBehavior => EscapeBehavior.EscapeAllText;
public override string EscapeFormatPart(string formatPart) => formatPart.Replace("%", "%%");
}
public class SwiftDateFormat : DateFormatType
{
// Escape with '
public override IEnumerable<string> CodeSamples(string format)
{
yield return $@"let df = DateFormatter()
df.dateFormat = {ToQuotedString(format)}
let dateString = df.string(from: date)";
}
public override bool MustEscapeWord(string word) =>
Regex.IsMatch(word, @"[a-zA-Z']");
public override string CompleteCodeSample(string format) =>
$@"import Foundation
let date = Date()
{CodeSamples(format).FirstOrDefault()}
print(dateString)";
public override EscapeBehavior EscapeBehavior => EscapeBehavior.EscapeWords;
public override string EscapeFormatPart(string formatPart) => "'" + formatPart + "'";
}
}