-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpunctuationFixer.js
259 lines (235 loc) · 7.71 KB
/
punctuationFixer.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
// https://akelpad.sourceforge.net/forum/viewtopic.php?p=9926#p9926
// https://infocatcher.ucoz.net/js/akelpad_scripts/punctuationFixer.js
// https://github.com/Infocatcher/AkelPad_scripts/blob/master/punctuationFixer.js
// (c) Infocatcher 2008-2009, 2014
// Version: 0.0.3.1 - 2014-10-18
// Author: Infocatcher
//// Tries to fix some punctuation issues
//== Settings
var fixerDash = "–";
var fixerForceUpperCase = true;
// Use
// Call("Scripts::Main", 1, "punctuationFixer.js", "-ignorecase", 0)
// to override this setting
var fixerCommas = ["«", "»"]; // var fixerCommas = null;
var fixerSaveSelPos = true;
//== End of settings
if(WScript.Arguments.length && WScript.Arguments(0) == "-ignorecase")
fixerForceUpperCase = false;
//var AkelPad = new ActiveXObject("AkelPad.document");
var hMainWnd = AkelPad.GetMainWnd();
var hWndEdit = AkelPad.GetEditWnd();
var oSys = AkelPad.SystemFunction();
function PunctuationFixer(dash, forceUpper, commas) {
this.dash = dash || "-";
this.forceUpper = forceUpper;
this.convCommas = commas instanceof Array;
if(this.convCommas) {
this.openComma = commas[0];
this.closeComma = commas[1];
}
}
PunctuationFixer.prototype = {
notConv: [],
subst: "",
init: function() { // Get random string for replacing exclusions
function rnd() {
return Math.random().toString().substr(2);
}
this.subst = "_" + rnd() + rnd() + "_";
},
getSubst: function(n) {
return this.subst + n + "_";
},
convert: function(txt) {
if(!txt)
return "";
this.init();
var _this = this;
//== Exclusions:
var excCnt = -1;
var subst;
// URLs like http://example.com/
this.notConv[++excCnt] = [], subst = this.getSubst(excCnt);
txt = txt.replace(
/[a-z]+:\/\/+([^\/\\.\s'"<>]+[.\/])+[^\/\\.\s'"<>]+/ig,
function() {
var a = arguments;
_this.notConv[excCnt].push(a[0]);
return subst;
}
);
// Time "10:43:01"
this.notConv[++excCnt] = [], subst = this.getSubst(excCnt);
txt = txt.replace(
/(^|\D)([0-2]?\d:[0-6]?\d(:[0-6]?\d)?)(\D|$)/mg,
function() {
var a = arguments;
_this.notConv[excCnt].push(a[2]);
return a[1] + subst + a[4];
}
);
// Dates "19.03.2009"
this.notConv[++excCnt] = [], subst = this.getSubst(excCnt);
txt = txt.replace(
/(^|\D)([0-3]?\d\.[0-1]\d\.\d\d(\d\d)?|\d\d(\d\d)?\.[0-1]\d\.[0-3]?\d)(\D|$)/mg,
function() {
var a = arguments;
_this.notConv[excCnt].push(a[2]);
return a[1] + subst + a[5];
}
);
// Numbers like "10.9" or "10,9"
this.notConv[++excCnt] = [], subst = this.getSubst(excCnt);
txt = txt.replace(
/(^|[^\d.,])(\d+[.,]\d+)([^\d.,]|$)/g,
function() {
var a = arguments;
_this.notConv[excCnt].push(a[2]);
return a[1] + subst + a[3];
}
);
// Smileys like ":)"
this.notConv[++excCnt] = [], subst = this.getSubst(excCnt);
txt = txt.replace(
/[ \t]*(:-?[D\(\)\\\/][\(\)]{0,2})[\(\)]*[ \t]*/g,
function() {
var a = arguments;
_this.notConv[excCnt].push(" " + a[1] + " ");
return subst;
}
);
// "->", "=>", "<=>"
this.notConv[++excCnt] = [], subst = this.getSubst(excCnt);
txt = txt.replace(
/<?[-=]+>/g,
function() {
var a = arguments;
_this.notConv[excCnt].push(a[0]);
return subst;
}
);
// Acronyms
this.notConv[++excCnt] = [], subst = this.getSubst(excCnt);
txt = txt.replace(
/([^а-яёa-z]([а-яёa-z]\.[ \t]*[а-яёa-z]\.|[а-яёa-z]{2}\.))/g,
function() {
var a = arguments;
_this.notConv[excCnt].push(a[1].replace(/[ \t]+/, " "));
return subst;
}
);
//== End of exclusions
// Del spaces:
txt = txt.replace(/(\S)([ \t])[ \t]+/g, "$1$2"); // "\t " -> "\t", " \t\t" -> " "
// Fix punctuation:
txt = txt
.replace(/((\) ?){1,3})(\) ?)*/g, "$1 ") // ")))))" -> ")))"
.replace(/[\t ]*([.?!])([^"])/g, "$1 $2") // " ! ! ! " -> "! ! ! "
.replace(/[\t ]*([:,;])[\t ]*/g, "$1 ") // " , " -> ", "
.replace(/([,;:]) ?([,;:] ?)+/g, "$1 ") // ",, ,, " -> ", "
.replace(
/(([.?!] *){1,3})([.?!] *)*(.|$)/mg, // "! !! ! " -> "!!! "
function($0, $1, $2, $3, $4) {
var noSp = $3 && /\S$/.test($3) && $4 == '"';
return $1.replace(/\s+/g, "") + (noSp ? "" : " ") + $4;
}
)
.replace(/([\(\{\[<])[\t ]+/, "$1")
.replace(/[\t ]+([\)\}\]>])/, "$1")
.replace(/[\t ]*([+=])[\t ]*/g, " $1 ");
// Fix brackets:
txt = txt
.replace(/(\S)[\t ]*\([\t ]*/g, "$1 (") // text(text -> text (text
.replace(/[\t ]*\)[\t ]*([^.!?:;,()])/g, ") $1"); // text)text -> text) text
// Fix lower case letters:
if(this.forceUpper) { // end. begin -> end. Begin
txt = txt.replace(
/(^|[^.][.?!] )([а-яёa-z])/mg,
function($0, $1, $2) { return $1 + $2.toUpperCase(); }
);
}
// Fix dashes:
txt = txt // " -", "- " -> " - "
.replace(/"[\t ]*-[\t ]*/g, "\" - ") // text"-text -> text" - text
.replace(/([^\s-])[\t ]*-[\t ]*"/g, "$1 - \"") // text-"text -> text - "text
.replace(/([^\s-])[\t ]*-\([\t ]*/g, "$1 - (") // text-(text -> text - (text
.replace(/([^\s-])[\t ]*\)-[\t ]*/g, "$1) - ") // text)-text -> text) - text
.replace(/([^\s-])[\t ]*-[\t ]+/g, "$1 - ") // text- text -> text - text
.replace(/([^\s-])[\t ]+-[\t ]*/g, "$1 - ") // text -text -> text - text
.replace(/^([\t ]*)-{1,3}[\t ]*/mg, "$1- ") // \n-text -> \n- text
.replace(/([^\s-])[\t ]*---?[\t ]*/g, "$1 - ") // text--text -> text - text
.replace(/ - /g, " " + this.dash + " ");
// Fix commas:
if(this.convCommas) {
txt = txt
.replace(/^[\t ]*"[\t ]*/mg, this.openComma) // \n" -> \n«
.replace(/[\t ]*"[\t ]*$/mg, this.closeComma) // "\n -> »\n
.replace(/([.?!])"(\s)/g, "$1" + this.closeComma + "$2") // 'text!" ' -> 'text!» '
.replace(/([а-яёa-z])"([-+\s.?!,;:\)\}\]\>]|$)/img, "$1" + this.closeComma + "$2") // text". -> text».
.replace(/[\t ]*"[\t ]*([-+.?!,;:\)\}\]\>]|$)/mg, this.closeComma + "$1") // ' " .' -> '».'
.replace(/(^|[\s\(\{\[<])"[\t ]*/mg, "$1" + this.openComma)
.replace(new RegExp(this.openComma + "[\\t ]+", "g"), this.openComma)
.replace(new RegExp("[\\t ]+" + this.closeComma, "g"), this.closeComma);
}
// Fix dashes (end):
txt = txt.replace(/- /g, this.dash + " ");
// Replace exclusions:
while(excCnt > -1) {
txt = txt.replace(
new RegExp(this.subst + excCnt + "_", "g"),
function() {
return _this.notConv[excCnt].shift();
}
);
excCnt--;
}
// Del spaces:
txt = txt
.replace(/(\S)([ \t])[ \t]+/g, "$1$2") // "\t " -> "\t", " \t\t" -> " "
.replace(/[ \t]+$/mg, ""); // "text \n" -> "text\n"
return txt;
}
};
if(hMainWnd && !AkelPad.GetEditReadOnly(hWndEdit)) {
var lpPoint = AkelPad.MemAlloc(8 /*sizeof(POINT)*/);
if(lpPoint) {
setRedraw(hWndEdit, false);
var selParams = getSelParams();
AkelPad.SendMessage(hWndEdit, 1245 /*EM_GETSCROLLPOS*/, 0, lpPoint);
// Get selection or all text and save fullTxt flag:
var txt = AkelPad.GetSelText();
var fullTxt = !txt;
if(fullTxt)
txt = AkelPad.SetSel(0, -1) || AkelPad.GetSelText();
var selStart = AkelPad.GetSelStart();
var err;
try {
var fixer = new PunctuationFixer(fixerDash, fixerForceUpperCase, fixerCommas);
txt = fixer.convert(txt);
AkelPad.ReplaceSel(txt);
if(!fixerSaveSelPos || !fullTxt)
selParams = [selStart, selStart + txt.length];
}
catch(e) {
err = e;
}
restoreSelParams(selParams);
AkelPad.SendMessage(hWndEdit, 1246 /*EM_SETSCROLLPOS*/, 0, lpPoint);
setRedraw(hWndEdit, true);
AkelPad.MemFree(lpPoint);
if(err)
throw err;
}
}
function setRedraw(hWnd, bRedraw) {
AkelPad.SendMessage(hWnd, 11 /*WM_SETREDRAW*/, bRedraw, 0);
if(bRedraw)
oSys.Call("user32::InvalidateRect", hWnd, 0, true);
}
function getSelParams() {
return [AkelPad.GetSelStart(), AkelPad.GetSelEnd()];
}
function restoreSelParams(selParams) {
AkelPad.SetSel(selParams[0], selParams[1]);
}