-
Notifications
You must be signed in to change notification settings - Fork 16
/
exolve-player.html
582 lines (545 loc) · 17.5 KB
/
exolve-player.html
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<!--
MIT License
Copyright (c) 2023 Viresh Ratnakar
See the full license notice in exolve-m.js.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" type="text/css" href="exolve-m.css?v1.59"/>
<script src="exolve-m.js?v1.59"></script>
<script src="exolve-from-ipuz.js?v1.59"></script>
<script src="exolve-from-puz.js?v1.59"></script>
<script src="exolve-from-text.js?v1.59"></script>
<style>
.xlvp-wait {
cursor: wait;
}
#xlvp-player {
border: 1px solid black;
width: 100%;
box-sizing: border-box;
padding: 20px 4%;
margin: 20px 0;
font-size: 16px;
font-family: monospace;
background-image: linear-gradient(45deg, #f8f8f8 25%, transparent 25%), linear-gradient(-45deg, #f8f8f8 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #f8f8f8 75%), linear-gradient(-45deg, transparent 75%, #f8f8f8 75%);
background-size: 40px 40px;
background-position: 0 0, 0 20px, 20px -20px, -20px 0px;
}
summary {
background: gainsboro;
padding: 4px 6px;
border: 1px solid gray;
cursor: pointer;
font-size: 70%;
}
pre,
#xlvp-help {
border: 1px solid gray;
background: white;
padding: 8px;
}
#xlvp-text,
#xlvp-chooser {
margin-top: 10px;
}
.xlvp-hscroll {
overflow-x: auto;
}
.xlvp-chooser-entry {
background: white;
padding: 4px 0px 6px 12px;
border: 1px solid black;
cursor: pointer;
}
.xlvp-chooser-entry-sel {
background: lightgray !important;
}
.xlvp-chooser-entry:hover {
background: lightgreen;
border: 1px solid darkgreen;
}
.xlvp-list {
padding-left: 14px;
}
.xlvp-list li + li {
margin-top: 16px;
}
.xlvp-button,
.xlvp-button-working {
padding: 2px 6px;
}
.xlvp-button:hover:enabled {
background: lightgreen;
}
.xlvp-button-working {
background: lightpink;
font-weight: bold;
}
#xlvp-infer-msg {
color: red;
display: inline-block;
font-size: small;
font-style: italic;
margin: 4px 0 0 0;
}
#xlvp-multiple-msg {
margin-top: 16px;
background: lightgreen;
padding: 4px;
border-bottom: 1px solid black;
}
#xlvp-save,
#xlvp-view {
margin-top: 10px;
}
#xlvp-last-crossword {
float: right;
}
</style>
<title>Exolve Player</title>
</head>
<body>
<div id="xlvp-frame">
</div>
<div id="xlvp-player" ondrop="xlvpDrop(event);" ondragover="xlvpDrag(event);">
<button id="xlvp-last-crossword">
Hide current crossword
</button>
<h2><b>Exolve Player</b></h2>
<h3><b>Interactively solve a crossword</b></h3>
<ul class="xlvp-list">
<li><b>Drag and drop a crossword file in any of these formats: Exolve, ipuz, puz, text</b></li>
<li><b>Or:</b> <input id="xlvp-file" onchange="xlvpOnOpenFile();" type="file"></input></li>
<li><b>Or: Enter crossword text (selected from a PDF file, for example):</b>
<br>
<div class="xlvp-hscroll">
<textarea onchange="xlvpSaveState()"
placeholder='Paste just the clues from the text of a blocked, symmetric (UK-style) crossword here, including the lines that say "Across" and "Down", optionally preceded by Title/Byline/Preamble. Clues can span multiple lines, but if one of those lines starts with a number then please join it up with the previous line. Change the grid width and height below if different.'
id="xlvp-text" rows="8" cols="80"></textarea>
</div>
<div style="margin-top:8px">
<label for="xlvp-w">Width:</label> <input id="xlvp-w" name="xlvp-w" onchange="xlvpSaveState()" type="text" size="2" maxlength="2" min="3" max="21" value="15"/>
<label for="xlvp-h">Height:</label> <input id="xlvp-h" name="xlvp-h" onchange="xlvpSaveState()" type="text" size="2" maxlength="2" min="3" max="21" value="15"/>
<button id="xlvp-infer"
class="xlvp-button" onclick="xlvpOnText()">"Auto-grid": Try to infer the crossword from the above text</button>
<br>
<span id="xlvp-infer-msg"></span>
</div>
<div id="xlvp-chooser" class="xlvphscroll">
<h3 id="xlvp-multiple-msg">Multiple grids match the text, choose the right one:</h3>
<div id="xlvp-chooser-strip" class="xlvp-hscroll">
</div>
</div></li>
</ul>
<div>
<details>
<summary>Help</summary>
<div id="xlvp-help">
<p>
You can specify the crossword to be opened by choosing a file to open, or by dragging-and-dropping it onto the player.
You can also paste plain-text-formatted crosswords into the "Enter crossword text" area.
</p>
<p>
Once you open a crossword, your state for that puzzle is automatically saved (it will be restored even if you revisit
it later, after having worked on other crosswords).
</p>
<p>
Here are some details on the various supported formats:
<ul>
<li><b>Exolve:</b> You can open Exolve-formatted files with exolve-player. Such files have to contain
a crossword in the Exolve format between "exolve-begin" and "exolve-end" lines. They can be HTML
files too, as long as the Exolve-formatted data is present in them.</li>
<li><b>PUZ:</b> You can open <a href="https://code.google.com/archive/p/puz/wikis/FileFormat.wiki">.puz files</a> with exolve-player.
<li><b>IPUZ:</b> You can open <a href="http://www.ipuz.org/">.ipuz files</a> with exolve-player.
<li><b>Text:</b> You can open text files or paste the text into the "Enter crossword text" area and
click on the "Auto-grid" button. In either case, the software will try to figure out the grid
from the clue numbers and their enumerations. The text should include clues with enumerations,
along with the "Across" and "Down" heading lines. Additionally, the clues may be optionally
preceded by some text, the first line of which will be taken to be the title and the remaining
lines will be treated as the preamble (with some slight attempt at parsing out a setter name).
Grid inference works for standard blocked grids ("UK-style": any 2x2 area must have at least one
black cell) with 180-deg./90-deg./-90-deg./hor.-flip/ver.-flip symmetry. The width and the height
of the crossword should be set in the entry fields under the "Enter crossword text" area (for
pasted text as well as text files).</li>
</ul>
</p>
</div>
</details>
<details id="xlvp-view">
<summary>
Exolve-formatted specs for the current crossword
</summary>
<pre id="xlvp-src" class="xlvp-hscroll">
There isn't a current crossword. Perhaps open one?
</pre>
</details>
<button id="xlvp-save" class="xlvp-button" disabled onclick="xlvpSave()">
Save Exolve-converted crossword as exolve-player-output.html
</button>
</div>
<div id="xlvp-links" style="font-size:80%">
<p><b>Links</b></p>
<p><a href="https://github.com/viresh-ratnakar/exolve">Exolve (free, open source crossword-solving software used here) on GitHub</a></p>
<p><a href="https://github.com/viresh-ratnakar/exet">Exet (free, open source crossword-setting software) on GitHub</a></p>
<p><a href="https://exet.app/">Exet web app: start setting a crossword right away!</a></p>
<p><a href="https://gussalufz.com/">Cryptic crosswords by Gussalufz</a></p>
</div>
</div>
<script>
let xlvpFileName = '';
let xlvpData = '';
let xlvpFromTextActive = null;
const xlvpFrame = document.getElementById('xlvp-frame');
const xlvpSrc = document.getElementById('xlvp-src');
const xlvpSaveButton = document.getElementById('xlvp-save');
const xlvpFile = document.getElementById('xlvp-file');
const xlvpText = document.getElementById('xlvp-text');
const xlvpW = document.getElementById('xlvp-w');
const xlvpH = document.getElementById('xlvp-h');
const xlvpInfer = document.getElementById('xlvp-infer');
const xlvpInferHTML = xlvpInfer.innerHTML;
const xlvpInferMsg = document.getElementById('xlvp-infer-msg');
const xlvpChooser = document.getElementById('xlvp-chooser');
const xlvpChooserStrip = document.getElementById('xlvp-chooser-strip');
const xlvpLastCrossword = document.getElementById('xlvp-last-crossword');
let xlvpShowing = false;
let xlvpPuz = null;
const xlvpStateKey = '42-xlvp-player-state';
function xlvpSaveState() {
const state = {
'data': xlvpData,
'textW': xlvpW.value,
'textH': xlvpH.value,
'textData': xlvpText.value,
'saveDisabled': xlvpSaveButton.disabled,
};
try {
window.localStorage.setItem(xlvpStateKey, JSON.stringify(state));
} catch (err) {
console.log(err);
console.log('Could not save state—local storage is full.');
}
}
function xlvpRestoreState() {
let state = window.localStorage.getItem(xlvpStateKey);
if (!state) {
return;
}
try {
state = JSON.parse(state)
if (state.textW) xlvpW.value = state.textW;
if (state.textH) xlvpH.value = state.textH;
if (state.textData) xlvpText.value = state.textData;
if (state.data) {
xlvpShowExolveInner(state.data, !state.saveDisabled, true);
}
console.log('Successfully restored exolve-player state');
} catch (err) {
console.log(err);
console.log('Failed to parse exolve-player state');
}
}
function xlvpDeleteOld(hideChooser=true) {
if (xlvpPuz) {
xlvpPuz.destroy();
xlvpPuz = null;
}
xlvpFrame.innerHTML = '';
if (hideChooser) {
xlvpChooser.style.display = 'none';
xlvpChooserStrip.innerHTML = '';
}
xlvpLastCrossword.disabled = true;
webifi = document.getElementById('webifi-root');
if (webifi) {
webifi.remove();
}
}
function xlvpToggleMinimizeCurrent() {
if (xlvpShowing) {
xlvpLastCrossword.innerHTML = 'Show current crossword';
xlvpFrame.style.display = 'none';
} else {
xlvpLastCrossword.innerHTML = 'Hide current crossword';
xlvpFrame.style.display = '';
}
xlvpShowing = !xlvpShowing;
}
xlvpDeleteOld();
xlvpLastCrossword.disabled = true;
xlvpLastCrossword.addEventListener('click', xlvpToggleMinimizeCurrent);
xlvpRestoreState();
function xlvpShowExolve(specs, converted=false, hideChooser=true) {
const ret = xlvpShowExolveInner(specs, converted, hideChooser);
if (ret) {
xlvpSaveState();
}
return ret;
}
function xlvpShowExolveInner(specs, converted, hideChooser) {
let start = specs.indexOf('exolve-begin')
let end = specs.indexOf('exolve-end')
if (start < 0 || end < 0 || start >= end) {
return false;
}
while (start > 0 && specs.charAt(start - 1) == ' ') {
start--;
}
const data = specs.substring(start, end) + 'exolve-end';
xlvpDeleteOld(hideChooser);
try {
xlvpPuz = new Exolve(data, 'xlvp-frame', null, false);
/**
* If there are warnings, then when they are dismissed, we update and save
* xlvData to add the options to persist ignoring them.
*/
if (xlvpPuz.optionsForWarningFixes.length > 0) {
const b = document.getElementById(xlvpPuz.prefix + '-dismiss-warnings')
b.title = 'Click to dismiss warnings persistently (appropriate options ' +
'will be added to the Exolve specs for this crossword)';
b.addEventListener('click', (evt) => {
const end = xlvpData.indexOf('exolve-end');
console.assert(end >= 0);
const noWarningsSpecs = xlvpData.substring(0, end) +
'exolve-option: ' +
xlvpPuz.optionsForWarningFixes.join(' ') +
'\nexolve-end';
xlvpShowExolve(noWarningsSpecs, converted, hideChooser);
});
}
} catch (err) {
console.log(err);
console.log('new Exolve() failed');
return false;
}
if (!xlvpPuz.useWebifi) {
xlvpPuz.useWebifi = true;
try {
xlvpPuz.loadWebifi();
} catch (err) {
console.log('Disabling webifi because of error: ' + err);
xlvpPuz.useWebifi = false;
}
}
xlvpData = data;
xlvpSrc.innerText = xlvpData;
xlvpSaveButton.disabled = !converted;
xlvpFrame.style.display = '';
xlvpShowing = true;
xlvpLastCrossword.innerHTML = 'Hide current crossword';
xlvpLastCrossword.disabled = false;
xlvpFrame.scrollIntoView();
return true;
}
function xlvpSave() {
const data = '' +
'<!DOCTYPE html>\n' +
'<html lang="en">\n' +
'<head>\n' +
'<meta charset="utf-8"/>\n' +
'<meta name="viewport" content="width=device-width, initial-scale=1"/>\n' +
'<link rel="stylesheet" type="text/css" ' +
'href="https://viresh-ratnakar.github.io/exolve-m.css?v1.59"/>\n' +
'<script src="https://viresh-ratnakar.github.io/exolve-m.js?v1.59">' +
'<\/script>\n' +
'<\/head>\n' +
'<body>\n' +
'<script>\n' +
'createExolve(`' +
`\n${xlvpData}` +
'`);\n' +
'<\/script>\n' +
'<\/body>\n' +
'<\/html>\n';
const a = document.createElement("a");
a.style.display = "none";
document.body.appendChild(a);
a.href = window.URL.createObjectURL(
new Blob([data], {type: "text/html;charset=UTF-8"})
);
a.setAttribute("download", 'exolve-player-output.html');
a.click();
window.URL.revokeObjectURL(a.href);
document.body.removeChild(a);
}
function xlvpShowIpuz(specs, fname) {
let start = specs.indexOf('{')
let end = specs.lastIndexOf('}')
if (start < 0 || end < 0 || start >= end) {
return false;
}
const ipuzJSON = specs.substring(start, end) + '}';
try {
const ipuz = JSON.parse(ipuzJSON);
const exolve = exolveFromIpuz(ipuz, fname);
if (!exolve) {
return false;
}
return xlvpShowExolve(exolve, true);
} catch (err) {
console.log(err);
}
return false;
}
function xlvpShowPuz(buffer, fname) {
const exolve = exolveFromPuz(buffer, fname);
if (!exolve) {
return false;
}
return xlvpShowExolve(exolve, true);
}
function xlvpShow(buffer, fname) {
let utf8decoder = new TextDecoder();
specs = utf8decoder.decode(buffer);
if (xlvpShowExolve(specs)) {
return;
}
if (xlvpShowIpuz(specs, fname)) {
return;
}
if (xlvpShowPuz(buffer, fname)) {
return;
}
// This last one is async.
xlvpShowText(specs, fname);
}
function xlvpOpenFile(f) {
if (!f) {
return;
}
let fr = new FileReader();
fr.onload = function(){
xlvpShow(fr.result, xlvpFileName);
}
xlvpFileName = f.name;
fr.readAsArrayBuffer(f);
}
function xlvpOnOpenFile(ev) {
let f = xlvpFile.files[0];
xlvpFile.value = '';
xlvpOpenFile(f);
}
function xlvpDrop(ev) {
ev.preventDefault();
let f = null;
if (ev.dataTransfer.items && ev.dataTransfer.items.length > 0) {
f = ev.dataTransfer.items[0].getAsFile();
} else if (ev.dataTransfer.files.length > 0) {
f = ev.dataTransfer.files[0];
}
if (!f) {
return;
}
xlvpOpenFile(f);
}
function xlvpDrag(ev) {
ev.preventDefault();
}
function xlvpOnText() {
if (xlvpFromTextActive) {
/* Cancel as been clicked */
xlvpEndFromTextWorker();
return;
}
xlvpShowText(xlvpText.value);
}
function xlvpEndFromTextWorker() {
xlvpInfer.innerHTML = xlvpInferHTML;
xlvpInfer.className = 'xlvp-button';
xlvpInferMsg.innerHTML = '';
if (xlvpFromTextActive) {
xlvpFromTextActive.worker.terminate();
xlvpFromTextActive = null;
}
}
function xlvpShowText(text, fname='provided text') {
if (xlvpFromTextActive) {
/* New text data, kill the old worker if active */
xlvpEndFromTextWorker();
}
xlvpInfer.className = 'xlvp-button-working';
xlvpInfer.innerHTML = 'Working! [click to cancel]';
const w = xlvpW.value;
const h = xlvpH.value;
const worker = exolveFromText(w, h, text, fname);
if (typeof worker === 'string') {
/* There was some error. */
const alertMsg = 'Error parsing a ' + w + 'x' + h +
' grid from ' + fname + ': ' + worker;
alert(alertMsg);
xlvpEndFromTextWorker();
return;
}
xlvpFromTextActive = {
worker: worker,
w: w,
h: h,
text: text,
fname: fname,
};
worker.onmessage = (e) => {
xlvpHandleFromTextMessage(e.data);
};
}
function xlvpHandleFromTextMessage(message) {
if (!xlvpFromTextActive) {
return;
}
if (message.update) {
xlvpInferMsg.innerHTML = message.update;
return;
}
const saved = xlvpFromTextActive;
xlvpEndFromTextWorker();
const matches = message.results || [];
if (matches.length == 0) {
alert('Could not parse or infer a ' + saved.w + 'x' + saved.h +
' grid from ' + saved.fname);
return;
}
console.log('Was able to infer ' + message.results.length + ' unique grid(s)');
xlvpDeleteOld();
if (matches.length > 1) {
let html = '<table><tr>';
for (let i = 0; i < matches.length; i++) {
html += `<td id="xlvp-chooser-${i}" class="xlvp-chooser-entry"></td>`;
}
html += '</tr></table>'
xlvpChooserStrip.innerHTML = html;
for (let i = 0; i < matches.length; i++) {
const smallSpecs = `
exolve-begin
exolve-width: ${saved.w}
exolve-height: ${saved.h}
exolve-grid:
${matches[i].gridSpecLines}
exolve-end`;
const smallXlv = new Exolve(smallSpecs, `xlvp-chooser-${i}`, function(puz) {
document.getElementById(puz.prefix + '-controls-etc').style.display = 'none';
document.getElementById(puz.prefix + '-clear-area').style.display = 'none';
}, false, 0, 150, false);
const chooser = document.getElementById(`xlvp-chooser-${i}`);
chooser.addEventListener('click', () => {
xlvpShowExolve(matches[i].exolve, true, false);
chooser.classList.add('xlvp-chooser-entry-sel');
for (let j = 0; j < matches.length; j++) {
if (j == i) {
continue;
}
document.getElementById(`xlvp-chooser-${j}`).className = 'xlvp-chooser-entry';
}
});
}
xlvpChooser.style.display = '';
} else {
const spec = matches[0].exolve;
xlvpShowExolve(spec, true);
}
}
</script>
</body>
</html>