forked from cky/wmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wmd-plus.js
311 lines (310 loc) · 6.87 KB
/
wmd-plus.js
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
var Attacklab=Attacklab||{};
Attacklab.wmdPlus=function(){
this.symboltable;
var _1=top;
var _2=_1["Attacklab"];
var _3=_1["document"];
var _4=_1["RegExp"];
var _5=_1["navigator"];
var _6=_2.Util;
var _7=_2.Position;
var _8=_2.Command;
_8.doAutoindent=function(_9){
_9.before=_9.before.replace(/(\n|^)[ ]{0,3}([*+-]|\d+[.])[ \t]*\n$/,"\n\n");
_9.before=_9.before.replace(/(\n|^)[ ]{0,3}>[ \t]*\n$/,"\n\n");
_9.before=_9.before.replace(/(\n|^)[ \t]+\n$/,"\n\n");
if(/(\n|^)[ ]{0,3}([*+-]|\d+[.])[ \t]+.*\n$/.test(_9.before)){
if(_8.doList){
_8.doList(_9);
}
}
if(/(\n|^)[ ]{0,3}>[ \t]+.*\n$/.test(_9.before)){
if(_8.doBlockquote){
_8.doBlockquote(_9);
}
}
if(/(\n|^)(\t|[ ]{4,}).*\n$/.test(_9.before)){
if(_8.doCode){
_8.doCode(_9);
}
}
};
_8.doBlockquote=function(_a){
_a.selection=_a.selection.replace(/^(\n*)([^\r]+?)(\n*)$/,function(_b,_c,_d,_e){
_a.before+=_c;
_a.after=_e+_a.after;
return _d;
});
_a.before=_a.before.replace(/(>[ \t]*)$/,function(_f,_10){
_a.selection=_10+_a.selection;
return "";
});
_a.selection=_a.selection.replace(/^(\s|>)+$/,"");
_a.selection=_a.selection||"Blockquote";
if(_a.before){
_a.before=_a.before.replace(/\n?$/,"\n");
}
if(_a.after){
_a.after=_a.after.replace(/^\n?/,"\n");
}
_a.before=_a.before.replace(/(((\n|^)(\n[ \t]*)*>(.+\n)*.*)+(\n[ \t]*)*$)/,function(_11){
_a.startTag=_11;
return "";
});
_a.after=_a.after.replace(/^(((\n|^)(\n[ \t]*)*>(.+\n)*.*)+(\n[ \t]*)*)/,function(_12){
_a.endTag=_12;
return "";
});
var _13=function(_14){
var _15=_14?"> ":"";
if(_a.startTag){
_a.startTag=_a.startTag.replace(/\n((>|\s)*)\n$/,function(_16,_17){
return "\n"+_17.replace(/^[ ]{0,3}>?[ \t]*$/gm,_15)+"\n";
});
}
if(_a.endTag){
_a.endTag=_a.endTag.replace(/^\n((>|\s)*)\n/,function(_18,_19){
return "\n"+_19.replace(/^[ ]{0,3}>?[ \t]*$/gm,_15)+"\n";
});
}
};
if(/^(?![ ]{0,3}>)/m.test(_a.selection)){
_8.wrap(_a,_2.wmd_env.lineLength-2);
_a.selection=_a.selection.replace(/^/gm,"> ");
_13(true);
_a.skipLines();
}else{
_a.selection=_a.selection.replace(/^[ ]{0,3}> ?/gm,"");
_8.unwrap(_a);
_13(false);
if(!/^(\n|^)[ ]{0,3}>/.test(_a.selection)){
if(_a.startTag){
_a.startTag=_a.startTag.replace(/\n{0,2}$/,"\n\n");
}
}
if(!/(\n|^)[ ]{0,3}>.*$/.test(_a.selection)){
if(_a.endTag){
_a.endTag=_a.endTag.replace(/^\n{0,2}/,"\n\n");
}
}
}
if(!/\n/.test(_a.selection)){
_a.selection=_a.selection.replace(/^(> *)/,function(_1a,_1b){
_a.startTag+=_1b;
return "";
});
}
};
_8.doCode=function(_1c){
var _1d=/\S[ ]*$/.test(_1c.before);
var _1e=/^[ ]*\S/.test(_1c.after);
if((!_1e&&!_1d)||/\n/.test(_1c.selection)){
_1c.before=_1c.before.replace(/[ ]{4}$/,function(_1f){
_1c.selection=_1f+_1c.selection;
return "";
});
var _20=1;
var _21=1;
if(/\n(\t|[ ]{4,}).*\n$/.test(_1c.before)){
_20=0;
}
if(/^\n(\t|[ ]{4,})/.test(_1c.after)){
_21=0;
}
_1c.skipLines(_20,_21);
if(!_1c.selection){
_1c.startTag=" ";
_1c.selection="print(\"code sample\");";
return;
}
if(/^[ ]{0,3}\S/m.test(_1c.selection)){
_1c.selection=_1c.selection.replace(/^/gm," ");
}else{
_1c.selection=_1c.selection.replace(/^[ ]{4}/gm,"");
}
}else{
_1c.trimWhitespace();
_1c.findTags(/`/,/`/);
if(!_1c.startTag&&!_1c.endTag){
_1c.startTag=_1c.endTag="`";
if(!_1c.selection){
_1c.selection="print(\"code sample\");";
}
}else{
if(_1c.endTag&&!_1c.startTag){
_1c.before+=_1c.endTag;
_1c.endTag="";
}else{
_1c.startTag=_1c.endTag="";
}
}
}
};
_8.autoindent={};
_8.autoindent.textOp=_8.doAutoindent;
_8.blockquote={};
_8.blockquote.description="Blockquote <blockquote>";
_8.blockquote.image="images/blockquote.png";
_8.blockquote.key=".";
_8.blockquote.keyCode=190;
_8.blockquote.textOp=function(_22){
return _8.doBlockquote(_22);
};
_8.code={};
_8.code.description="Code Sample <pre><code>";
_8.code.image="images/code.png";
_8.code.key="k";
_8.code.textOp=_8.doCode;
_8.img={};
_8.img.description="Image <img>";
_8.img.image="images/img.png";
_8.img.key="g";
_8.img.textOp=function(_23,_24){
return _8.doLinkOrImage(_23,true,_24);
};
_8.doList=function(_25,_26){
var _27=/(([ ]{0,3}([*+-]|\d+[.])[ \t]+.*)(\n.+|\n{2,}([*+-].*|\d+[.])[ \t]+.*|\n{2,}[ \t]+\S.*)*)\n*/;
var _28="";
var _29=1;
var _2a=function(){
if(_26){
var _2b=" "+_29+". ";
_29++;
return _2b;
}
var _2c=_28||"-";
return " "+_2c+" ";
};
var _2d=function(_2e){
if(_26==undefined){
_26=/^\s*\d/.test(_2e);
}
_2e=_2e.replace(/^[ ]{0,3}([*+-]|\d+[.])\s/gm,function(_2f){
return _2a();
});
return _2e;
};
var _30=function(){
_31=_6.regexToString(_27);
_31.expression="^\n*"+_31.expression;
var _32=_6.stringToRegex(_31);
_25.after=_25.after.replace(_32,_2d);
};
_25.findTags(/(\n|^)*[ ]{0,3}([*+-]|\d+[.])\s+/,null);
var _33=/^\n/;
if(_25.before&&!/\n$/.test(_25.before)&&!_33.test(_25.startTag)){
_25.before+=_25.startTag;
_25.startTag="";
}
if(_25.startTag){
var _34=/\d+[.]/.test(_25.startTag);
_25.startTag="";
_25.selection=_25.selection.replace(/\n[ ]{4}/g,"\n");
_8.unwrap(_25);
_25.skipLines();
if(_34){
_30();
}
if(_26==_34){
return;
}
}
var _35=1;
var _31=_6.regexToString(_27);
_31.expression="(\\n|^)"+_31.expression+"$";
var _36=_6.stringToRegex(_31);
_25.before=_25.before.replace(_36,function(_37){
if(/^\s*([*+-])/.test(_37)){
_28=_4.$1;
}
_35=/[^\n]\n\n[^\n]/.test(_37)?1:0;
return _2d(_37);
});
if(!_25.selection){
_25.selection="List item";
}
var _38=_2a();
var _39=1;
_31=_6.regexToString(_27);
_31.expression="^\n*"+_31.expression;
_36=_6.stringToRegex(_31);
_25.after=_25.after.replace(_36,function(_3a){
_39=/[^\n]\n\n[^\n]/.test(_3a)?1:0;
return _2d(_3a);
});
_25.trimWhitespace(true);
_25.skipLines(_35,_39,true);
_25.startTag=_38;
var _3b=_38.replace(/./g," ");
_8.wrap(_25,_2.wmd_env.lineLength-_3b.length);
_25.selection=_25.selection.replace(/\n/g,"\n"+_3b);
};
_8.doHeading=function(_3c){
_3c.selection=_3c.selection.replace(/\s+/g," ");
_3c.selection=_3c.selection.replace(/(^\s+|\s+$)/g,"");
var _3d=0;
_3c.findTags(/#+[ ]*/,/[ ]*#+/);
if(/#+/.test(_3c.startTag)){
_3d=_4.lastMatch.length;
}
_3c.startTag=_3c.endTag="";
_3c.findTags(null,/\s?(-+|=+)/);
if(/=+/.test(_3c.endTag)){
_3d=1;
}
if(/-+/.test(_3c.endTag)){
_3d=2;
}
_3c.startTag=_3c.endTag="";
_3c.skipLines(1,1);
if(!_3c.selection){
_3c.startTag="## ";
_3c.selection="Heading";
_3c.endTag=" ##";
return;
}
var _3e=_3d==0?2:_3d-1;
if(_3e){
var _3f=_3e>=2?"-":"=";
var _40=_3c.selection.length;
if(_40>_2.wmd_env.lineLength){
_40=_2.wmd_env.lineLength;
}
_3c.endTag="\n";
while(_40--){
_3c.endTag+=_3f;
}
}
};
_8.ol={};
_8.ol.description="Numbered List <ol>";
_8.ol.image="images/ol.png";
_8.ol.key="o";
_8.ol.textOp=function(_41){
_8.doList(_41,true);
};
_8.ul={};
_8.ul.description="Bulleted List <ul>";
_8.ul.image="images/ul.png";
_8.ul.key="u";
_8.ul.textOp=function(_42){
_8.doList(_42,false);
};
_8.h1={};
_8.h1.description="Heading <h1>/<h2>";
_8.h1.image="images/h1.png";
_8.h1.key="h";
_8.h1.textOp=_8.doHeading;
_8.hr={};
_8.hr.description="Horizontal Rule <hr>";
_8.hr.image="images/hr.png";
_8.hr.key="r";
_8.hr.textOp=function(_43){
_43.startTag="----------\n";
_43.selection="";
_43.skipLines(2,1,true);
};
};
if(Attacklab.fileLoaded){
Attacklab.fileLoaded("wmd-plus.js");
}