diff --git a/.travis-build.php b/.travis-build.php new file mode 100644 index 0000000..9333350 --- /dev/null +++ b/.travis-build.php @@ -0,0 +1,70 @@ +setFlags(SplFileObject::DROP_NEW_LINE); + +$cliRedBackground = "\033[37;41m"; +$cliReset = "\033[0m"; +$exitStatus = 0; + +$indentationSteps = 3; +$manIndex = 0; +$linesWithSpaces = []; +$tableOfContentsStarted = null; +$currentTableOfContentsChapters = []; +$chaptersFound = []; +foreach ($readMeFile as $lineNumber => $line) { + if (preg_match('/\s$/', $line)) { + $linesWithSpaces[] = sprintf('%5s: %s', 1 + $lineNumber, $line); + } + if (preg_match('/^(?##+)\s(?.+)/', $line, $matches)) { + if (null === $tableOfContentsStarted) { + $tableOfContentsStarted = true; + continue; + } + $tableOfContentsStarted = false; + + $chaptersFound[] = sprintf('%s [%s](#%s)', + strlen($matches['depth']) === 2 + ? sprintf(' %s.', ++$manIndex) + : ' *' + , + $matches['title'], + preg_replace(['/ /', '/[^-\w]+/'], ['-', ''], strtolower($matches['title'])) + ); + } + if ($tableOfContentsStarted === true && isset($line[0])) { + $currentTableOfContentsChapters[] = $line; + } +} + +if (count($linesWithSpaces)) { + fwrite(STDERR, sprintf("${cliRedBackground}The following lines end with a space character:${cliReset}\n%s\n\n", + implode(PHP_EOL, $linesWithSpaces) + )); + $exitStatus = 1; +} + +$currentTableOfContentsChaptersFilename = __DIR__ . '/current-chapters'; +$chaptersFoundFilename = __DIR__ . '/chapters-found'; + +file_put_contents($currentTableOfContentsChaptersFilename, implode(PHP_EOL, $currentTableOfContentsChapters)); +file_put_contents($chaptersFoundFilename, implode(PHP_EOL, $chaptersFound)); + +$tableOfContentsDiff = shell_exec(sprintf('diff --unified %s %s', + escapeshellarg($currentTableOfContentsChaptersFilename), + escapeshellarg($chaptersFoundFilename) +)); + +@ unlink($currentTableOfContentsChaptersFilename); +@ unlink($chaptersFoundFilename); + +if (!empty($tableOfContentsDiff)) { + fwrite(STDERR, sprintf("${cliRedBackground}The table of contents is not aligned:${cliReset}\n%s\n\n", + $tableOfContentsDiff + )); + $exitStatus = 1; +} + +exit($exitStatus); diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..e4ea357 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,11 @@ +language: php + +sudo: false + +php: + - nightly + +script: php .travis-build.php + +notifications: + email: false