Initial ASK question data
All checks were successful
Validate QAML / validate (push) Successful in 1s
All checks were successful
Validate QAML / validate (push) Successful in 1s
This commit is contained in:
90
tools/validate_qaml.php
Normal file
90
tools/validate_qaml.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?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";
|
||||
Reference in New Issue
Block a user