News et newsletter : page /news/ (5 langues), bouton News, reglages (theme clair par defaut, sombre au choix), formulaire newsletter
Repris des copies site-principal-news et site-principal-reglages. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cette révision appartient à :
1 Parent
cb8f0f4b54
révision
baa25612e9
15 fichiers modifiés
+2264
-3
Aucun fichier ne correspond à votre recherche
@@ -0,0 +1,137 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* tools/add-news.js : ajoute une entrée à data/actus.json, après validation.
|
||||
*
|
||||
* Chaque entrée porte des thèmes, un texte court (pour les listes) et une version par niveau :
|
||||
* normal (obligatoire), simple et developpeur (facultatifs, repli sur « normal »).
|
||||
*
|
||||
* Exemple :
|
||||
* node tools/add-news.js --themes technique,applications \
|
||||
* --title "Nouvelle version de GWS" --text "Le logiciel sait maintenant exporter en PDF." \
|
||||
* --simple-text "Tu peux maintenant enregistrer tes résultats en PDF." \
|
||||
* --dev-title "GWS : export PDF" --dev-text "Ajout d'un export PDF des rapports." \
|
||||
* --short-title "GWS exporte en PDF" --short-text "Une nouveauté pour garder tes résultats." \
|
||||
* --link /gws/ [--date 2026-11-02] [--image /assets/img/photo.webp --image-alt "Description"] [--dry-run]
|
||||
*
|
||||
* Thèmes : audiovisuel, applications, technique, site, meteo, minecraft, mind, hub, vie-locale.
|
||||
* Zéro dépendance. N'écrit rien si une règle n'est pas respectée.
|
||||
*/
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const FILE = path.resolve(__dirname, '..', 'data', 'actus.json');
|
||||
const THEMES = ['audiovisuel', 'applications', 'technique', 'site', 'meteo', 'minecraft', 'mind', 'hub', 'vie-locale'];
|
||||
const LIMITS = { title: 120, text: 600, shortTitle: 80, shortText: 160 };
|
||||
|
||||
function parseArgs(argv) {
|
||||
const out = {};
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const a = argv[i];
|
||||
if (!a.startsWith('--')) throw new Error(`Argument inattendu : ${a}`);
|
||||
const key = a.slice(2);
|
||||
if (key === 'dry-run' || key === 'help') { out[key] = true; continue; }
|
||||
const v = argv[++i];
|
||||
if (v === undefined || v.startsWith('--')) throw new Error(`Valeur manquante pour --${key}`);
|
||||
out[key] = v;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function todayLocal() {
|
||||
const d = new Date();
|
||||
const p = (n) => String(n).padStart(2, '0');
|
||||
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}`;
|
||||
}
|
||||
|
||||
function validDate(s) {
|
||||
if (!/^\d{4}-\d{2}-\d{2}$/.test(s)) return false;
|
||||
const d = new Date(s + 'T12:00:00Z');
|
||||
return !isNaN(d) && d.toISOString().slice(0, 10) === s;
|
||||
}
|
||||
|
||||
function validUrl(u) { return /^\/(?!\/)\S*$/.test(u) || /^https:\/\/\S+$/i.test(u); }
|
||||
|
||||
/* Première phrase, coupée à n caractères au plus. */
|
||||
function firstSentence(t, n) {
|
||||
const m = t.match(/^.*?[.!?](\s|$)/);
|
||||
let s = (m ? m[0] : t).trim();
|
||||
if (s.length > n) s = s.slice(0, n - 1).replace(/\s+\S*$/, '') + '…';
|
||||
return s;
|
||||
}
|
||||
|
||||
function checkLen(errors, label, v, max) {
|
||||
if (v.length > max) errors.push(`${label} : ${max} caractères maximum (${v.length})`);
|
||||
}
|
||||
|
||||
function main() {
|
||||
const a = parseArgs(process.argv.slice(2));
|
||||
if (a.help) {
|
||||
console.log('Usage : node tools/add-news.js --themes t1,t2 --title "..." --text "..." [--simple-title ".." --simple-text ".."] [--dev-title ".." --dev-text ".."] [--short-title ".." --short-text ".."] [--link URL] [--image URL --image-alt "..."] [--date AAAA-MM-JJ] [--dry-run]\nThèmes : ' + THEMES.join(', '));
|
||||
return;
|
||||
}
|
||||
const errors = [];
|
||||
const date = a.date || todayLocal();
|
||||
const themes = (a.themes || '').split(',').map((s) => s.trim()).filter(Boolean);
|
||||
const title = (a.title || '').trim();
|
||||
const text = (a.text || '').trim();
|
||||
|
||||
if (!themes.length) errors.push('--themes est obligatoire (au moins un thème)');
|
||||
themes.forEach((t) => { if (!THEMES.includes(t)) errors.push(`thème inconnu « ${t} » (valides : ${THEMES.join(', ')})`); });
|
||||
if (new Set(themes).size !== themes.length) errors.push('--themes : thème répété');
|
||||
if (!title) errors.push('--title (niveau normal) est obligatoire');
|
||||
else checkLen(errors, '--title', title, LIMITS.title);
|
||||
if (!text) errors.push('--text (niveau normal) est obligatoire');
|
||||
else checkLen(errors, '--text', text, LIMITS.text);
|
||||
|
||||
const levels = { normal: { title, text } };
|
||||
[['simple', 'simple'], ['developpeur', 'dev']].forEach(([lvl, p]) => {
|
||||
const t = (a[p + '-title'] || '').trim();
|
||||
const x = (a[p + '-text'] || '').trim();
|
||||
if (!t && !x) return;
|
||||
if (!x) { errors.push(`--${p}-text est obligatoire si --${p}-title est donné`); return; }
|
||||
checkLen(errors, `--${p}-title`, t, LIMITS.title);
|
||||
checkLen(errors, `--${p}-text`, x, LIMITS.text);
|
||||
levels[lvl] = { title: t || title, text: x };
|
||||
});
|
||||
|
||||
const sTitle = (a['short-title'] || '').trim() || title;
|
||||
const sText = (a['short-text'] || '').trim() || firstSentence(text, LIMITS.shortText);
|
||||
checkLen(errors, '--short-title', sTitle, LIMITS.shortTitle);
|
||||
checkLen(errors, '--short-text', sText, LIMITS.shortText);
|
||||
|
||||
if (!validDate(date)) errors.push('--date doit être une vraie date au format AAAA-MM-JJ');
|
||||
if (a.link && !validUrl(a.link)) errors.push('--link doit commencer par / (page du site) ou https://');
|
||||
if (a.image && !validUrl(a.image)) errors.push('--image doit commencer par / ou https://');
|
||||
if (a.image && !a['image-alt']) errors.push('--image-alt est obligatoire quand il y a une image (accessibilité)');
|
||||
if (errors.length) { console.error('Entrée refusée :\n- ' + errors.join('\n- ')); process.exit(1); }
|
||||
|
||||
let list = [];
|
||||
if (fs.existsSync(FILE)) {
|
||||
try { list = JSON.parse(fs.readFileSync(FILE, 'utf8')); }
|
||||
catch (e) { console.error(`${FILE} n'est pas un JSON valide : ${e.message}`); process.exit(1); }
|
||||
if (!Array.isArray(list)) { console.error('actus.json doit contenir un tableau.'); process.exit(1); }
|
||||
}
|
||||
if (list.some((n) => n && n.date === date && n.levels && n.levels.normal && n.levels.normal.title === title)) {
|
||||
console.error('Une news avec le même titre et la même date existe déjà : rien écrit.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const id = list.reduce((m, n) => Math.max(m, Number(n && n.id) || 0), 0) + 1;
|
||||
const entry = { id, date, themes, short: { title: sTitle, text: sText }, levels };
|
||||
if (a.link) entry.link = a.link;
|
||||
if (a.image) { entry.image = a.image; entry.imageAlt = a['image-alt']; }
|
||||
|
||||
list.unshift(entry);
|
||||
list.sort((x, y) => (x.date === y.date ? y.id - x.id : x.date < y.date ? 1 : -1));
|
||||
const out = JSON.stringify(list, null, 2) + '\n';
|
||||
JSON.parse(out); // contrôle final
|
||||
|
||||
if (a['dry-run']) { console.log('[essai] rien écrit. Entrée :\n' + JSON.stringify(entry, null, 2)); return; }
|
||||
fs.copyFileSync(FILE, FILE + '.bak');
|
||||
fs.writeFileSync(FILE, out, 'utf8');
|
||||
console.log(`News n°${id} ajoutée (${date}, thèmes : ${themes.join(', ')}). Sauvegarde : data/actus.json.bak`);
|
||||
console.log('Suite : node tools/build.js puis node tools/deploy.js (lancés par toi).');
|
||||
}
|
||||
|
||||
try { main(); } catch (e) { console.error(e.message); process.exit(1); }
|
||||
Référencer dans un nouveau ticket
Bloquer un utilisateur