diff options
Diffstat (limited to 'scripts/update-library-dates.mjs')
| -rw-r--r-- | scripts/update-library-dates.mjs | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/scripts/update-library-dates.mjs b/scripts/update-library-dates.mjs index 2317b17..74b9302 100644 --- a/scripts/update-library-dates.mjs +++ b/scripts/update-library-dates.mjs @@ -25,7 +25,7 @@ const targetIdx = args.indexOf('--target'); const targetPath = targetIdx >= 0 ? args[targetIdx + 1] - : path.join('src', 'content', 'docs', 'kodaira-data', 'hp-archive', 'schedule.mdx'); + : path.join('src', 'data', 'schedule', 'events.json'); async function fetchText(url) { const res = await fetch(url, { headers: { 'User-Agent': UA } }); @@ -99,30 +99,28 @@ function formatDates(dateKeys, withWeekday) { } // 図書館のページ(info.html?id=…)を出典に含む行を対象にする。 -// 市報など他の出典と併記している行(例:「市報こだいら10月20日号3面/[図書館のページ](…)」)も拾う。 -const ROW_RE = - /^\| ([^|]*) \| ([^|]*) \| ([^|]*) \| ([^|]*) \| ([^|]*library\.kodaira\.ed\.jp\/viewer\/info\.html\?id=(\d+)[^|]*) \|$/; +// 市報など他の出典と併記している行(例:「市報こだいら10月20日号3面/<a …info.html?id=…>」)も拾う。 +const LIB_SOURCE_RE = /library\.kodaira\.ed\.jp\/viewer\/info\.html\?id=(\d+)/; async function main() { const events = await collectEvents(); - const src = fs.readFileSync(targetPath, 'utf8'); - const lines = src.split('\n'); + const data = JSON.parse(fs.readFileSync(targetPath, 'utf8')); + const rows = data.rows; const inTable = new Map(); const changes = []; - lines.forEach((line, index) => { - // Windows の CRLF でも一致するよう、照合時だけ行末の \r を除く - const m = line.replace(/\r$/, '').match(ROW_RE); - if (!m) return; - const [, datesCell, name, , , , id] = m; - inTable.set(id, { index, name, datesCell }); + for (const row of rows) { + const m = (row.source || '').match(LIB_SOURCE_RE); + if (!m) continue; + const id = m[1]; + inTable.set(id, { name: row.title, datesCell: row.date, row }); const ev = events.get(id); - if (!ev || ev.dates.size === 0) return; + if (!ev || ev.dates.size === 0) continue; const next = formatDates(ev.dates, ev.dates.size === 1); - if (next !== datesCell) changes.push({ index, name, id, from: datesCell, to: next }); - }); + if (next !== row.date) changes.push({ name: row.title, id, from: row.date, to: next, row }); + } console.log(`図書館の予定: カレンダー ${events.size}件 / 表 ${inTable.size}件(${months}か月分を取得)`); @@ -133,7 +131,7 @@ async function main() { console.log(`- ${c.name}`); console.log(` 現在: ${c.from}`); console.log(` 新規: ${c.to}`); - if (apply) lines[c.index] = lines[c.index].replace(`| ${c.from} |`, `| ${c.to} |`); + if (apply) c.row.date = c.to; } } @@ -152,7 +150,7 @@ async function main() { } if (apply && changes.length > 0) { - fs.writeFileSync(targetPath, lines.join('\n'), 'utf8'); + fs.writeFileSync(targetPath, JSON.stringify(data, null, 2) + '\n', 'utf8'); console.log(`\n${changes.length}件を ${targetPath} に反映しました。`); } else if (!apply && changes.length > 0) { console.log('\n(--apply を付けると反映します)'); |
