-
Notifications
You must be signed in to change notification settings - Fork 0
/
breakParagraphs.js
177 lines (160 loc) · 4.67 KB
/
breakParagraphs.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
// WARNING: This function overwrites the work's "lines" file.
function breakParagraphs () {
if (!Array.isArray(lines) || lines.length < 1) {
console.error('Invalid lines. Expected an array of strings. Received:', lines);
return;
}
let breakerInd, capInd, exceptionInd, fragInd, frags, lineInd;
const caps = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
const exceptions = [
'!” I ',
'0. ',
'1. ',
'2. ',
'3. ',
'4. ',
'5. ',
'6. ',
'7. ',
'8. ',
'9. ',
'?” I ',
'Adm. ',
'ADM. ',
'Capt. ',
'CAPT. ',
'Cmdr. ',
'CMDR. ',
'Col. ',
'COL. ',
'Dr. ',
'DR. ',
'Gen. ',
'GEN. ',
'JR. ',
'Jr. ',
'Lt. ',
'LT. ',
'Maj. ',
'MAJ. ',
'Mr. ',
'MR. ',
'Mrs. ',
'MRS. ',
'Ms. ',
'MS. ',
'No. ',
'NO. ',
'Prof. ',
'PROF. ',
'Rev. ',
'REV. ',
'Sgt. ',
'SGT. ',
'St. ',
'ST. ',
];
const rawBreakers = [
'! ',
'! (',
'! ‘',
'! ’',
'! “',
'! “‘',
'! “’',
'!’ ',
'!’ ‘',
'!’ “',
'!’” ',
'!” ',
'!” ‘',
'!” ’',
'!” “',
'. ',
'. (',
'. ‘',
'. ’',
'. “',
'. “‘',
'. “’',
'.’ ',
'.’ ‘',
'.’ “',
'.’” ',
'.” ',
'.” ‘',
'.” ’',
'.” “',
'? ',
'? (',
'? ‘',
'? ’',
'? “',
'? “‘',
'? “’',
'?’ ',
'?’ ‘',
'?’ “',
'?’” ',
'?” ',
'?” ‘',
'?” ’',
'?” “',
];
const breakers = [];
// Add spaced double capital initials (eg: `A. A.`) to exceptions array.
for (let i = 0; i < caps.length; i++) {
for (let j = 0; j < caps.length; j++) {
exceptions.push(`${caps[i]}. ${caps[j]}.`);
}
}
// Build `breakers` array containing every raw breaker suffixed by every capital letter.
for (breakerInd = 0; breakerInd < rawBreakers.length; breakerInd++) {
for (capInd = 0; capInd < caps.length; capInd++) {
breakers.push(rawBreakers[breakerInd].concat(caps[capInd]));
}
}
// Search all lines.
for (lineInd = 0; lineInd < lines.length; lineInd++) {
// Search each line for exceptions. If found, replace space in exception with non-breaking space, to prevent later break.
for (exceptionInd = 0; exceptionInd < exceptions.length; exceptionInd++) {
if (lines[lineInd].includes(exceptions[exceptionInd])) {
lines[lineInd] = lines[lineInd].replaceAll(exceptions[exceptionInd], exceptions[exceptionInd].replace(' ', ' '));
}
}
// Search each line for breakers. If found, create array of fragments split by breaker.
for (breakerInd = 0; breakerInd < breakers.length; breakerInd++) {
if (lines[lineInd].includes(breakers[breakerInd])) {
frags = lines[lineInd].split(breakers[breakerInd]);
// Split removes breaker. Re-add it to fragments.
for (fragInd = 0; fragInd < frags.length - 1; fragInd++) { // For every fragment but last...
frags[fragInd] += breakers[breakerInd].split(' ')[0]; // Concat first half of breaker back onto end of fragment.
frags[fragInd + 1] = breakers[breakerInd].split(' ')[1] + frags[fragInd + 1]; // Concat second half of breaker onto start of next fragment.
}
// Splice fragments back into lines.
lines.splice(...[lineInd, 1].concat(frags));
}
}
}
return lines;
}
console.log('process.argv:', process.argv);
const worksObject = require('./works');
const works = Object.keys(worksObject);
const work = process.argv[2];
const fs = require('fs');
if (!works.includes(work)) {
throw new Error(`Invalid work: ${work}`);
}
let lines = require(`./lines/${work}`);
lines = breakParagraphs();
lines.forEach(function (line, ind) {
lines[ind] = line.replace(/\n/g, '\\n'); // Preserve in-string newlines.
});
let data = `module.exports = [\n\'${lines.join('\',\n\'')}\',\n];\n`; // Create data string to write file.
fs.writeFile(`./lines/${work}.js`, data, function (error) {
if (error) {
throw error;
}
console.log('File saved.');
});