-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcracita.js
More file actions
276 lines (224 loc) · 9.69 KB
/
Copy pathcracita.js
File metadata and controls
276 lines (224 loc) · 9.69 KB
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
import { byId, createStatusSetter, debounce } from './utils.js';
export function initCracita() {
const cracitaPage = byId('page-cracita');
if (!cracitaPage) return;
const els = {
input: byId('cracita-inputText'),
output: byId('cracita-outputText'),
loadBtn: byId('cracita-loadBtn'),
saveBtn: byId('cracita-saveBtn'),
copyBtn: byId('cracita-copyBtn'),
fileInput: byId('cracita-fileInput'),
status: byId('cracita-statusField'),
stats: byId('cracita-statsField'),
mode: byId('cracita-mode-select'),
regexControls: byId('cracita-regex-controls'),
regexInput: byId('cracita-regex-input')
};
const setStatus = createStatusSetter(els.status);
const isQuoteBoundary = (character) => character === '' || /[\s([\{<:;!?—–-]/.test(character);
const isWordCharacter = (character) => /[\p{L}\p{N}]/u.test(character);
const isLowercaseLetter = (character) => /\p{Ll}/u.test(character);
const getQuoteFamily = (character) => {
if (character === '"' || character === '“' || character === '”') return 'double';
if (character === "'" || character === '‘' || character === '’') return 'single';
return null;
};
const hasLaterQuote = (value, startIndex, family) => {
for (let index = startIndex; index < value.length; index += 1) {
const quoteFamily = getQuoteFamily(value[index]);
if (quoteFamily === family) {
return true;
}
}
return false;
};
const htmlParser = typeof DOMParser !== 'undefined' ? new DOMParser() : null;
const preservedHtmlTags = new Set(['SCRIPT', 'STYLE', 'TEXTAREA', 'PRE', 'CODE']);
const textNodeFilter = typeof NodeFilter !== 'undefined' ? NodeFilter.SHOW_TEXT : 4;
const createTypographicTransformer = () => {
const quoteState = {
insideDoubleQuote: false,
insideSingleQuote: false
};
return (text) => {
if (!text) return "";
const normalizeSmartQuotes = (value) => {
let result = "";
for (let index = 0; index < value.length;) {
const character = value[index];
const family = getQuoteFamily(character);
if (!family) {
result += character;
index += 1;
continue;
}
while (index < value.length && getQuoteFamily(value[index]) === family) {
index += 1;
}
const previousCharacter = result[result.length - 1] || '';
const nextCharacter = value[index] || '';
if (family === 'single' && isWordCharacter(previousCharacter) && isWordCharacter(nextCharacter)) {
result += '’';
continue;
}
if (family === 'single' && isQuoteBoundary(previousCharacter) && isLowercaseLetter(nextCharacter)) {
const looksLikeOpeningQuote = hasLaterQuote(value, index, family);
if (looksLikeOpeningQuote) {
const opensQuote = !quoteState.insideSingleQuote;
result += opensQuote ? '‘' : '’';
quoteState.insideSingleQuote = opensQuote;
} else {
result += '’';
}
continue;
}
if (family === 'single') {
const opensQuote = !quoteState.insideSingleQuote && isQuoteBoundary(previousCharacter);
result += opensQuote ? '‘' : '’';
quoteState.insideSingleQuote = opensQuote;
continue;
}
const opensQuote = !quoteState.insideDoubleQuote && isQuoteBoundary(previousCharacter);
result += opensQuote ? '“' : '”';
quoteState.insideDoubleQuote = opensQuote;
}
return result;
};
return normalizeSmartQuotes(text)
.replace(/---/g, '—')
.replace(/--/g, '—')
.replace(/\.\.\./g, '…')
.replace(/'(?=\d{2}s)/g, '’')
.replace(/((?:^|[\s(\[{>]))'/g, '$1‘')
.replace(/'/g, '’');
};
};
const applyTypographicRules = (text) => createTypographicTransformer()(text);
const applyTypographicRulesToHtml = (html) => {
if (!htmlParser) return applyTypographicRules(html);
const quoteTransformer = createTypographicTransformer();
const document = htmlParser.parseFromString(html, 'text/html');
const walker = document.createTreeWalker(document.body, textNodeFilter);
const textNodes = [];
while (walker.nextNode()) {
const node = walker.currentNode;
const parentTagName = node.parentElement?.tagName;
if (parentTagName && preservedHtmlTags.has(parentTagName)) {
continue;
}
textNodes.push(node);
}
for (const node of textNodes) {
node.textContent = quoteTransformer(node.textContent || '');
}
return document.body.innerHTML;
};
const convertText = (text) => {
const looksLikeHtml = /<\/?[a-z][\s\S]*>/i.test(text);
return looksLikeHtml ? applyTypographicRulesToHtml(text) : applyTypographicRules(text);
};
const performConversion = () => {
const mode = els.mode.value;
const input = els.input.value;
let output = "";
if (mode === 'Normal') {
output = convertText(input);
setStatus("Auto-converted");
} else if (mode === 'Regex') {
if (!els.regexInput.value) {
els.output.value = input;
setStatus("Waiting for pattern...");
return;
}
try {
const regex = new RegExp(els.regexInput.value, 'gs');
output = input.replace(regex, (match, ...args) => {
const captures = args.slice(0, -2);
if (captures.length >= 2) {
const pre = captures[0] || '';
const content = captures[1] || '';
const post = captures.slice(2).join('');
return `${pre}${convertText(content)}${post}`;
}
return match;
});
setStatus("Targeted conversion complete.");
} catch (e) {
output = input;
setStatus(`Regex error: ${e.message}`);
}
}
els.output.value = output;
localStorage.setItem('cracita-text', els.input.value);
};
const updateStats = () => {
const text = els.input.value;
const wordCount = (text.match(/\b[\w'-]+\b/gu) || []).length;
els.stats.textContent = `Words: ${wordCount} | Chars: ${text.length}`;
};
const onTextChange = debounce(() => {
performConversion();
updateStats();
}, 300);
els.mode.addEventListener('change', () => {
const isRegex = els.mode.value === 'Regex';
els.regexControls.classList.toggle('hidden', !isRegex);
els.regexControls.classList.toggle('flex', isRegex);
if (isRegex && !els.regexInput.value) {
els.regexInput.value = '("description":\\s*")(.+?)(")';
}
performConversion();
});
els.regexInput.addEventListener('input', onTextChange);
els.input.addEventListener('input', onTextChange);
els.loadBtn.addEventListener('click', () => els.fileInput.click());
els.saveBtn.addEventListener('click', () => {
const blob = new Blob([els.output.value], { type: "text/plain" });
if (typeof saveAs !== 'undefined') {
saveAs(blob, "converted_text.txt");
setStatus("File saved.");
} else {
console.error("FileSaver.js (saveAs) is missing.");
setStatus("Error: Save dependency missing.");
}
});
els.copyBtn.addEventListener('click', () => {
if (!els.output.value) return;
const originalLabel = els.copyBtn.textContent;
navigator.clipboard.writeText(els.output.value)
.then(() => {
setStatus("Copied to clipboard!");
els.copyBtn.textContent = "Copied!";
window.setTimeout(() => {
els.copyBtn.textContent = originalLabel;
}, 1200);
})
.catch(() => setStatus("Copy failed."));
});
els.fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (ev) => {
els.input.value = ev.target.result;
onTextChange();
setStatus(`Loaded: ${file.name}`);
els.fileInput.value = '';
};
reader.readAsText(file);
});
document.addEventListener('keydown', (e) => {
if (cracitaPage.classList.contains('hidden')) return;
if ((e.ctrlKey || e.metaKey) && ['s', 'o', 'c'].includes(e.key.toLowerCase())) {
e.preventDefault();
if (e.key.toLowerCase() === 's') els.saveBtn.click();
if (e.key.toLowerCase() === 'o') els.loadBtn.click();
}
});
const savedText = localStorage.getItem('cracita-text');
if (savedText) {
els.input.value = savedText;
onTextChange();
}
}