-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.mjs
More file actions
412 lines (353 loc) · 16.8 KB
/
Copy pathbuild.mjs
File metadata and controls
412 lines (353 loc) · 16.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
#!/usr/bin/env node
/**
* ZEanth Laboratory — build script
* ------------------------------------------------------------
* Reads papers.json and generates, for every paper:
* - abs/{id}/index.html (permalink abstract page)
* - pdf/{id}/index.html (PDF viewer page, if PDF exists)
* Also generates:
* - pdf-manifest.json (list of paper IDs that have a PDF uploaded)
* - sitemap.xml
* - rss.xml
*
* Usage:
* node scripts/build.mjs
*
* Zero external dependencies — only Node's built-in fs/path modules.
* Safe to run repeatedly (idempotent).
* ------------------------------------------------------------
*/
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.resolve(__dirname, '..');
const PAPERS_JSON = path.join(ROOT, 'papers.json');
const TEMPLATE_PATH = path.join(ROOT, 'templates', 'abs.template.html');
const PDF_TEMPLATE_PATH = path.join(ROOT, 'templates', 'pdf.template.html');
const ABS_DIR = path.join(ROOT, 'abs');
const PDF_DIR = path.join(ROOT, 'pdf');
function readJSON(p) {
return JSON.parse(fs.readFileSync(p, 'utf-8'));
}
function escapeHtml(str) {
return String(str ?? '').replace(/[&<>"']/g, c => ({
'&': '&', '<': '<', '>': '>', '"': '"', "'": '''
}[c]));
}
function formatDate(iso) {
if (!iso) return '—';
const d = new Date(iso + 'T00:00:00');
const months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
return `${String(d.getDate()).padStart(2,'0')} ${months[d.getMonth()]} ${d.getFullYear()}`;
}
const STATUS_LABELS = {
'preprint': 'Preprint',
'under-review': 'Under Review',
'accepted': 'Accepted',
'published': 'Published',
'withdrawn': 'Withdrawn'
};
function statusBadge(status) {
const label = STATUS_LABELS[status] || status;
return `<span class="badge badge-${escapeHtml(status)}">${escapeHtml(label)}</span>`;
}
function categoryTags(categories) {
return (categories || []).map(c => `<span class="tag mono">${escapeHtml(c)}</span>`).join('');
}
function bibDate(iso) {
if (!iso) return { year: '' };
return { year: new Date(iso + 'T00:00:00').getFullYear() };
}
function buildBibtex(paper, archive) {
const firstAuthorLast = (paper.authors[0] || 'anonymous').split(' ').pop().toLowerCase().replace(/[^a-z]/g, '');
const { year } = bibDate(paper.submittedDate);
const key = `${firstAuthorLast}${year}${paper.id.replace(/[^0-9]/g, '')}`;
const authors = (paper.authors || []).join(' and ');
return [
`@misc{${key},`,
` title={${paper.title}},`,
` author={${authors}},`,
` year={${year}},`,
` eprint={${paper.id}},`,
` archivePrefix={${archive.shortName}},`,
` primaryClass={${(paper.categories || [])[0] || 'cs'}}`,
`}`
].join('\n');
}
/* ──────────────────────────────────────────────
PDF helpers — versioned structure: pdf/{id}/v1.pdf, v2.pdf, …
────────────────────────────────────────────── */
/**
* Detect all PDF versions for a paper.
* Checks for versioned files (v1.pdf, v2.pdf, …) and
* legacy non-versioned file ({id}.pdf).
* Returns array of version strings found, e.g. ['v1','v2'].
*/
function detectPdfVersions(id) {
const pdfDir = path.join(PDF_DIR, id);
const versions = [];
// Check for versioned files inside pdf/{id}/ directory
if (fs.existsSync(pdfDir) && fs.statSync(pdfDir).isDirectory()) {
const files = fs.readdirSync(pdfDir);
for (const f of files) {
const m = f.match(/^v(\d+)\.pdf$/i);
if (m) versions.push({ num: parseInt(m[1], 10), label: `v${m[1]}` });
}
}
// Also check for legacy flat file pdf/{id}.pdf — treat as v1
const legacyFile = path.join(PDF_DIR, `${id}.pdf`);
if (versions.length === 0 && fs.existsSync(legacyFile)) {
// Migrate: move legacy file into the versioned directory structure
fs.mkdirSync(pdfDir, { recursive: true });
fs.renameSync(legacyFile, path.join(pdfDir, 'v1.pdf'));
versions.push({ num: 1, label: 'v1' });
console.log(` migrated pdf/${id}.pdf → pdf/${id}/v1.pdf`);
}
versions.sort((a, b) => a.num - b.num);
return versions.map(v => v.label);
}
function hasPdf(id) {
return detectPdfVersions(id).length > 0;
}
function latestPdfVersion(id) {
const versions = detectPdfVersions(id);
return versions.length > 0 ? versions[versions.length - 1] : null;
}
function btn(href, label, disabled, note) {
if (disabled) {
return `<span class="btn disabled" title="${escapeHtml(note || 'Not available')}">${escapeHtml(label)}</span>`;
}
return `<a class="btn ${label === 'View PDF' ? 'btn-primary' : ''}" href="${escapeHtml(href)}">${escapeHtml(label)}</a>`;
}
function renderTemplate(tpl, vars) {
let out = tpl;
for (const [key, val] of Object.entries(vars)) {
out = out.split(`{{${key}}}`).join(val ?? '');
}
return out;
}
/* ──────────────────────────────────────────────
License helpers
────────────────────────────────────────────── */
function buildLicenseSection(paper, archive) {
const license = paper.license || archive.defaultPaperLicense;
if (!license) {
return '<div class="license-info" style="color:var(--muted)">No license information available.</div>';
}
const spdx = escapeHtml(license.spdx || '');
const name = escapeHtml(license.name || spdx);
const url = license.url || '';
const isDefault = !paper.license;
const archiveRepo = `${archive.github.replace(/\/$/, '')}/${archive.url.replace(/^https?:\/\//, '').replace(/\/.*/,'')}`;
const licenseFile = paper.license?.licenseFile || archive.defaultPaperLicense?.licenseFile || 'LICENSE';
const localLicensePath = path.join(PDF_DIR, paper.id, licenseFile);
const hasLocalFile = fs.existsSync(localLicensePath);
let licenseFileUrl = paper.license?.licenseFileUrl || archive.defaultPaperLicense?.licenseFileUrl || null;
if (hasLocalFile) {
if (!licenseFileUrl) {
licenseFileUrl = `${archiveRepo}/blob/main/pdf/${paper.id}/${licenseFile}`;
}
} else {
if (paper.license?.licenseFile) {
console.warn(` Warning: licenseFile "${licenseFile}" configured for paper ${paper.id} but not found at pdf/${paper.id}/${licenseFile}`);
}
if (licenseFileUrl) {
console.warn(` Warning: licenseFileUrl configured for paper ${paper.id} but local license file not found at pdf/${paper.id}/${licenseFile}`);
}
}
const targetUrl = licenseFileUrl || url;
let html = '<div class="license-info">';
html += `<span class="license-badge">${spdx}</span>`;
if (targetUrl) {
html += `<a href="${escapeHtml(targetUrl)}" target="_blank" rel="noopener">${name}</a>`;
} else {
html += name;
}
if (isDefault) {
html += ' <span style="color:var(--muted); font-size:12.5px;">(archive default)</span>';
}
html += '</div>';
return html;
}
/* ──────────────────────────────────────────────
Abstract page builder
────────────────────────────────────────────── */
function buildAbsPage(paper, archive, template) {
const primaryCategory = (paper.categories || [])[0] || 'cs';
const pdfAvailable = hasPdf(paper.id);
const commentsRow = paper.comments
? `<dt>Comments</dt><dd>${escapeHtml(paper.comments)}</dd>` : '';
const journalRefRow = paper.journalRef
? `<dt>Journal ref.</dt><dd>${escapeHtml(paper.journalRef)}</dd>` : '';
const doiRow = paper.doi
? `<dt>DOI</dt><dd><a href="https://doi.org/${escapeHtml(paper.doi)}" target="_blank" rel="noopener">${escapeHtml(paper.doi)}</a></dd>` : '';
// Build history rows with version PDF links
const pdfVersions = detectPdfVersions(paper.id);
const historyRows = (paper.history || []).map(h => {
const versionLabel = escapeHtml(h.version);
const hasThisVersion = pdfVersions.includes(h.version);
const versionLink = hasThisVersion
? `<td><a class="version-link" href="../../pdf/${encodeURIComponent(paper.id)}?version=${encodeURIComponent(h.version)}">View PDF</a></td>`
: '<td></td>';
return `
<tr><td class="mono">${versionLabel}</td><td>${formatDate(h.date)}</td><td>${escapeHtml(h.note || '')}</td>${versionLink}</tr>
`;
}).join('');
const pdfButton = btn(`../../pdf/${paper.id}`, 'View PDF', !pdfAvailable, 'PDF not yet uploaded — check back soon');
const codeButton = paper.codeUrl ? btn(paper.codeUrl, 'Code') : '';
const datasetButton = paper.datasetUrl ? btn(paper.datasetUrl, 'Dataset') : '';
const doiButton = paper.doi ? btn(`https://doi.org/${paper.doi}`, 'View DOI') : '';
const repoUrl = archive.github.replace(/\/$/, '') + '/' + archive.url.replace(/^https?:\/\//, '').split('/').pop();
// Build repo URL: e.g. https://github.com/zeanth + zeanth.github.io
const archiveRepo = `${archive.github.replace(/\/$/, '')}/${archive.url.replace(/^https?:\/\//, '').replace(/\/.*/,'')}`;
const vars = {
ID: escapeHtml(paper.id),
VERSION: escapeHtml(paper.version || 'v1'),
TITLE: escapeHtml(paper.title),
AUTHORS: escapeHtml((paper.authors || []).join(', ')),
STATUS_BADGE: statusBadge(paper.status),
CATEGORY_TAGS: categoryTags(paper.categories),
ABSTRACT: escapeHtml(paper.abstract),
SUBMITTED_DATE_FMT: formatDate(paper.submittedDate),
COMMENTS_ROW: commentsRow,
JOURNALREF_ROW: journalRefRow,
DOI_ROW: doiRow,
PDF_BUTTON: pdfButton,
CODE_BUTTON: codeButton,
DATASET_BUTTON: datasetButton,
DOI_BUTTON: doiButton,
HISTORY_ROWS: historyRows,
BIBTEX: escapeHtml(buildBibtex(paper, archive)),
LICENSE_SECTION: buildLicenseSection(paper, archive),
ARCHIVE_NAME: escapeHtml(archive.name),
ARCHIVE_SHORT: escapeHtml(archive.shortName),
ARCHIVE_TAGLINE: escapeHtml(archive.tagline),
ARCHIVE_LOGO: archive.logo,
ARCHIVE_GITHUB: archive.github,
ARCHIVE_REPO: archiveRepo,
PRIMARY_CATEGORY: escapeHtml(primaryCategory),
META_DESCRIPTION: escapeHtml((paper.abstract || '').slice(0, 180)),
CANONICAL_URL: `${archive.url.replace(/\/$/, '')}/abs/${paper.id}`
};
return renderTemplate(template, vars);
}
/* ──────────────────────────────────────────────
PDF viewer page builder (PDF.js-based)
────────────────────────────────────────────── */
function escapeJsString(str) {
return String(str ?? '').replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n').replace(/\r/g, '\\r');
}
function buildPdfViewerPage(paper, archive, versions, template) {
const latestVersion = versions[versions.length - 1];
const versionsJson = JSON.stringify(versions);
const archiveRepo = `${archive.github.replace(/\/$/, '')}/${archive.url.replace(/^https?:\/\//, '').replace(/\/.*/,'')}`;
const primaryCategory = (paper.categories || [])[0] || 'cs';
let versionSelectHtml = '';
if (versions.length > 1) {
versionSelectHtml = `
<select class="version-select" id="versionSelect">
${versions.map(v => `<option value="${escapeHtml(v)}"${v === latestVersion ? ' selected' : ''}>${escapeHtml(v).toUpperCase()}</option>`).join('')}
</select>`;
}
const vars = {
ID: escapeHtml(paper.id),
TITLE: escapeHtml(paper.title),
TITLE_JS: escapeJsString(paper.title),
ARCHIVE_NAME: escapeHtml(archive.name),
ARCHIVE_LOGO: archive.logo,
ARCHIVE_TAGLINE: escapeHtml(archive.tagline),
ARCHIVE_SHORT: escapeHtml(archive.shortName),
ARCHIVE_GITHUB: archive.github,
ARCHIVE_REPO: archiveRepo,
PRIMARY_CATEGORY: escapeHtml(primaryCategory),
LATEST_VERSION: escapeHtml(latestVersion),
LATEST_VERSION_UPPER: escapeHtml(latestVersion).toUpperCase(),
VERSIONS_JSON: versionsJson,
VERSION_SELECT: versionSelectHtml
};
return renderTemplate(template, vars);
}
/* ──────────────────────────────────────────────
Sitemap & RSS builders
────────────────────────────────────────────── */
function buildSitemap(papers, archive) {
const base = archive.url.replace(/\/$/, '');
const urls = [
`${base}/`,
...papers.map(p => `${base}/abs/${p.id}`),
...papers.filter(p => hasPdf(p.id)).map(p => `${base}/pdf/${p.id}`)
];
const body = urls.map(u => ` <url><loc>${u}</loc></url>`).join('\n');
return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${body}\n</urlset>\n`;
}
function buildRss(papers, archive) {
const base = archive.url.replace(/\/$/, '');
const sorted = [...papers].sort((a, b) => b.submittedDate.localeCompare(a.submittedDate));
const items = sorted.map(p => `
<item>
<title>${escapeHtml(p.title)}</title>
<link>${base}/abs/${p.id}</link>
<guid>${base}/abs/${p.id}</guid>
<pubDate>${new Date(p.submittedDate + 'T00:00:00').toUTCString()}</pubDate>
<description>${escapeHtml((p.abstract || '').slice(0, 300))}</description>
</item>`).join('');
return `<?xml version="1.0" encoding="UTF-8"?>\n<rss version="2.0"><channel>\n <title>${escapeHtml(archive.name)} — Paper Archive</title>\n <link>${base}/</link>\n <description>${escapeHtml(archive.tagline)}</description>\n${items}\n</channel></rss>\n`;
}
/* ──────────────────────────────────────────────
Main
────────────────────────────────────────────── */
function main() {
const data = readJSON(PAPERS_JSON);
const { archive, papers } = data;
fs.mkdirSync(ABS_DIR, { recursive: true });
fs.mkdirSync(PDF_DIR, { recursive: true });
const template = fs.readFileSync(TEMPLATE_PATH, 'utf-8');
const pdfTemplate = fs.readFileSync(PDF_TEMPLATE_PATH, 'utf-8');
const seenIds = new Set();
for (const paper of papers) {
if (seenIds.has(paper.id)) {
throw new Error(`Duplicate paper id detected: ${paper.id}. Every paper needs a unique id.`);
}
seenIds.add(paper.id);
// Validation: check that paper version matches the latest history entry
const latestHistoryVersion = paper.history && paper.history.length > 0
? paper.history[paper.history.length - 1].version
: null;
if (latestHistoryVersion && paper.version !== latestHistoryVersion) {
console.warn(` Warning: Paper ${paper.id} current version (${paper.version}) does not match latest history version (${latestHistoryVersion})`);
}
// Validation: check for missing versioned PDF files mentioned in history
const pdfVersions = detectPdfVersions(paper.id);
if (paper.history && pdfVersions.length > 0) {
for (const entry of paper.history) {
if (!pdfVersions.includes(entry.version)) {
console.warn(` Warning: History entry version "${entry.version}" for paper ${paper.id} has no corresponding PDF file in pdf/${paper.id}/${entry.version}.pdf`);
}
}
}
// Generate abstract page
const outDir = path.join(ABS_DIR, paper.id);
fs.mkdirSync(outDir, { recursive: true });
const html = buildAbsPage(paper, archive, template);
fs.writeFileSync(path.join(outDir, 'index.html'), html, 'utf-8');
console.log(` built abs/${paper.id}/index.html`);
// Generate PDF viewer page (if any version exists)
if (pdfVersions.length > 0) {
const pdfOutDir = path.join(PDF_DIR, paper.id);
fs.mkdirSync(pdfOutDir, { recursive: true });
const pdfHtml = buildPdfViewerPage(paper, archive, pdfVersions, pdfTemplate);
fs.writeFileSync(path.join(pdfOutDir, 'index.html'), pdfHtml, 'utf-8');
console.log(` built pdf/${paper.id}/index.html (versions: ${pdfVersions.join(', ')})`);
}
}
const pdfManifest = papers.filter(p => hasPdf(p.id)).map(p => p.id);
fs.writeFileSync(path.join(ROOT, 'pdf-manifest.json'), JSON.stringify(pdfManifest, null, 2), 'utf-8');
console.log(` built pdf-manifest.json (${pdfManifest.length} PDF(s) found)`);
fs.writeFileSync(path.join(ROOT, 'sitemap.xml'), buildSitemap(papers, archive), 'utf-8');
console.log(' built sitemap.xml');
fs.writeFileSync(path.join(ROOT, 'rss.xml'), buildRss(papers, archive), 'utf-8');
console.log(' built rss.xml');
console.log(`\nDone. ${papers.length} paper(s) processed.`);
}
main();