91 lines
2.0 KiB
PHP
91 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
$file = $argv[1] ?? 'pytania.txt';
|
|
$baseDir = dirname(__DIR__);
|
|
$path = $baseDir . '/' . $file;
|
|
|
|
if (!is_file($path)) {
|
|
fwrite(STDERR, "File not found: {$file}\n");
|
|
exit(1);
|
|
}
|
|
|
|
$lines = file($path, FILE_IGNORE_NEW_LINES);
|
|
$errors = [];
|
|
$question = null;
|
|
$questionLine = 0;
|
|
$answers = 0;
|
|
$questionCount = 0;
|
|
|
|
$finishQuestion = static function () use (&$errors, &$question, &$questionLine, &$answers, &$questionCount): void {
|
|
if ($question === null) {
|
|
return;
|
|
}
|
|
|
|
if ($answers === 0) {
|
|
$errors[] = "Line {$questionLine}: question has no answers.";
|
|
}
|
|
|
|
$questionCount++;
|
|
$question = null;
|
|
$questionLine = 0;
|
|
$answers = 0;
|
|
};
|
|
|
|
foreach ($lines as $i => $rawLine) {
|
|
$lineNo = $i + 1;
|
|
$line = trim($rawLine);
|
|
|
|
if ($line === '' || str_starts_with($line, '//')) {
|
|
continue;
|
|
}
|
|
|
|
if (!str_starts_with($line, '-')) {
|
|
$finishQuestion();
|
|
$question = $line;
|
|
$questionLine = $lineNo;
|
|
continue;
|
|
}
|
|
|
|
if ($question === null) {
|
|
$errors[] = "Line {$lineNo}: answer appears before any question.";
|
|
continue;
|
|
}
|
|
|
|
if (str_starts_with($line, '-|')) {
|
|
$answer = trim(substr($line, 2));
|
|
} else {
|
|
$answer = trim(substr($line, 1));
|
|
}
|
|
|
|
if ($answer === '') {
|
|
$errors[] = "Line {$lineNo}: answer is empty.";
|
|
}
|
|
|
|
$answers++;
|
|
}
|
|
|
|
$finishQuestion();
|
|
|
|
$content = file_get_contents($path) ?: '';
|
|
preg_match_all('/<img\s+[^>]*src=["\']([^"\']+)["\'][^>]*>/i', $content, $matches);
|
|
foreach ($matches[1] ?? [] as $src) {
|
|
if (str_starts_with($src, 'img/') && !is_file($baseDir . '/' . $src)) {
|
|
$errors[] = "Missing image referenced from pytania.txt: {$src}";
|
|
}
|
|
}
|
|
|
|
if ($questionCount === 0) {
|
|
$errors[] = 'No questions found.';
|
|
}
|
|
|
|
if ($errors !== []) {
|
|
foreach ($errors as $error) {
|
|
fwrite(STDERR, $error . PHP_EOL);
|
|
}
|
|
exit(1);
|
|
}
|
|
|
|
echo "OK: {$questionCount} questions validated.\n";
|