diff --git a/new.php b/new.php index 1716fde..f4ba533 100644 --- a/new.php +++ b/new.php @@ -9,19 +9,30 @@ function safe_input_dir($base, $name){ $name = basename((string)$name); $path = function read_data($dir){ $data = ['title'=>'','date'=>'','place'=>'','description'=>'']; $file = $dir . '/data.txt'; if (!is_file($file)) return $data; + $header = []; $description = []; $inDescription = false; foreach (file($file, FILE_IGNORE_NEW_LINES) as $line) { - if (!str_contains($line, ':')) continue; - [$k,$v] = array_map('trim', explode(':', $line, 2)); - $k = strtolower($k); if ($k === 'location') $k = 'place'; - if (array_key_exists($k, $data)) $data[$k] = $v; + $line = trim($line); + if ($line === '' && ($header || $inDescription)) { $inDescription = true; continue; } + if ($line === '' || str_starts_with($line, '#')) continue; + if (str_contains($line, ':')) continue; // key:value is only for per-file captions + 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; } function write_data($dir, $post){ + $title = trim($post['title'] ?? ''); + $place = trim($post['place'] ?? ''); + $date = trim($post['date'] ?? ''); + $description = trim($post['description'] ?? ''); $lines = []; - foreach (['title'=>'Title','date'=>'Date','place'=>'Place','description'=>'Description'] as $k=>$label) { - $v = trim($post[$k] ?? ''); if ($v !== '') $lines[] = "$label: $v"; - } + if ($title !== '') $lines[] = $title; + if ($place !== '') $lines[] = $place; + if ($place !== '' && $date !== '') $lines[] = $date; + if ($description !== '') { $lines[] = ''; $lines[] = $description; } file_put_contents($dir . '/data.txt', implode("\n", $lines) . "\n"); } function list_files($dir){ $files = array_values(array_filter(scandir($dir), fn($f)=>$f!=='.' && $f!=='..' && is_file($dir.'/'.$f) && $f !== 'data.txt')); natcasesort($files); return $files; }