-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpdf.html
More file actions
431 lines (359 loc) · 11.8 KB
/
Copy pathpdf.html
File metadata and controls
431 lines (359 loc) · 11.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
---
layout: default
title: Essay Printer
permalink: /pdf/
---
<style>
body {
max-width: 1200px;
}
.printer-grid {
display: grid;
grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
gap: 30px;
}
.printer-editor label {
display: block;
font-weight: bold;
}
.printer-editor input,
.printer-editor textarea {
box-sizing: border-box;
width: 100%;
padding: 6px;
}
.printer-editor textarea {
min-height: 28em;
resize: vertical;
}
.printer-editor button {
padding: 6px 10px;
}
.printer-preview {
border-left: 1px solid #ccc;
padding-left: 30px;
}
.print-header {
border-bottom: 1px solid #aaa;
margin-bottom: 1em;
padding-bottom: .75em;
}
.print-header:empty {
display: none;
}
.print-header h1 {
margin: 0;
}
.print-subtitle {
font-size: 1.1em;
margin: .25em 0 0;
}
.print-metadata {
font-family: sans-serif;
font-size: .8em;
margin: .5em 0 0;
}
.print-metadata span + span::before {
content: " · ";
}
.print-body h1,
.print-body h2,
.print-body h3,
.print-body h4 {
break-after: avoid;
}
.print-body p,
.print-body li {
orphans: 3;
widows: 3;
}
.print-body blockquote {
border-left: 3px solid #aaa;
margin-left: 0;
padding-left: 1em;
}
.print-body pre {
white-space: pre-wrap;
}
@media (max-width: 850px) {
.printer-grid {
grid-template-columns: 1fr;
}
.printer-preview {
border-left: 0;
border-top: 1px solid #ccc;
padding-left: 0;
padding-top: 20px;
}
}
@page {
margin: .6in;
}
@media print {
body {
margin: 0;
max-width: none;
padding: 0;
font-size: 10pt;
line-height: 1.35;
}
body > header,
.screen-only,
.printer-editor {
display: none !important;
}
.printer-grid {
display: block;
}
.printer-preview {
border: 0;
padding: 0;
}
.preview-empty {
display: none;
}
.print-body a {
color: inherit;
text-decoration: none;
}
}
</style>
<div class="printer-grid">
<section class="printer-editor">
<form id="essay-printer-form">
<p>
<label for="author">Author</label>
<input id="author" name="author" autocomplete="off">
</p>
<p>
<label for="published">Published</label>
<input id="published" name="published" autocomplete="off">
</p>
<p>
<label for="source">Source</label>
<input id="source" name="source" autocomplete="off">
</p>
<p>
<label for="title">Title</label>
<input id="title" name="title" autocomplete="off">
</p>
<p>
<label for="subtitle">Subtitle</label>
<input id="subtitle" name="subtitle" autocomplete="off">
</p>
<p>
<label for="markdown">Markdown</label>
<textarea id="markdown" name="markdown" spellcheck="true"></textarea>
</p>
<p>
<button type="button" id="print-button">Print / Save PDF</button>
</p>
</form>
<details>
<summary>Extraction prompt</summary>
<p>Copy this prompt alongside the source page HTML, then paste the returned values into the fields above.</p>
<button type="button" id="copy-prompt-button">Copy extraction prompt</button>
</details>
</section>
<section class="printer-preview">
<article id="print-document">
<header class="print-header" id="print-header"></header>
<div class="print-body" id="print-body">
<p class="preview-empty"><em>The rendered article will appear here.</em></p>
</div>
</article>
</section>
</div>
<script>
(() => {
"use strict";
const EXTRACTION_PROMPT = `You are preparing a source-faithful article for compact printing. The complete HTML of the source page appears after this instruction.
Extract the actual essay and its metadata. Return exactly the following labeled sections, with no introductory or closing commentary:
TITLE
[title, or leave blank]
SUBTITLE
[subtitle or deck, or leave blank]
AUTHOR
[author, or leave blank]
PUBLISHED
[displayed publication date, or leave blank]
SOURCE
[publication name and a readable source URL, or leave blank]
MARKDOWN
[complete article as clean Markdown]
Rules:
- Preserve the complete article text and its original order. Do not summarize, rewrite, fact-check, or add analysis.
- Remove site navigation, cookie notices, share controls, newsletter promotions, related stories, comments, ads, and other page furniture.
- Omit decorative images and captions. Omit pull quotes when they duplicate article text. Preserve an image description only when it carries information necessary to understand the essay.
- Preserve meaningful section headings, paragraphs, lists, links, emphasis, and genuine block quotes as clean Markdown.
- Put metadata only in its matching labeled section. Begin the Markdown section with the article body exactly as it should be pasted into a Markdown field.
- In Source, include the publication/site name and canonical article URL when they are available. This will be printed as plain text, not used as a hyperlink.
- Preserve the displayed publication date faithfully. Do not invent missing metadata; leave its section blank instead.
- Do not wrap the entire Markdown section in a code fence; it should be ready to copy directly into the app.
- Before returning, verify that no paragraph from the main essay was accidentally dropped.
SOURCE PAGE HTML:
`;
const form = document.getElementById("essay-printer-form");
const printHeader = document.getElementById("print-header");
const printBody = document.getElementById("print-body");
function escapeHtml(value) {
return String(value)
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """)
.replaceAll("'", "'");
}
function safeUrl(value) {
const raw = String(value || "").trim();
if (!raw) return "";
try {
const parsed = new URL(raw, window.location.href);
return ["http:", "https:", "mailto:"].includes(parsed.protocol) ? parsed.href : "";
} catch (_) {
return "";
}
}
function inlineMarkdown(value) {
const tokens = [];
let text = String(value);
text = text.replace(/`([^`]+)`/g, (_, code) => {
const token = `\u0000${tokens.length}\u0000`;
tokens.push(`<code>${escapeHtml(code)}</code>`);
return token;
});
text = escapeHtml(text);
text = text.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_, alt) => escapeHtml(alt));
text = text.replace(/\[([^\]]+)\]\(([^()\s]+(?:\([^()\s]*\)[^()\s]*)?)(?:\s+"[^&]*")?\)/g, (_, label, url) => {
const href = safeUrl(url.replaceAll("&", "&"));
if (!href) return label;
const token = `\u0000${tokens.length}\u0000`;
tokens.push(`<a href="${escapeHtml(href)}">${label}</a>`);
return token;
});
text = text
.replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>")
.replace(/__([^_]+)__/g, "<strong>$1</strong>")
.replace(/(^|[^*])\*([^*\n]+)\*(?!\*)/g, "$1<em>$2</em>")
.replace(/(^|[^_])_([^_\n]+)_(?!_)/g, "$1<em>$2</em>");
return text.replace(/\u0000(\d+)\u0000/g, (_, index) => tokens[Number(index)]);
}
function renderMarkdown(markdown) {
const lines = String(markdown || "").replace(/\r\n?/g, "\n").split("\n");
const output = [];
let paragraph = [];
let listType = "";
let codeFence = false;
let codeLines = [];
const closeParagraph = () => {
if (!paragraph.length) return;
output.push(`<p>${inlineMarkdown(paragraph.join(" ").trim())}</p>`);
paragraph = [];
};
const closeList = () => {
if (!listType) return;
output.push(`</${listType}>`);
listType = "";
};
for (let index = 0; index < lines.length; index += 1) {
const line = lines[index];
const trimmed = line.trim();
if (/^```/.test(trimmed)) {
closeParagraph();
closeList();
if (codeFence) {
output.push(`<pre><code>${escapeHtml(codeLines.join("\n"))}</code></pre>`);
codeLines = [];
}
codeFence = !codeFence;
continue;
}
if (codeFence) {
codeLines.push(line);
continue;
}
if (!trimmed) {
closeParagraph();
closeList();
continue;
}
const quoteMatch = line.match(/^\s*>\s?(.*)$/);
if (quoteMatch) {
closeParagraph();
closeList();
const quoteLines = [quoteMatch[1]];
while (index + 1 < lines.length) {
const next = lines[index + 1].match(/^\s*>\s?(.*)$/);
if (!next) break;
quoteLines.push(next[1]);
index += 1;
}
output.push(`<blockquote>${renderMarkdown(quoteLines.join("\n"))}</blockquote>`);
continue;
}
const heading = line.match(/^\s*(#{1,4})\s+(.+?)\s*#*\s*$/);
if (heading) {
closeParagraph();
closeList();
const level = heading[1].length;
output.push(`<h${level}>${inlineMarkdown(heading[2])}</h${level}>`);
continue;
}
if (/^\s*((\*\s*){3,}|(-\s*){3,}|(_\s*){3,})$/.test(line)) {
closeParagraph();
closeList();
output.push("<hr>");
continue;
}
const unordered = line.match(/^\s*[-+*]\s+(.+)$/);
const ordered = line.match(/^\s*\d+[.)]\s+(.+)$/);
if (unordered || ordered) {
closeParagraph();
const requestedType = unordered ? "ul" : "ol";
if (listType && listType !== requestedType) closeList();
if (!listType) {
listType = requestedType;
output.push(`<${listType}>`);
}
output.push(`<li>${inlineMarkdown((unordered || ordered)[1])}</li>`);
continue;
}
closeList();
paragraph.push(trimmed);
}
closeParagraph();
closeList();
if (codeFence && codeLines.length) {
output.push(`<pre><code>${escapeHtml(codeLines.join("\n"))}</code></pre>`);
}
return output.join("\n");
}
function updatePreview() {
const state = Object.fromEntries(new FormData(form).entries());
const header = [];
if (state.title) header.push(`<h1>${escapeHtml(state.title)}</h1>`);
if (state.subtitle) header.push(`<p class="print-subtitle">${escapeHtml(state.subtitle)}</p>`);
const metadata = [];
if (state.author) metadata.push(`<span>By ${escapeHtml(state.author)}</span>`);
if (state.published) metadata.push(`<span>${escapeHtml(state.published)}</span>`);
if (state.source) metadata.push(`<span>Source: ${escapeHtml(state.source)}</span>`);
if (metadata.length) header.push(`<p class="print-metadata">${metadata.join("")}</p>`);
printHeader.innerHTML = header.join("");
printBody.innerHTML = renderMarkdown(state.markdown) || '<p class="preview-empty"><em>The rendered article will appear here.</em></p>';
}
form.addEventListener("input", updatePreview);
document.getElementById("print-button").addEventListener("click", () => window.print());
document.getElementById("copy-prompt-button").addEventListener("click", async (event) => {
const button = event.currentTarget;
const originalLabel = button.textContent;
try {
await navigator.clipboard.writeText(EXTRACTION_PROMPT);
button.textContent = "Prompt copied";
} catch (_) {
window.prompt("Copy this extraction prompt:", EXTRACTION_PROMPT);
}
window.setTimeout(() => { button.textContent = originalLabel; }, 1500);
});
updatePreview();
})();
</script>