exporter-registre.mjs : sans --registre, AEP_REGISTRE. registre_lib.py : AEP_REGISTRE, puis REGISTRE. test-bibliotheque.mjs : AEP_REGISTRE, puis BIBLIOTHEQUE_REGISTRE, puis le registre du vault. Sert à éprouver un registre candidat sans toucher au vault. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
5.5 KiB
JavaScript
105 lines
5.5 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Export du registre des œuvres pour la bibliothèque navigable (lot B9).
|
|
*
|
|
* Usage :
|
|
* node scripts/bibliotheque/exporter-registre.mjs --registre <registre.json> --jeux <dossier des jeux M6b> \
|
|
* [--sortie data/bibliotheque/oeuvres-registre.json]
|
|
* Sans --registre : la variable AEP_REGISTRE (lot 13 bis, pour éprouver un registre candidat).
|
|
*
|
|
* Lit le registre du vault (lecture seule) et le dossier des jeux M6b (un `<slug>/<slug>__extraction.json`
|
|
* par œuvre lue, pour joindre l'identifiant du registre au slug de la fiche) ; écrit un seul fichier,
|
|
* commité. Le site ne lit jamais le vault ni ~/rag.
|
|
*
|
|
* LISTE BLANCHE : seuls les champs de CHAMPS sortent, recopiés un par un. Un champ du registre qui n'y est
|
|
* pas (licence, source d'acquisition, notes, variantes, confiance…) ne sort pas, quel qu'il soit.
|
|
* id · titre (nom_canonique) · auteurs (identifiants, noms dans `personnes`) · annee (annee, sinon
|
|
* date_debut) · epoque (relation situe_a, sinon l'époque qui contient l'année) · courants (courants
|
|
* thématiques ; un courant historique remonte à son courant thématique) · regime (les œuvres lues) ·
|
|
* dans_corpus · fiche (slug du socle, ou null) · couche (« ancienne » : le registre et le corpus).
|
|
*/
|
|
import { readFileSync, writeFileSync, readdirSync, existsSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
|
|
export const CHAMPS = ['id', 'titre', 'auteurs', 'annee', 'epoque', 'courants', 'regime', 'dans_corpus', 'fiche', 'couche']
|
|
export const PREFIXE_THEME = 'cou:t-'
|
|
|
|
/** Le registre (objet JSON) et la table identifiant → slug des œuvres lues → l'export. Fonction pure. */
|
|
export function exporter(R, slugs, { date = new Date().toISOString().slice(0, 10) } = {}) {
|
|
const N = new Map(R.noeuds.map((x) => [x.id, x]))
|
|
const epoques = R.noeuds.filter((x) => x.type === 'epoque')
|
|
.map((e) => ({ id: e.id, nom: e.nom_canonique, debut: e.date_debut, fin: e.date_fin }))
|
|
.sort((a, b) => a.debut - b.debut)
|
|
const courants = R.noeuds.filter((x) => x.type === 'courant' && x.id.startsWith(PREFIXE_THEME))
|
|
.map((c) => ({ id: c.id, nom: c.nom_canonique.replace(/\s*\(contre-point\)\s*$/, ''), contre_point: /\(contre-point\)/.test(c.nom_canonique) }))
|
|
const rangCourant = new Map(courants.map((c, i) => [c.id, i]))
|
|
const situe = new Map()
|
|
for (const r of R.relations) if (r.type === 'situe_a' && N.get(r.source_id)?.type === 'oeuvre' && N.get(r.cible_id)?.type === 'epoque') situe.set(r.source_id, r.cible_id)
|
|
const epoqueDeAnnee = (a) => (a == null ? null : epoques.find((e) => e.debut <= a && a <= e.fin)?.id ?? null)
|
|
const themes = (o) => {
|
|
const out = new Set()
|
|
for (const c of o.courants ?? []) {
|
|
if (c.startsWith(PREFIXE_THEME)) out.add(c)
|
|
else for (const p of N.get(c)?.courants ?? []) if (p.startsWith(PREFIXE_THEME)) out.add(p)
|
|
}
|
|
return [...out].filter((c) => rangCourant.has(c)).sort((a, b) => rangCourant.get(a) - rangCourant.get(b))
|
|
}
|
|
const personnes = {}
|
|
const oeuvres = R.noeuds.filter((x) => x.type === 'oeuvre').map((o) => {
|
|
const annee = o.annee ?? o.date_debut ?? null
|
|
for (const a of o.auteur_ids ?? []) {
|
|
const p = N.get(a)
|
|
if (!p) throw new Error(`${o.id} : auteur ${a} absent du registre`)
|
|
personnes[a] = p.nom_canonique
|
|
}
|
|
// Recopie champ par champ : rien d'autre ne peut sortir
|
|
return {
|
|
id: o.id,
|
|
titre: o.nom_canonique,
|
|
auteurs: [...(o.auteur_ids ?? [])],
|
|
annee,
|
|
epoque: situe.get(o.id) ?? epoqueDeAnnee(annee),
|
|
courants: themes(o),
|
|
regime: o.regime ?? null,
|
|
dans_corpus: !!o.dans_corpus,
|
|
fiche: slugs.get(o.id) ?? null,
|
|
couche: 'ancienne',
|
|
}
|
|
}).sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0))
|
|
const triees = Object.fromEntries(Object.entries(personnes).sort(([a], [b]) => (a < b ? -1 : 1)))
|
|
return { version: 1, source: { registre_maj: R.maj, exporte_le: date }, champs: CHAMPS, personnes: triees, epoques, courants, oeuvres }
|
|
}
|
|
|
|
/** id du registre → slug, depuis `<jeux>/<slug>/<slug>__extraction.json`. */
|
|
export function slugsDesJeux(jeux) {
|
|
const m = new Map()
|
|
for (const slug of readdirSync(jeux)) {
|
|
const f = join(jeux, slug, `${slug}__extraction.json`)
|
|
if (!existsSync(f)) continue
|
|
const { id } = JSON.parse(readFileSync(f, 'utf8'))
|
|
if (m.has(id)) throw new Error(`deux jeux pour ${id}`)
|
|
m.set(id, slug)
|
|
}
|
|
return m
|
|
}
|
|
|
|
const principal = process.argv[1] && import.meta.url.endsWith(process.argv[1].split('/').pop())
|
|
if (principal) {
|
|
const a = process.argv.slice(2)
|
|
const arg = (k, d) => (a.includes(k) ? a[a.indexOf(k) + 1] : d)
|
|
const registre = arg('--registre', process.env.AEP_REGISTRE) // lot 13 bis : AEP_REGISTRE, si --registre manque
|
|
const jeux = arg('--jeux')
|
|
const sortie = arg('--sortie', 'data/bibliotheque/oeuvres-registre.json')
|
|
if (!registre || !jeux) {
|
|
console.error('usage : exporter-registre.mjs --registre <registre.json> --jeux <dossier des jeux M6b> [--sortie <fichier>]')
|
|
process.exit(2)
|
|
}
|
|
const R = JSON.parse(readFileSync(registre, 'utf8'))
|
|
const slugs = slugsDesJeux(jeux)
|
|
const ex = exporter(R, slugs)
|
|
const sansFiche = [...slugs.keys()].filter((id) => !ex.oeuvres.some((o) => o.id === id))
|
|
if (sansFiche.length) throw new Error(`jeux sans œuvre au registre : ${sansFiche.join(', ')}`)
|
|
writeFileSync(sortie, `${JSON.stringify(ex, null, 1)}\n`)
|
|
console.log(JSON.stringify({ oeuvres: ex.oeuvres.length, fiches: ex.oeuvres.filter((o) => o.fiche).length, personnes: Object.keys(ex.personnes).length, champs: CHAMPS.length, sortie }))
|
|
}
|