Skip to content

Commit

Permalink
check docs
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Mar 21, 2024
1 parent a7ebc6e commit b69e7ba
Show file tree
Hide file tree
Showing 5 changed files with 584 additions and 3 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/docs_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# https://help.github.com/en/categories/automating-your-workflow-with-github-actions

name: "Check Docs"

on:
pull_request:
push:
branches:
- "[0-9]+.[0-9]+.x"
- "renovate/*"

jobs:
checkdocs:
name: "Check Docs"

runs-on: ${{ matrix.operating-system }}

strategy:
matrix:
dependencies:
- "locked"
php-version:
- "8.3"
operating-system:
- "ubuntu-latest"

steps:
- name: "Checkout"
uses: actions/checkout@v4

- name: "Install PHP"
uses: "shivammathur/[email protected]"
with:
coverage: "pcov"
php-version: "${{ matrix.php-version }}"
ini-values: memory_limit=-1
extensions: pdo_sqlite

- uses: ramsey/[email protected]
with:
dependency-versions: ${{ matrix.dependencies }}

- name: "extract php code"
run: "bin/extract-php-code-from-docs"

- name: "lint php"
run: "php -l docs_php/*.php"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ infection.log
.phpbench/
infection.html
*.sqlite3
.deptrac.cache
.deptrac.cache
docs_php/
59 changes: 59 additions & 0 deletions bin/extract-php-code-from-docs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env php
<?php

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
use League\CommonMark\Node\Query;
use League\CommonMark\Parser\MarkdownParser;

require __DIR__ . '/../vendor/autoload.php';


$environment = new Environment([]);
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addExtension(new GithubFlavoredMarkdownExtension());

$parser = new MarkdownParser($environment);

$path = __DIR__ . '/../docs/pages/subscription.md';
$targetDir = __DIR__ . '/../docs_php';

if (file_exists($targetDir)) {
exec('rm -rf ' . $targetDir);
}

mkdir($targetDir);

$finder = new Symfony\Component\Finder\Finder();
$finder->files()->in(__DIR__ . '/../docs/pages')->name('*.md');

foreach ($finder as $file) {
$fileName = pathinfo($file->getBasename(), PATHINFO_FILENAME);

$content = file_get_contents($file->getPathname());
$document = $parser->parse($content);

$result = (new Query())
->where(Query::type(FencedCode::class))
->findAll($document);

/**
* @var FencedCode $node
*/
foreach ($result as $node) {
if ($node->getInfo() !== 'php') {
continue;
}

$source = sprintf('%s:%s', $file->getRealPath(), $node->getStartLine());

$code = "<?php // " . $source . " \n\n" . $node->getLiteral() . "\n";

$targetPath = $targetDir . '/' . $fileName . '_' . $node->getStartLine() . '.php';
file_put_contents($targetPath, $code);

$node->setLiteral($source);
}
}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"cspray/phinal": "^2.0.0",
"doctrine/orm": "^2.18.0|^3.0.0",
"infection/infection": "^0.27.0",
"league/commonmark": "^2.4",
"patchlevel/coding-standard": "^1.3.0",
"patchlevel/event-sourcing-psalm-plugin": "^2.1.0",
"phpbench/phpbench": "^1.2.15",
Expand All @@ -48,7 +49,8 @@
"roave/infection-static-analysis-plugin": "^1.34.0",
"symfony/messenger": "^5.4.31|^6.4.0|^7.0.1",
"symfony/var-dumper": "^5.4.29|^6.4.0|^7.0.0",
"vimeo/psalm": "^5.17.0"
"vimeo/psalm": "^5.17.0",
"wnx/commonmark-markdown-renderer": "^1.4"
},
"suggest": {
"patchlevel/event-sourcing-psalm-plugin": "for psalm support"
Expand Down
Loading

0 comments on commit b69e7ba

Please sign in to comment.