436 lines
14 KiB
JavaScript
436 lines
14 KiB
JavaScript
function pickRandom(arr) {
|
|
return arr[Math.floor(Math.random() * arr.length)];
|
|
}
|
|
|
|
function escapeHtml(text) {
|
|
if (!text) return '';
|
|
const map = {
|
|
'&': '&',
|
|
'<': '<',
|
|
'>': '>',
|
|
'"': '"',
|
|
"'": '''
|
|
};
|
|
return String(text).replace(/[&<>"']/g, m => map[m]);
|
|
}
|
|
|
|
export function dungeonTemplate(data) {
|
|
const bodyFonts = [
|
|
"'Lora', serif",
|
|
"'Merriweather', serif",
|
|
"'Libre Baskerville', serif",
|
|
"'Source Serif 4', serif"
|
|
];
|
|
|
|
const headingFonts = [
|
|
"'New Rocker', system-ui",
|
|
"'UnifrakturCook', cursive",
|
|
"'IM Fell DW Pica', serif",
|
|
"'Cinzel', serif",
|
|
"'Cormorant Garamond', serif",
|
|
"'Playfair Display', serif"
|
|
];
|
|
|
|
const quoteFonts = [
|
|
"'Playfair Display', serif",
|
|
"'Libre Baskerville', serif",
|
|
"'Merriweather', serif"
|
|
];
|
|
|
|
const bodyFont = pickRandom(bodyFonts);
|
|
const headingFont = pickRandom(headingFonts);
|
|
const quoteFont = pickRandom(quoteFonts);
|
|
|
|
// Check if we have a map image to include
|
|
const hasMap = data.map && typeof data.map === 'string' && data.map.startsWith('data:image/');
|
|
|
|
return `
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>${data.title}</title>
|
|
<link
|
|
href="https://fonts.googleapis.com/css2?family=Cinzel+Decorative&family=UnifrakturCook&family=New+Rocker&family=Metamorphous&family=Playfair+Display&family=Alegreya+Sans&family=Cabin&family=IBM+Plex+Sans&family=Cormorant+Garamond&family=Lora&family=Merriweather&family=Libre+Baskerville&family=Source+Serif+4&family=Walter+Turncoat&family=Uncial+Antiqua&family=Beth+Ellen&family=Pinyon+Script&display=swap"
|
|
rel="stylesheet">
|
|
<style>
|
|
@page {
|
|
size: A4 landscape;
|
|
margin: 0;
|
|
}
|
|
body {
|
|
margin: 0;
|
|
padding: 0;
|
|
font-family: ${bodyFont};
|
|
color: #1a1a1a;
|
|
font-size: 0.7em;
|
|
line-height: 1.35em;
|
|
}
|
|
.content-page {
|
|
height: 100vh;
|
|
box-sizing: border-box;
|
|
padding: 1.2cm;
|
|
page-break-after: always;
|
|
overflow: visible;
|
|
break-inside: avoid;
|
|
}
|
|
h1 {
|
|
font-family: ${headingFont};
|
|
text-align: center;
|
|
text-transform: uppercase;
|
|
font-size: 1.8em;
|
|
margin: 0.15em 0 0.2em;
|
|
color: #1a1a1a;
|
|
border-bottom: 2px solid #1a1a1a;
|
|
padding-bottom: 0.15em;
|
|
letter-spacing: 0.1em;
|
|
}
|
|
.flavor {
|
|
text-align: center;
|
|
font-style: italic;
|
|
font-family: ${quoteFont};
|
|
margin: 0.3em 0 0.6em;
|
|
font-size: 0.85em;
|
|
line-height: 1.35em;
|
|
}
|
|
.columns {
|
|
display: grid;
|
|
grid-template-columns: 1fr 1fr 1fr;
|
|
gap: 0.4cm;
|
|
align-items: start;
|
|
}
|
|
.col {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 0.25em;
|
|
overflow-wrap: break-word;
|
|
word-break: normal;
|
|
hyphens: auto;
|
|
}
|
|
.section-block {
|
|
break-inside: avoid;
|
|
page-break-inside: avoid;
|
|
margin-bottom: 0.3em;
|
|
}
|
|
h2 {
|
|
font-family: ${headingFont};
|
|
font-size: 1.05em;
|
|
margin: 0.2em 0 0.2em;
|
|
color: #1a1a1a;
|
|
border-bottom: 1px solid #1a1a1a;
|
|
padding-bottom: 0.08em;
|
|
text-transform: uppercase;
|
|
letter-spacing: 0.05em;
|
|
break-inside: avoid;
|
|
page-break-inside: avoid;
|
|
}
|
|
.room {
|
|
break-inside: avoid;
|
|
page-break-inside: avoid;
|
|
}
|
|
.room h3 {
|
|
margin: 0.1em 0 0.05em;
|
|
font-size: 0.95em;
|
|
font-weight: bold;
|
|
color: #1a1a1a;
|
|
}
|
|
.room p {
|
|
margin: 0 0 0.25em;
|
|
font-size: 0.85em;
|
|
font-weight: normal;
|
|
line-height: 1.3em;
|
|
}
|
|
.encounter, .npc, .treasure, .plot-resolution {
|
|
margin: 0 0 0.35em;
|
|
break-inside: avoid;
|
|
page-break-inside: avoid;
|
|
font-size: 0.85em;
|
|
line-height: 1.3em;
|
|
}
|
|
.random-events {
|
|
margin: 0.2em 0;
|
|
break-inside: avoid;
|
|
page-break-inside: avoid;
|
|
font-size: 0.85em;
|
|
}
|
|
.random-events table {
|
|
margin-top: 0.15em;
|
|
}
|
|
.encounter strong, .npc strong, .treasure strong {
|
|
font-weight: bold;
|
|
}
|
|
table {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
margin: 0.2em 0;
|
|
font-size: 0.85em;
|
|
break-inside: avoid;
|
|
page-break-inside: avoid;
|
|
border: 1px solid #1a1a1a;
|
|
}
|
|
table th {
|
|
font-family: ${headingFont};
|
|
text-align: left;
|
|
border-bottom: 1px solid #1a1a1a;
|
|
padding: 0.15em 0.3em;
|
|
font-size: 0.85em;
|
|
text-transform: uppercase;
|
|
font-weight: bold;
|
|
}
|
|
table td {
|
|
padding: 0.25em 0.4em;
|
|
vertical-align: top;
|
|
line-height: 1.3em;
|
|
border-bottom: 1px solid #1a1a1a;
|
|
word-wrap: break-word;
|
|
overflow-wrap: break-word;
|
|
word-break: normal;
|
|
}
|
|
table tr:last-child td {
|
|
border-bottom: 1px solid #1a1a1a;
|
|
}
|
|
table td:first-child {
|
|
font-weight: bold;
|
|
width: 2em;
|
|
text-align: center;
|
|
border-right: 1px solid #1a1a1a;
|
|
}
|
|
.encounters-table td:nth-child(2) {
|
|
font-weight: bold;
|
|
min-width: 20%;
|
|
max-width: 30%;
|
|
padding-right: 0.5em;
|
|
border-right: 1px solid #1a1a1a;
|
|
}
|
|
.encounters-table td:nth-child(3) {
|
|
width: auto;
|
|
font-size: 0.8em;
|
|
line-height: 1.3em;
|
|
}
|
|
.map-page {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
page-break-before: always;
|
|
}
|
|
.map-container {
|
|
text-align: center;
|
|
margin: 1em 0;
|
|
}
|
|
.map-container img {
|
|
max-width: 100%;
|
|
max-height: calc(100vh - 3cm);
|
|
border: 1px solid #1a1a1a;
|
|
}
|
|
ul {
|
|
margin: 0.2em 0;
|
|
padding-left: 1.2em;
|
|
}
|
|
li {
|
|
margin: 0.08em 0;
|
|
font-size: 0.85em;
|
|
line-height: 1.3em;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="content-page">
|
|
<h1>${escapeHtml(data.title)}</h1>
|
|
${data.flavor ? `<p class="flavor">${escapeHtml(data.flavor)}</p>` : ''}
|
|
|
|
<div class="columns">
|
|
<div class="col">
|
|
${data.hooksRumors && data.hooksRumors.length > 0 ? `
|
|
<div class="section-block">
|
|
<h2>Hooks & Rumors</h2>
|
|
<ul>
|
|
${data.hooksRumors.map(hook => `<li>${escapeHtml(hook)}</li>`).join('')}
|
|
</ul>
|
|
</div>
|
|
` : ''}
|
|
|
|
${data.randomEvents && data.randomEvents.length > 0 ? `
|
|
<div class="section-block random-events">
|
|
<h2>Random Events (d6)</h2>
|
|
<table>
|
|
<tbody>
|
|
${data.randomEvents.map((event, index) => `
|
|
<tr>
|
|
<td>${index + 1}</td>
|
|
<td>${escapeHtml(event)}</td>
|
|
</tr>
|
|
`).join('')}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
` : ''}
|
|
|
|
${data.rooms && data.rooms.length > 0 ? `
|
|
<div class="section-block">
|
|
<h2>Locations</h2>
|
|
${data.rooms.map(room => {
|
|
let desc = room.description || '';
|
|
// Truncate to 2 sentences max
|
|
const sentences = desc.match(/[^.!?]+[.!?]+/g) || [desc];
|
|
if (sentences.length > 2) {
|
|
desc = sentences.slice(0, 2).join(' ').trim();
|
|
}
|
|
// Also limit by character count (~150 chars)
|
|
if (desc.length > 150) {
|
|
desc = desc.substring(0, 147).trim();
|
|
const lastPeriod = desc.lastIndexOf('.');
|
|
if (lastPeriod > 100) {
|
|
desc = desc.substring(0, lastPeriod + 1);
|
|
} else {
|
|
desc += '...';
|
|
}
|
|
}
|
|
return `
|
|
<div class="room">
|
|
<h3>${escapeHtml(room.name)}</h3>
|
|
<p>${escapeHtml(desc)}</p>
|
|
</div>
|
|
`;
|
|
}).join('')}
|
|
</div>
|
|
` : ''}
|
|
</div>
|
|
|
|
<div class="col">
|
|
${data.encounters && data.encounters.length > 0 ? `
|
|
<div class="section-block">
|
|
<h2>Encounters (d6)</h2>
|
|
<table class="encounters-table">
|
|
<tbody>
|
|
${data.encounters.map((encounter, index) => {
|
|
// Truncate details to 4 sentences max to prevent overflow
|
|
let details = encounter.details || '';
|
|
// Remove encounter name if it appears at start
|
|
if (details.toLowerCase().startsWith(encounter.name.toLowerCase())) {
|
|
details = details.substring(encounter.name.length).replace(/^:\s*/, '').trim();
|
|
}
|
|
// Remove location prefix if present (format: "Location Name: description")
|
|
// Handle multiple colons - strip the first one that looks like a location
|
|
const locationMatch = details.match(/^([^:]+):\s*(.+)$/);
|
|
if (locationMatch) {
|
|
const potentialLocation = locationMatch[1].trim();
|
|
// If it looks like a location name (capitalized, not too long), remove it
|
|
if (potentialLocation.length > 3 && potentialLocation.length < 50 && /^[A-Z]/.test(potentialLocation)) {
|
|
details = locationMatch[2].trim();
|
|
}
|
|
}
|
|
// Split into sentences and keep only first 4
|
|
const sentences = details.match(/[^.!?]+[.!?]+/g) || [details];
|
|
if (sentences.length > 4) {
|
|
details = sentences.slice(0, 4).join(' ').trim();
|
|
}
|
|
// Also limit by character count as fallback (max ~350 chars)
|
|
if (details.length > 350) {
|
|
details = details.substring(0, 347).trim();
|
|
// Try to end at a sentence boundary
|
|
const lastPeriod = details.lastIndexOf('.');
|
|
if (lastPeriod > 280) {
|
|
details = details.substring(0, lastPeriod + 1);
|
|
} else {
|
|
details += '...';
|
|
}
|
|
}
|
|
return `
|
|
<tr>
|
|
<td>${index + 1}</td>
|
|
<td><strong>${escapeHtml(encounter.name)}</strong></td>
|
|
<td>${escapeHtml(details)}</td>
|
|
</tr>
|
|
`;
|
|
}).join('')}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
` : ''}
|
|
</div>
|
|
|
|
<div class="col">
|
|
${data.treasure && data.treasure.length > 0 ? `
|
|
<div class="section-block">
|
|
<h2>Treasure</h2>
|
|
${data.treasure.map(item => {
|
|
if (typeof item === 'object' && item.name && item.description) {
|
|
return `<div class="treasure"><strong>${escapeHtml(item.name)}</strong> — ${escapeHtml(item.description)}</div>`;
|
|
} else if (typeof item === 'string') {
|
|
// Handle string format "Name — Description"
|
|
const parts = item.split(/[—–-]/);
|
|
if (parts.length >= 2) {
|
|
return `<div class="treasure"><strong>${escapeHtml(parts[0].trim())}</strong> — ${escapeHtml(parts.slice(1).join(' ').trim())}</div>`;
|
|
}
|
|
return `<div class="treasure">${escapeHtml(item)}</div>`;
|
|
}
|
|
return '';
|
|
}).filter(Boolean).join('')}
|
|
</div>
|
|
` : ''}
|
|
|
|
${data.npcs && data.npcs.length > 0 ? `
|
|
<div class="section-block">
|
|
<h2>NPCs</h2>
|
|
${data.npcs.map(npc => {
|
|
if (typeof npc === 'object' && npc.name && npc.trait) {
|
|
return `<div class="npc"><strong>${escapeHtml(npc.name)}</strong>: ${escapeHtml(npc.trait)}</div>`;
|
|
} else if (typeof npc === 'string') {
|
|
// Handle string format "Name: Description"
|
|
const parts = npc.split(/:/);
|
|
if (parts.length >= 2) {
|
|
return `<div class="npc"><strong>${escapeHtml(parts[0].trim())}</strong>: ${escapeHtml(parts.slice(1).join(':').trim())}</div>`;
|
|
}
|
|
return `<div class="npc">${escapeHtml(npc)}</div>`;
|
|
}
|
|
return '';
|
|
}).filter(Boolean).join('')}
|
|
</div>
|
|
` : ''}
|
|
|
|
${data.plotResolutions && data.plotResolutions.length > 0 ? `
|
|
<div class="section-block">
|
|
<h2>Plot Resolutions</h2>
|
|
${data.plotResolutions.map(resolution => {
|
|
// Truncate to 2 sentences max to prevent overflow
|
|
let text = resolution || '';
|
|
// Split into sentences and keep only first 2
|
|
const sentences = text.match(/[^.!?]+[.!?]+/g) || [text];
|
|
if (sentences.length > 2) {
|
|
text = sentences.slice(0, 2).join(' ').trim();
|
|
}
|
|
// Also limit by character count as fallback (max ~150 chars)
|
|
if (text.length > 150) {
|
|
text = text.substring(0, 147).trim();
|
|
// Try to end at a sentence boundary
|
|
const lastPeriod = text.lastIndexOf('.');
|
|
if (lastPeriod > 100) {
|
|
text = text.substring(0, lastPeriod + 1);
|
|
} else {
|
|
text += '...';
|
|
}
|
|
}
|
|
return `
|
|
<div class="plot-resolution">
|
|
${escapeHtml(text)}
|
|
</div>
|
|
`;
|
|
}).join('')}
|
|
</div>
|
|
` : ''}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
${hasMap ? `
|
|
<div class="content-page map-page">
|
|
<div class="map-container">
|
|
<img src="${data.map}" alt="Dungeon Map" />
|
|
</div>
|
|
</div>
|
|
` : ''}
|
|
</body>
|
|
</html>
|
|
`;
|
|
}
|