Use key-value data file syntax

This commit is contained in:
hbrain 2026-05-25 20:51:47 +02:00
parent 7718e97d68
commit 1405abfcd7

36
new.php
View file

@ -9,22 +9,17 @@ function safe_input_dir($base, $name){ $name = basename((string)$name); $path =
function read_data($dir){ function read_data($dir){
$data = ['title'=>'','date'=>'','place'=>'','description'=>'','captions'=>[]]; $file = $dir . '/data.txt'; $data = ['title'=>'','date'=>'','place'=>'','description'=>'','captions'=>[]]; $file = $dir . '/data.txt';
if (!is_file($file)) return $data; if (!is_file($file)) return $data;
$header = []; $description = []; $inDescription = false;
foreach (file($file, FILE_IGNORE_NEW_LINES) as $line) { foreach (file($file, FILE_IGNORE_NEW_LINES) as $line) {
$line = trim($line); $line = trim($line);
if ($line === '' && ($header || $inDescription)) { $inDescription = true; continue; } if ($line === '' || str_starts_with($line, '#') || !str_contains($line, ':')) continue;
if ($line === '' || str_starts_with($line, '#')) continue; [$key, $value] = array_map('trim', explode(':', $line, 2));
if (str_contains($line, ':')) { $low = strtolower($key);
[$fileName, $caption] = array_map('trim', explode(':', $line, 2)); if (in_array($low, ['title','name'], true)) $data['title'] = $value;
if ($fileName !== '') $data['captions'][$fileName] = $caption; elseif (in_array($low, ['date','dates','when'], true)) $data['date'] = $value;
continue; elseif (in_array($low, ['place','location','where'], true)) $data['place'] = $value;
elseif (in_array($low, ['description','desc','synopsis'], true)) $data['description'] = $value;
elseif ($key !== '') $data['captions'][$key] = $value;
} }
if ($inDescription) $description[] = $line; else $header[] = $line;
}
if ($header) $data['title'] = $header[0];
if (count($header) > 1) $data['place'] = $header[1];
if (count($header) > 2) $data['date'] = $header[2];
if ($description) $data['description'] = implode("\n", $description);
return $data; return $data;
} }
function write_data($dir, $post){ function write_data($dir, $post){
@ -34,22 +29,17 @@ function write_data($dir, $post){
$description = trim($post['description'] ?? ''); $description = trim($post['description'] ?? '');
if (function_exists('mb_substr')) $description = mb_substr($description, 0, 5000, 'UTF-8'); else $description = substr($description, 0, 5000); if (function_exists('mb_substr')) $description = mb_substr($description, 0, 5000, 'UTF-8'); else $description = substr($description, 0, 5000);
$lines = []; $lines = [];
if ($title !== '') $lines[] = $title; if ($title !== '') $lines[] = 'Title: ' . $title;
if ($place !== '') $lines[] = $place; if ($place !== '') $lines[] = 'Location: ' . $place;
if ($place !== '' && $date !== '') $lines[] = $date; if ($date !== '') $lines[] = 'Date: ' . $date;
if ($description !== '') { $lines[] = ''; $lines[] = $description; } if ($description !== '') $lines[] = 'Description: ' . str_replace(["\r", "\n"], ' ', $description);
$captionFiles = $post['caption_files'] ?? []; $captionFiles = $post['caption_files'] ?? [];
$captions = $post['captions'] ?? []; $captions = $post['captions'] ?? [];
if (is_array($captionFiles) && is_array($captions)) { if (is_array($captionFiles) && is_array($captions)) {
$captionLines = [];
foreach ($captionFiles as $i => $fileName) { foreach ($captionFiles as $i => $fileName) {
$fileName = basename((string)$fileName); $fileName = basename((string)$fileName);
$caption = trim((string)($captions[$i] ?? '')); $caption = trim((string)($captions[$i] ?? ''));
if ($fileName !== '' && $caption !== '') $captionLines[] = $fileName . ': ' . $caption; if ($fileName !== '' && $caption !== '') $lines[] = $fileName . ': ' . $caption;
}
if ($captionLines) {
if ($description === '') $lines[] = '';
array_push($lines, ...$captionLines);
} }
} }
file_put_contents($dir . '/data.txt', implode("\n", $lines) . "\n"); file_put_contents($dir . '/data.txt', implode("\n", $lines) . "\n");