Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ryan778
GitHub Repository: Ryan778/Ryan778.github.io
Path: blob/master/sobvc/script.js
574 views
1
//Replacer script v2.0 - Sorting between "in quotes" and "not in quotes", past tense checking, and MLA/Regular formatting
2
var editor = CKEDITOR.replace( 't' );//Link the editor
3
var searchPastTense = true;
4
var edata;//Define the edata variable
5
var sobv = ["is", "be", "am", "are", "was", "were", "been", "has", "have", "had", "do", "does", "did", "can", "could", "shall", "should", "will", "would", "may", "might", "must", "being"];//The list of 22 SOBV
6
var wc = [" ", ".", ",", "\n", ":", "'", '"', "|", "!", "?", "<", ">", "&"];//Whitespace characters
7
for(var i = 0; i<sobv.length; i++){sobv[i]='\.'+sobv[i]+'\.'}//Add wildcards to every one
8
var other_words = ["due date", "sample text", "sample title", "template title"];//The other phrases you don't want
9
var hwords = sobv.concat(other_words);//Highlighted words
10
for(var i = 0; i<hwords.length; i++){
11
hwords[i] = new RegExp(hwords[i], "ig")}
12
var ptv = [/\swas\s/ig,/\sgot\s/ig,/\sdrank\s/ig,/\shad\s/ig,/\sdid\s/ig,/\sbought\s/ig,/\sate\s/ig, /\swent\s/ig, /\swere\s/ig, /\sran\s/ig, /\ssat\s/ig, /\sblew\s/ig, /\sflew\s/ig, /\S+ed\s/ig]; //Last item detects verbs ending in "ed" (usually past tense)
13
//Convert the list to rexexp
14
15
var stats = {
16
sobv: 0,//SOBV, excluding ones in quotes
17
sobv_all:0,//SOBV, including ones in quotes
18
pastTense: 0,
19
pastTense_all:0
20
};
21
22
function createString(x){
23
var text = "";
24
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
25
26
for( var i=0; i < (x!=undefined?x:16); i++ ){
27
text += possible.charAt(Math.floor(Math.random() * possible.length));}
28
return text;
29
}
30
31
var quotePos = {
32
start: [],
33
end: []
34
};
35
36
editor.addCommand("ct", {
37
exec: function() {
38
markText();
39
}
40
});//Register the markText function
41
42
editor.ui.addButton('checkTextButton', {
43
label: "Check Text",
44
command: 'ct',
45
toolbar: 'others',
46
icon: 'highlighter.png'
47
});//Add the "Mark Text" button
48
49
function checkForQuote(offset){
50
var res = 0;
51
var pos = 0;
52
if(quotePos.start[pos] > offset){
53
return false;
54
}
55
while(offset >= quotePos.start[pos] && pos <= quotePos.start.length){
56
res = pos;
57
pos ++;
58
}
59
if(offset <= quotePos.end[res]){
60
return true;
61
}
62
else{
63
return false;
64
}
65
}
66
67
function replacer(match, offset){
68
var lastChar = match.slice(match.length-1, match.length);
69
var firstChar = match.slice(0, 1);
70
if(other_words.indexOf(match.toLowerCase())!==-1){
71
stats.sobv_all++;
72
var inQuote = checkForQuote(offset);
73
if(!inQuote){stats.sobv++}
74
return firstChar+'<span style="background-color: #'+(inQuote?'fff9d5':'ffce00')+'>'+match+'</span>'+lastChar;
75
//Highlight String
76
}
77
else if(wc.indexOf(lastChar)!==-1 && wc.indexOf(firstChar)!==-1){
78
stats.sobv_all++;
79
var inQuote = checkForQuote(offset);
80
if(!inQuote){stats.sobv++}
81
return firstChar+'<span style="background-color: #'+(inQuote?'fff9d5':'ffce00')+'">'+match.slice(1,match.length-1)+'</span>'+lastChar;
82
//Highlight string
83
}
84
else{
85
return match;//False detection (ex. am in camera)
86
}
87
}
88
89
var gq = 0;
90
function grayQuotes(match, offset){
91
if(gq === 0){
92
gq = 1;
93
return '<span style="color: #888">'+match;
94
}
95
else{
96
gq = 0;
97
return match+'</span>';
98
}
99
}
100
101
function markQuotes(str){
102
quotePos = {start: [],end: []};
103
var newstr = str;
104
newstr = str.replace(/&ldquo;|&rdquo;/g,'&quot; ');
105
var slicePos = 0;
106
var t = 0;
107
while(typeof str === 'string' && newstr.indexOf('&quot;')!==-1){
108
if(t === 0){
109
t = 1;
110
quotePos.start[quotePos.start.length]=newstr.indexOf('&quot;')+slicePos;
111
}
112
else{
113
t = 0;
114
quotePos.end[quotePos.end.length]=newstr.indexOf('&quot;')+slicePos;
115
}
116
slicePos += (newstr.indexOf('&quot;')+1);
117
newstr = newstr.slice(newstr.indexOf('&quot;')+1);
118
}
119
}
120
121
var ptmarked = [];
122
function ptreplacer(match, offset){
123
let term = match.toLocaleLowerCase();
124
if(wc.indexOf(term.slice(-1)) !== -1){term = term.slice(0, term.length-1)}
125
if(['bed','red','need','reed', 'breed', 'seed','deed','feed'].indexOf(term)!==-1){return match}//False positives (not completely inclusive)
126
var lastChar = match.slice(match.length-1, match.length);
127
var firstChar = '';
128
if(wc.indexOf(match.slice(0, 1)) !== -1){firstChar = match.slice(0, 1); match = match.slice(1)}
129
var id = createString(6);
130
ptmarked[ptmarked.length] = offset;
131
var inQuote = checkForQuote(offset);
132
stats.pastTense_all++;
133
if(!inQuote){
134
stats.pastTense ++;
135
ptmarked[ptmarked.length] = id;
136
}
137
return firstChar+'<span data-pt'+id+' style="text-decoration:underline">'+match.slice(0, match.length-1)+'</span>'+lastChar;
138
};
139
140
function ptmark(){
141
for(var i = 0; i < ptmarked.length; i++){
142
var pos = edata.indexOf('pt'+ptmarked[i]);
143
var str = edata;
144
var foundStart = false;
145
var p = pos;
146
while (!foundStart && p > 0){
147
p -= 1;
148
var s = edata.slice(p, p+1);
149
if(s === '&quot;' || s === '.' || s === ',' || s === '!' || s === ';' || s === '?' || edata.slice(p-2, p+1)==='<p>'){
150
foundStart = true;
151
}
152
}
153
var foundEnd = false;
154
var p2 = pos;
155
while (!foundEnd && p2 < str.length){
156
p2 += 1;
157
var s = edata.slice(p2, p2+1);
158
if(s === '&quot;' || s === '.' || s === ',' || s === '!' || s === ';' || s === '?'){
159
foundEnd = true;
160
}
161
}
162
if(p !== -1){
163
var startpos = 0;
164
if(str.slice(0, 1) === ' '){startpos = 1}
165
edata = str.slice(startpos, p+1)+'<span style="background-color: #c4ddff">'+str.slice(p+1, p2+1)+'</span>'+edata.slice(p2+1);
166
}
167
}
168
ptmarked = [];
169
}
170
171
function markText(){
172
stats.sobv=0;
173
stats.sobv_all=0;
174
stats.pastTense=0;
175
stats.pastTense_all=0;
176
quotePos = {
177
start: [],
178
end: []
179
};
180
edata = editor.getData();//Get the text from the editor
181
edata = edata.replace(/&quot;|&ldquo;|&rdquo;/ig, grayQuotes);//Gray out quotes
182
if(searchPastTense){
183
for(var i = 0; i < ptv.length; i++){
184
markQuotes(edata);
185
console.log(ptv[i]);
186
edata = edata.replace(ptv[i],ptreplacer);
187
ptmark(edata);
188
}
189
}
190
for(var i = 0; i < hwords.length; i++){
191
markQuotes(edata);
192
//Mark position of quotes (not very efficient, but prevents inaccuracy from HTML characters)
193
edata = edata.replace(hwords[i], replacer);//Run the replacer on every word from the list
194
}
195
editor.setData(edata);//Set the data in the editor with the highlighted version
196
if(stats.sobv > 0){
197
alertify.success("Found "+stats.sobv+" State of Being Verbs (Gold)");
198
}
199
else{
200
alertify.success("No instances to highlight")
201
}
202
if(stats.pastTense > 0){
203
alertify.success('Found '+stats.pastTense+' Past Tense Sentences (Blue)')
204
}
205
//Send the results in a notification
206
}
207
208
function cl(){
209
alertify.alert('<p style="text-align:left;padding:0px 12px;margin-top:0"><b>Changelog</b><br><br>Relase v2.1 (11/13/17): </p><ul style="text-align:left"><li>Improved past tense checker (fewer false positives, more detections, minor improvements)</li><li>Improved site metadata/descriptions</li><li>Several minor aesthetic changes</li><li>Changed license to CC-BY-NC-SA 4.0</li></ul><p style="text-align:left;padding:12px;padding-bottom:0px;line-height:0">Release v2.0 (2/15/17): </p><ul style="text-align:left"><li>Better, simpler, updated editor</li><li>Now uses MLA styling: "12px" font (16px actual size) w/ Double Spacing (Customizable)</li><li>Results now differentiate between verbs in quotes and verbs outside of quotes</li><li>Basic past tense checker (looks for sentences written with past tense verbs)</li><li>Different colored highlighting for SOBV/Past Tense</li></ul>')
210
}
211
212
function formatEditor(v){
213
if(v === 0){
214
bfe0.disabled = true;
215
bfe1.disabled = false;
216
altStyle.innerHTML = '';
217
localStorage.sobvcFormat = '0';
218
}
219
else{
220
bfe0.disabled = false;
221
bfe1.disabled = true;
222
altStyle.innerHTML = ".cke_editable{font-family: 'Arial', sans-serif;font-size: 14px;line-height: 1.15;}";
223
localStorage.sobvcFormat = '1';
224
}
225
}
226
227
function togglePTVS(){
228
if(searchPastTense){
229
searchPastTense = false;
230
localStorage.sobvcSPT = '0';
231
bptv.style.backgroundColor = '#f9ffce';
232
bptv.innerHTML = 'Inactive';
233
}
234
else{
235
searchPastTense = true;
236
localStorage.sobvcSPT = '1';
237
bptv.style.backgroundColor = '#daffe7';
238
bptv.innerHTML = 'Active';
239
}
240
}
241
242
if(localStorage.sobvcFormat === '1'){formatEditor(1)}
243
if(localStorage.sobvcSPT === '0'){togglePTVS()}
244
245