-
Notifications
You must be signed in to change notification settings - Fork 0
/
veAutocorrect.js
339 lines (311 loc) · 8.38 KB
/
veAutocorrect.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* Autocorrection features in Visual Editor.
*
* Polska instrukcja:
* https://pl.wikipedia.org/wiki/WP:NAC
*
* Version history and technical docs:
* https://github.com/Eccenux/veAutocorrect
*
* Authors:
* Maciej Nux Jaros, Schnark.
*
* Deployed with love using Wikiploy:
* [[Wikipedia:Wikiploy]]
*
* <nowiki>
*/
/*global mediaWiki, OO, ve*/
(function (mw) {
"use strict";
var version = '2.1.9';
/**
* Helpers for defining replacements.
*/
class Helpers {
/**
* p-starter
*
* Note! The paragraph is not closed, so that it can be used in `from`.
* E.g.: `from: p('=z+'),`
*
* Normally paragraphs should be closed (see h2).
*/
p(text) {
var textArray = text.split('');
return [{type: 'paragraph'}].concat(textArray);
}
/**
* Standard header.
*
* E.g.: `to: h2('See also'),`
*/
h2(text, skipParagraph) {
var head = [
{type: 'heading', attributes: {level: 2}},
...text.split(''),
{type: '/heading'},
];
var p = [
{type: 'paragraph'},
{type: '/paragraph'},
];
return skipParagraph ? head : head.concat(p);
}
/**
* Inline or block template.
*
* Note! Inline templates should be inside a paragraph.
* E.g.:
* ```
* tpl({
target: {
href: 'Szablon:Przypisy',
wt: 'Przypisy'
},
//params: {}
})
* ```
*/
tpl(template, block) {
var tplType = block ? 'mwTransclusionBlock' : 'mwTransclusionInline';
return [
{
type: tplType,
attributes: {
mw: {
parts: [ { template: template } ]
}
}
},
{ type: '/' + tplType },
];
}
}
/**
* Autocorrect class (export).
*/
var veNuxAutocorrect = {
version: version,
helpers: new Helpers(),
_ready: false,
_configs: [],
/**
* Add replacemnt rule.
*
* Examples in documentation.
* See also: `autoCorrectFromTo`.
*/
addReplacements: function(config) {
if (this._ready) {
this._run(config);
} else {
this._configs.push(config);
}
},
/**
* Alias `addReplacements`.
*/
add: function(config) {
this.addReplacements(config);
},
_run: function(config) {
autoCorrectFromTo(config.from, config.to);
},
_onReady: function() {
for (var i = 0; i < this._configs.length; i++) {
this._run(this._configs[i]);
}
this._configs = [];
this._ready = true;
mw.hook('userjs.veNuxAutocorrect.ready').fire(veNuxAutocorrect, veNuxAutocorrect.helpers);
},
};
mw.hook('userjs.veNuxAutocorrect').fire(veNuxAutocorrect, veNuxAutocorrect.helpers);
// shorthand for helpers
var h = veNuxAutocorrect.helpers;
// Usage info helper
// This is only for quick death, expected to be re-checked on page reload e.g. from wiki-code editor.
var usageInfoDone = false;
/**
* Append gadget usage info.
*
* Adds documentation page shortcut in summary.
*/
function appendUsageInfo() {
// quick death
if (usageInfoDone) {
//console.log('[NAC] appendUsageInfo: quick death');
return;
}
if (!(ve.init && typeof ve.init.target === 'object')) {
//console.log('[NAC] appendUsageInfo: no target');
return;
}
var target = ve.init.target;
var myInfo = "[[WP:NAC]]";
// append if not already
if (typeof target.initialEditSummary === 'string' && target.initialEditSummary.length) {
//console.log('[NAC] appendUsageInfo: append?');
if (target.initialEditSummary.indexOf(myInfo) < 0) {
//console.log('[NAC] appendUsageInfo: append');
target.initialEditSummary += ", " + myInfo;
}
// create when empty
} else {
//console.log('[NAC] appendUsageInfo: create info');
target.initialEditSummary = myInfo;
}
usageInfoDone = true;
}
/**
* AutoCorrectCommand.
*
* Command to replace selected content and place the cursor after it.
*
* inherit from ve.ui.Command, and override execute
*/
function AutoCorrectCommand (name, content) {
AutoCorrectCommand.parent.call(this, name);
this.content = content;
}
/**
* ReSequence.
*
* like ve.ui.Sequence, with the difference that for regular expressions
* of the form /foo(bar)/ only the parentheses is used as Range, not the whole expression
*/
function ReSequence () {
ReSequence.parent.apply(this, arguments);
}
var customVeClassesReady = false;
/**
* Init classes when ready ve.ui is ready.
*
* @returns true if already there.
*/
function initCustomVeClasses() {
// avoid re-run when VE re-opened without reloading page
if (customVeClassesReady) {
return true;
}
customVeClassesReady = true;
OO.inheritClass(AutoCorrectCommand, ve.ui.Command);
AutoCorrectCommand.prototype.execute = function (surface) {
surface.getModel().getFragment().insertContent(this.content).collapseToEnd().select();
appendUsageInfo();
return true;
};
OO.inheritClass(ReSequence, ve.ui.Sequence);
ReSequence.prototype.match = function (data, offset, plaintext) {
var execResult;
if (this.data instanceof RegExp) {
execResult = this.data.exec(plaintext);
return execResult && new ve.Range(offset - execResult[1].length, offset);
}
return ReSequence.parent.prototype.match.apply(this, arguments);
};
}
/**
* autoCorrectFromTo.
*
* when the user enters "from" change it to "to"
* @param from can be a string, a regular expression of the form /foo(bar)/ or an array of data
* @param to can be a string or an array of data
*/
function autoCorrectFromTo (from, to) {
//get a unique name, we use it for both the command and the sequnce
var name = 'nuxAutoCorrectCommand-' + (autoCorrectCommandCount++);
//create and register the command
ve.ui.commandRegistry.register(
new AutoCorrectCommand(name, to)
);
//let the surface know that there is a new command that can be executed
ve.init.target.getSurface().commands.push(name);
//create and register the sequence
ve.ui.sequenceRegistry.register(
new ReSequence(/*sequence*/ name, /*command*/ name, from, 0, true)
);
}
var autoCorrectCommandCount = 0;
/**
* Init commands.
*/
function initAutoCorrect (lang, wiki) {
//define what should be autocorrected
//for all languages and projects
autoCorrectFromTo('--', '–');
autoCorrectFromTo('–-', '—');
autoCorrectFromTo('...', '…');
autoCorrectFromTo('<<', '«');
autoCorrectFromTo('>>', '»');
autoCorrectFromTo('->', '→');
autoCorrectFromTo(/(?:^|[^\d])(1\/2 )$/, '½ ');
autoCorrectFromTo(/(?:^|[^\d])(1\/4 )$/, '¼ ');
autoCorrectFromTo(/(?:^|[^\d])(3\/4 )$/, '¾ ');
autoCorrectFromTo('+-', '±');
/*
autoCorrectFromTo(/\d(')/, '′');
autoCorrectFromTo(/\D(')/, '’');
autoCorrectFromTo(/\d(")/, '″');
*/
//depending on the content language
switch (lang) {
case 'de':
autoCorrectFromTo(/(?:^|[( \n])(")$/, '„');
autoCorrectFromTo(/[^\d( \n](")$/, '“');
break;
// disabled per en.wiki policies [[:en:MOS:PUNCT]]
/*
case 'en':
autoCorrectFromTo(/(?:^|[( \n])(")$/, '“');
autoCorrectFromTo(/[^\d( \n](")$/, '”');
break;
*/
case 'pl':
autoCorrectFromTo(/(?:^|[( \n])(")$/, '„');
autoCorrectFromTo(/[^\d( \n](")$/, '”');
break;
}
//depending on the wiki
/*jshint onecase: true*/
switch (wiki) {
case 'dewiki':
autoCorrectFromTo([{type: 'paragraph'}, '=', 'w'], [
{type: 'heading', attributes: {level: 2}},
'W', 'e', 'b', 'l', 'i', 'n', 'k', 's',
{type: '/heading'},
{type: 'paragraph'}
]);
break;
case 'plwiki':
var iso = (new Date()).toISOString();
var ym = iso.substr(0,7);
autoCorrectFromTo('{fd',
h.tpl({
target: {
href: 'Szablon:Fakt',
wt: 'fakt'
},
params: {
'data': {
wt: ym
}
}
})
);
break;
}
// run custom commands
veNuxAutocorrect._onReady();
}
//we just need to run once the editor is ready
//don't care about dependencies, they should be fine when activation is complete
mw.hook('ve.activationComplete').add(function () {
var alreadyDone = initCustomVeClasses();
if (!alreadyDone) {
initAutoCorrect(mw.config.get('wgContentLanguage'), mw.config.get('wgDBname'));
}
});
})(mediaWiki);
//</nowiki>