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