Use positional data.txt syntax in admin
This commit is contained in:
parent
eefd745d55
commit
85f9d6e30f
1 changed files with 18 additions and 7 deletions
25
new.php
25
new.php
|
|
@ -9,19 +9,30 @@ function safe_input_dir($base, $name){ $name = basename((string)$name); $path =
|
||||||
function read_data($dir){
|
function read_data($dir){
|
||||||
$data = ['title'=>'','date'=>'','place'=>'','description'=>'']; $file = $dir . '/data.txt';
|
$data = ['title'=>'','date'=>'','place'=>'','description'=>'']; $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) {
|
||||||
if (!str_contains($line, ':')) continue;
|
$line = trim($line);
|
||||||
[$k,$v] = array_map('trim', explode(':', $line, 2));
|
if ($line === '' && ($header || $inDescription)) { $inDescription = true; continue; }
|
||||||
$k = strtolower($k); if ($k === 'location') $k = 'place';
|
if ($line === '' || str_starts_with($line, '#')) continue;
|
||||||
if (array_key_exists($k, $data)) $data[$k] = $v;
|
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;
|
return $data;
|
||||||
}
|
}
|
||||||
function write_data($dir, $post){
|
function write_data($dir, $post){
|
||||||
|
$title = trim($post['title'] ?? '');
|
||||||
|
$place = trim($post['place'] ?? '');
|
||||||
|
$date = trim($post['date'] ?? '');
|
||||||
|
$description = trim($post['description'] ?? '');
|
||||||
$lines = [];
|
$lines = [];
|
||||||
foreach (['title'=>'Title','date'=>'Date','place'=>'Place','description'=>'Description'] as $k=>$label) {
|
if ($title !== '') $lines[] = $title;
|
||||||
$v = trim($post[$k] ?? ''); if ($v !== '') $lines[] = "$label: $v";
|
if ($place !== '') $lines[] = $place;
|
||||||
}
|
if ($place !== '' && $date !== '') $lines[] = $date;
|
||||||
|
if ($description !== '') { $lines[] = ''; $lines[] = $description; }
|
||||||
file_put_contents($dir . '/data.txt', implode("\n", $lines) . "\n");
|
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; }
|
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; }
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue