← back to Allnewsdaily
scripts/short/formats.js
38 lines
"use strict";
const FORMATS = [
{ id: 'centered', label: 'DAILY BRIEFING', box: '1120x1040', x: 115, y: 650, gravity: 'center', sourceY: 1900 },
{ id: 'sidebar', label: 'HEADLINE DESK', box: '940x1120', x: 290, y: 610, gravity: 'NorthWest', sourceY: 1900 },
{ id: 'bulletin', label: 'NEWS BULLETIN', box: '1080x960', x: 135, y: 940, gravity: 'NorthWest', sourceY: 2000 },
];
function editionFormat(date) {
if (typeof date !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(date)) throw new Error('Edition date must be YYYY-MM-DD');
const ms = Date.parse(date + 'T00:00:00Z');
if (!Number.isFinite(ms) || new Date(ms).toISOString().slice(0, 10) !== date) throw new Error('Invalid edition date');
return FORMATS[((ms / 86400000) % 3 + 3) % 3];
}
function wording(format, dateLabel) {
const variants = {
centered: [`All News Daily. Your headlines for ${dateLabel}.`, 'That concludes this briefing. Read the source stories at all news daily dot com.'],
sidebar: [`All News Daily. Here is the ${dateLabel} news briefing.`, 'Those are the headlines. Find the source reporting at all news daily dot com.'],
bulletin: [`All News Daily. The headline bulletin for ${dateLabel}.`, 'This bulletin is complete. Source stories are at all news daily dot com.'],
};
if (!variants[format]) throw new Error('Unknown Short format');
return variants[format];
}
function validateScript(script) {
if (!script || !FORMATS.some(f => f.id === script.format)) throw new Error('Missing or invalid Short format');
if (!Array.isArray(script.beats) || !script.beats.length) throw new Error('Script requires beats');
const segments = [script.intro, ...script.beats, script.outro];
if (segments.some(s => !s || typeof s.text !== 'string' || !s.text.trim() || !Number.isFinite(s.estSec) || s.estSec <= 0)) throw new Error('Invalid text or segment timing');
if (script.beats.some((s, i) => s.n !== i + 1 || typeof s.headline !== 'string' || !s.headline.trim() || typeof s.outlet !== 'string' || !s.outlet.trim())) throw new Error('Invalid beat order or attribution');
const sum = segments.reduce((a, s) => a + s.estSec, 0);
if (!Number.isFinite(script.totalSec) || script.totalSec <= 0 || script.totalSec > 58 || Math.abs(sum - script.totalSec) > 0.11) throw new Error('Script duration must match segments and remain within 58 seconds');
return FORMATS.find(f => f.id === script.format);
}
module.exports = { FORMATS, editionFormat, wording, validateScript };