-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.app
249 lines (201 loc) · 7.21 KB
/
string.app
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
module elib/elib-utils/string
type String{
substring(Int,Int):String
}
entity StringPair {
left :: String
right :: String
}
// does string have no other characters than whitespace?
function isEmptyString(x : String) : Bool {
return x == null || (x == "") || /[\n\t\r\ ]+/.replaceAll("", x) == "";
}
// remove trailing whitespace
function removeTrailingSpaces(x : String) : String {
return /^[\n\t\ ]+/.replaceAll("",/[\n\t\ ]+$/.replaceAll("",x));
}
function words(x : String) : List<String> {
return /[\t\n\ ]+/.split(removeTrailingSpaces(x));
}
function lines(x: String): List<String> {
return /[\n]/.split(x);
}
function text(lines: List<String>): String {
var t := "";
for(line: String in lines) { t := t + line + "\n"; }
return t;
}
function commentOut(comm: String, x: String): String { return commentOut(comm, "", x); }
function commentOut(commStart : String, commEnd : String, x : String) : String {
// When the text is empty or contains only whitespace,
// do not comment out anything and just return an empty string.
if (isEmptyString(x)) { return ""; }
return [commStart + line + commEnd | line in lines(x)].concat("\n");
}
function splitCommaSeparated(x : String) : List<String> {
return [removeTrailingSpaces(y).toLowerCase() | y : String in x.split(",") where !isEmptyString(y)];
}
function paragraphs(x : String) : List<String> {
return /[\n]([\t\ ]*[\n])+/.split(x);
}
function keyFromName(name : String) : String {
return (/(\ )+|(\/)+/.replaceAll("-",removeTrailingSpaces(name))).toLowerCase();
}
// replace multiple adjacent whitespace with single space
// remove trailing whitespace
function normalizeName(name : String) : String {
return /[\n\t\ ]+/.replaceAll(" ", removeTrailingSpaces(name));
}
function normalizeCiteKey(key : String) : String {
return makeValidKeyForURL(/[\/+\n\t\ ]+/.replaceAll("-", removeTrailingSpaces(key)));
}
function isValidKeyForURL(url : String) : Bool {
return /[a-zA-Z0-9:\-\.]+/.match(url);
}
function makeValidKeyForURL(url : String) : String {
return /[^a-zA-Z0-9:\-\.]/.replaceAll("", url);
}
function substring(x : String, i : Int, j : Int) : String {
return x.substring(i,j);
}
function isURL(x : String) : Bool {
return !isEmptyString(x) && x != "http://" && x != "http:///";
/* /"http:\/\/\*"/.match(x) && !isEmptyString(/"http:\/\/"/.replaceAll("",x)); */
}
function isAffiliation(x : String) : Bool {
return !isEmptyString(x);
}
function isnull(x : String) : String {
if(x == null) { return ""; } else { return x; }
}
function prefixSuffix(s : String, prefixLen : Int, suffixLen : Int) : String{
if(s.length() <= (prefixLen + suffixLen+4)){
return s;
} else {
return prefix(s,prefixLen) + "... " + suffix(s,suffixLen);
}
}
function abbreviate(s : String, length : Int) : String {
return abbreviate(s, length, true);
}
function abbreviate(s : String, length : Int, hardbreak : Bool) : String {
if(s.length() <= length) {
return s;
} else {
var abbr := if(hardbreak) prefix(s, length - 4) else /\S+$/.replaceAll("", prefix(s, length - 4));
return abbr + " ...";
}
}
function abbreviateNE(s : String, length : Int) : String {
if(s.length() <= length) {
return s;
} else {
return prefix(s, length);
}
}
function concat(xs: List<String>, sep: String): String {
if(xs.length == 0) {
return "";
} else {
var x := xs[0];
var i := 1;
while(i < xs.length) {
x := x + sep + xs[i];
i := i + 1;
}
return x;
}
}
type String{
substring(Int):String
substring(Int,Int):String
}
function prefix(s : String, length : Int) : String {
return s.substring(0, length);
}
function suffix(s : String, length : Int) : String {
var len := s.length();
return s.substring(s.length()-min(length, len), s.length());
}
function numPlural(num : Int, singular : String) : String{
return if(num == 1) "1 " + singular else num + " " + singular + "s";
}
function numPlural(num : Int, singular : String, plural : String) : String{
return if(num == 1) "1 " + singular else num + " " + plural;
}
function plural(singular : String, num : Int ) : String{
return if(num == 1) singular else singular + "s";
}
function plural(singular : String, plural : String, num : Int) : String{
return if(num == 1) singular else plural;
}
function capitalize(name : String) : String{
return if( name.length() > 0 ) name.substring( 0,1 ).toUpperCase() + name.substring( 1, name.length() ) else name;
}
function enumerate(strings : [String]) : String{
var len := strings.length;
var result := "";
for(i : Int from 0 to len){
if( len - i == 2 ){
result := result + strings[i] + " and ";
} else if( len - i > 2 ){
result := result + strings[i] + ", ";
} else {
result := result + strings[i];
}
}
return result;
}
section live preview on text
define inputWithPreview( txt : Ref<Text> ){
inputWithPreview(txt, false)
}
define inputWithPreview( txt : Ref<Text>, rawoutput : Bool ){
var owningEntity := txt.getEntity();
var ph := if(owningEntity.version < 1) "html-preview" else "ph-" + (if(owningEntity != null) owningEntity.id.toString() else "");
inputWithPreview( txt, rawoutput, ph )[all attributes]
}
define inputWithPreview( txt : Ref<Text>, rawoutput : Bool, ph : String){
action ignore-validation updatePreview(){
replace( ""+ph, livePreviewInternal(txt, rawoutput) );
rollback();
}
input( txt )[oninput:=updatePreview(), all attributes]
}
define livePreview( txt : Ref<Text> ){
livePreview(txt, false)
}
define livePreview( txt : Ref<Text>, rawoutput : Bool ){
var owningEntity := txt.getEntity();
var ph := if(owningEntity.version < 1) "html-preview" else "ph-" + (if(owningEntity != null) owningEntity.id.toString() else "");
livePreview(txt, rawoutput, ph)
}
define livePreview( txt : Ref<Text>, rawoutput : Bool, ph : String){
placeholder ""+ph{ livePreviewInternal( txt, rawoutput ) }
}
define ajax ignore-access-control livePreviewInternal( txt : Text, rawoutput : Bool ){
if( rawoutput ){
rawoutput( txt )
} else {
output( txt )
}
}
section view highlighted diff for 2 Strings
template diffView(name1 : String, name2 : String, text1 : String, text2 :String){
includeJS("https://cdn.jsdelivr.net/npm/[email protected]/dist/diff.min.js")
includeJS("https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html.min.js")
includeCSS("https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css")
div[id=id]
<script>
const text1 = "~(text1.escapeJavaScript())";
const text2 = "~(text2.escapeJavaScript())";
const name1 = "~(name1.escapeJavaScript())";
const name2 = "~(name2.escapeJavaScript())";
// Generate the diff
const diff = Diff.createTwoFilesPatch(name1, name2, text1, text2);
// Render the diff as HTML
const diffHtml = Diff2Html.html(diff, { drawFileList: false });
// Display the diff
document.getElementById("~id").innerHTML = diffHtml;
</script>
}