-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate.html
More file actions
469 lines (400 loc) · 12.8 KB
/
Copy pathgenerate.html
File metadata and controls
469 lines (400 loc) · 12.8 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Crossword Embed Generator</title>
<link rel="icon" href="assets/logo.png" type="image/png">
<link rel="stylesheet" href="style.css">
<style>
/* Page-specific overrides */
html, body {
overflow: auto;
height: auto;
}
body {
min-height: 100vh;
display: flex;
flex-direction: column;
background: var(--gray-bg);
}
.gen-header {
background: var(--header-bg);
color: var(--header-fg);
padding: 0 1.5rem;
height: 48px;
display: flex;
align-items: center;
gap: 1rem;
}
.gen-header h1 {
font-family: var(--serif);
font-size: 1rem;
font-weight: 400;
color: #fff;
}
.gen-header a {
color: rgba(255,255,255,0.6);
text-decoration: none;
font-size: 0.85rem;
margin-left: auto;
}
.gen-header a:hover {
color: #fff;
}
.gen-main {
flex: 1;
max-width: 680px;
margin: 2rem auto;
width: 100%;
padding: 0 1rem;
display: flex;
flex-direction: column;
gap: 1.5rem;
}
.gen-card {
background: #fff;
border-radius: 10px;
padding: 1.5rem;
box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}
.gen-card h2 {
font-family: var(--serif);
font-size: 1.2rem;
margin-bottom: 1rem;
}
.field-group {
display: flex;
flex-direction: column;
gap: 0.35rem;
margin-bottom: 1rem;
}
.field-group label {
font-size: 0.85rem;
font-weight: 600;
color: #444;
}
.field-group input[type="text"],
.field-group input[type="number"],
.field-group input[type="url"] {
padding: 0.55rem 0.75rem;
border: 1px solid #ccc;
border-radius: 6px;
font-family: var(--sans);
font-size: 0.95rem;
width: 100%;
transition: border-color 0.15s;
}
.field-group input:focus {
outline: none;
border-color: var(--revealed);
}
/* Radio option cards */
.radio-group {
display: flex;
gap: 0.75rem;
flex-wrap: wrap;
}
.radio-card {
flex: 1;
min-width: 140px;
border: 2px solid #ddd;
border-radius: 8px;
padding: 0.75rem 1rem;
cursor: pointer;
display: flex;
align-items: center;
gap: 0.6rem;
transition: border-color 0.15s, background 0.15s;
user-select: none;
}
.radio-card:hover {
border-color: #aaa;
}
.radio-card input[type="radio"] {
accent-color: var(--revealed);
width: 16px;
height: 16px;
flex-shrink: 0;
}
.radio-card input[type="radio"]:checked + .radio-card-content .radio-label {
color: var(--revealed);
}
.radio-card-content {
display: flex;
align-items: center;
gap: 0.5rem;
}
.radio-card:has(input:checked) {
border-color: var(--revealed);
background: #eef4fc;
}
.radio-card img {
height: 28px;
width: auto;
object-fit: contain;
}
.radio-label {
font-size: 0.9rem;
font-weight: 600;
color: #333;
}
.field-hint {
font-size: 0.8rem;
color: #777;
}
.required {
color: var(--incorrect, #b91c1c);
}
#generate-btn {
padding: 0.6rem 1.5rem;
background: var(--header-bg);
color: #fff;
border: none;
border-radius: 6px;
font-family: var(--sans);
font-size: 0.95rem;
cursor: pointer;
transition: background 0.15s;
}
#generate-btn:hover {
background: var(--cardinal-dark, #651010);
}
#generate-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.output-area {
display: none;
}
.output-area.visible {
display: block;
}
.output-label {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 0.5rem;
}
.output-label span {
font-size: 0.85rem;
font-weight: 600;
color: #444;
}
#copy-btn {
padding: 0.3rem 0.85rem;
background: var(--gray-bg);
border: 1px solid #ccc;
border-radius: 5px;
font-family: var(--sans);
font-size: 0.85rem;
cursor: pointer;
transition: background 0.15s;
}
#copy-btn:hover {
background: #d4d4d4;
}
#output-textarea {
width: 100%;
min-height: 180px;
padding: 0.75rem;
border: 1px solid #ccc;
border-radius: 6px;
font-family: 'Menlo', 'Consolas', monospace;
font-size: 0.8rem;
resize: vertical;
color: #333;
background: #fafafa;
}
#puzzle-title-preview {
font-size: 0.85rem;
color: #555;
min-height: 1.2em;
margin-top: 0.25rem;
}
#gen-error {
color: var(--incorrect);
font-size: 0.875rem;
min-height: 1.2em;
}
.gen-footer {
text-align: center;
padding: 1rem;
font-size: 0.8rem;
color: #888;
}
@media (max-width: 640px) {
.gen-main {
margin: 1rem auto;
}
}
</style>
</head>
<body>
<header class="gen-header">
<h1>The Stanford Daily — Crossword Embed Generator</h1>
<a href="index.html">← Play</a>
</header>
<main class="gen-main">
<div class="gen-card">
<h2>Generate WordPress Embed Code</h2>
<div class="field-group">
<label>Puzzle type</label>
<div class="radio-group">
<label class="radio-card">
<input type="radio" name="puzzle-type" value="dimi" checked>
<div class="radio-card-content">
<span class="radio-label">Daily Diminutive</span>
</div>
</label>
<label class="radio-card">
<input type="radio" name="puzzle-type" value="crossword">
<div class="radio-card-content">
<span class="radio-label">Daily Crossword</span>
</div>
</label>
</div>
</div>
<div class="field-group">
<label for="puzzle-num-input">Puzzle number <span class="required">*</span></label>
<input type="text" id="puzzle-num-input" placeholder="e.g. 139" autocomplete="off">
<span class="field-hint">Shown in the header as "Daily Diminutive #139".</span>
</div>
<div class="field-group">
<label for="puz-url-input">Puzzle file URL <span class="required">*</span></label>
<input type="url" id="puz-url-input"
placeholder="https://github.com/user/repo/blob/main/puzzles/2024-01-15.puz"
autocomplete="off" spellcheck="false">
<span class="field-hint">Paste any GitHub URL — it will be converted to a raw link automatically. Title and constructor are read from the file.</span>
</div>
<div id="gen-error" role="alert" aria-live="polite"></div>
<div id="puzzle-title-preview" aria-live="polite"></div>
<button id="generate-btn">Generate Embed Code</button>
</div>
<div class="gen-card output-area" id="output-area">
<div class="output-label">
<span>WordPress embed snippet</span>
<button id="copy-btn">Copy</button>
</div>
<textarea id="output-textarea" readonly spellcheck="false" aria-label="Generated embed HTML"></textarea>
<p style="font-size:0.8rem; color:#777; margin-top:0.5rem;">
Paste this into a <strong>Custom HTML</strong> block in WordPress.
Make sure the iframe's <code>src</code> domain is allowed under
<em>Settings → Reading → Allowed iframe domains</em> if required by your theme.
</p>
</div>
</main>
<footer class="gen-footer">
Made by Anna Yang
</footer>
<script type="module">
import { parsePuz } from './puz-parser.js';
const puzUrlInput = document.getElementById('puz-url-input');
const puzzleNumInput = document.getElementById('puzzle-num-input');
const generateBtn = document.getElementById('generate-btn');
const outputArea = document.getElementById('output-area');
const outputTextarea = document.getElementById('output-textarea');
const copyBtn = document.getElementById('copy-btn');
const genError = document.getElementById('gen-error');
const titlePreview = document.getElementById('puzzle-title-preview');
// Resolve index.html relative to this page — always correct regardless of host/path
const playerBase = new URL('index.html', location.href).href;
generateBtn.addEventListener('click', async () => {
genError.textContent = '';
titlePreview.textContent = '';
outputArea.classList.remove('visible');
// Validate required fields
const puzzleNum = puzzleNumInput.value.trim();
let rawUrl = puzUrlInput.value.trim();
if (!puzzleNum || !rawUrl) {
genError.textContent = 'Puzzle number and puzzle file URL are required.';
return;
}
// Auto-convert github.com blob URLs → raw.githubusercontent.com
// e.g. https://github.com/user/repo/blob/main/puzzles/file.puz
// → https://raw.githubusercontent.com/user/repo/main/puzzles/file.puz
const githubBlobMatch = rawUrl.match(
/^https?:\/\/github\.com\/([^/]+)\/([^/]+)\/blob\/(.+)$/
);
if (githubBlobMatch) {
rawUrl = `https://raw.githubusercontent.com/${githubBlobMatch[1]}/${githubBlobMatch[2]}/${githubBlobMatch[3]}`;
puzUrlInput.value = rawUrl;
titlePreview.textContent = 'Converted to raw URL for direct access.';
}
try {
new URL(rawUrl);
} catch {
genError.textContent = 'Invalid URL. Please enter a full URL including https://.';
return;
}
const puzzleType = document.querySelector('input[name="puzzle-type"]:checked')?.value || 'dimi';
generateBtn.disabled = true;
generateBtn.textContent = 'Loading puzzle…';
let parsedTitle = '';
let parsedAuthor = '';
try {
const resp = await fetch(rawUrl);
if (!resp.ok) throw new Error(`HTTP ${resp.status} — check that the URL is publicly accessible.`);
const buf = await resp.arrayBuffer();
const puz = parsePuz(buf);
parsedTitle = puz.title || '';
parsedAuthor = puz.author || '';
titlePreview.textContent = parsedTitle
? `Loaded: "${parsedTitle}"${parsedAuthor ? ` by ${parsedAuthor}` : ''}`
: 'Loaded successfully.';
} catch (err) {
if (err.message.includes('Failed to fetch') || err.message.includes('NetworkError') || err.message.includes('CORS')) {
genError.textContent = 'Could not fetch the file — make sure the URL points to a raw file (raw.githubusercontent.com), not a GitHub page.';
} else {
genError.textContent = `Could not load puzzle: ${err.message}`;
}
generateBtn.disabled = false;
generateBtn.textContent = 'Generate Embed Code';
return;
}
generateBtn.disabled = false;
generateBtn.textContent = 'Generate Embed Code';
const typeName = puzzleType === 'dimi' ? 'Daily Diminutive' : 'Daily Crossword';
const footerLabel = `${typeName} #${puzzleNum}`;
const playerUrl = new URL(playerBase);
playerUrl.searchParams.set('puz', rawUrl);
playerUrl.searchParams.set('embed', '1');
playerUrl.searchParams.set('type', puzzleType);
playerUrl.searchParams.set('num', puzzleNum);
if (parsedTitle) playerUrl.searchParams.set('title', parsedTitle);
if (parsedAuthor) playerUrl.searchParams.set('author', parsedAuthor);
const iframeSrc = playerUrl.toString();
const titleAttr = parsedTitle ? `${footerLabel} — ${parsedTitle}` : footerLabel;
const snippet = `<div style="max-width:760px; margin:0 auto;">
<iframe
src="${iframeSrc}"
width="100%"
height="530"
frameborder="0"
style="border:none; border-radius:8px; display:block;"
title="${titleAttr}"
allow="clipboard-write">
</iframe>
</div>`;
outputTextarea.value = snippet;
outputArea.classList.add('visible');
outputTextarea.select();
});
copyBtn.addEventListener('click', () => {
navigator.clipboard.writeText(outputTextarea.value).then(() => {
copyBtn.textContent = 'Copied!';
setTimeout(() => { copyBtn.textContent = 'Copy'; }, 2000);
}).catch(() => {
// Fallback: select the textarea so user can copy manually
outputTextarea.select();
copyBtn.textContent = 'Select all → copy manually';
setTimeout(() => { copyBtn.textContent = 'Copy'; }, 3000);
});
});
// Allow pressing Enter in the URL field to trigger generate
puzUrlInput.addEventListener('keydown', e => {
if (e.key === 'Enter') generateBtn.click();
});
</script>
</body>
</html>