Skip to content

Commit

Permalink
code format docs
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Mar 21, 2024
1 parent b69e7ba commit b5d4fb5
Show file tree
Hide file tree
Showing 23 changed files with 341 additions and 442 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
dependency-versions: ${{ matrix.dependencies }}

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

- name: "lint php"
run: "php -l docs_php/*.php"
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,8 @@ benchmark-diff-test: vendor #
dev: static test ## run dev tools

.PHONY: docs
docs: ## run mkdocs
cd docs && mkdocs serve
docs: mkdocs ## run mkdocs
cd docs && python3 -m mkdocs serve

mkdocs: ## run mkdocs
cd docs && pip3 install -r requirements.txt
10 changes: 2 additions & 8 deletions bin/extract-php-code-from-docs → bin/docs-extract-php-code
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@
<?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;
use Wnx\CommonmarkMarkdownRenderer\MarkdownRendererExtension;

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


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

$parser = new MarkdownParser($environment);

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

if (file_exists($targetDir)) {
Expand Down Expand Up @@ -53,7 +49,5 @@ foreach ($finder as $file) {

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

$node->setLiteral($source);
}
}
63 changes: 63 additions & 0 deletions bin/docs-inject-php-code
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env php
<?php

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
use League\CommonMark\Node\Query;
use League\CommonMark\Parser\MarkdownParser;
use Wnx\CommonmarkMarkdownRenderer\MarkdownRendererExtension;
use Wnx\CommonmarkMarkdownRenderer\Renderer\MarkdownRenderer;

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



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

$parser = new MarkdownParser($environment);
$markdownRenderer = new MarkdownRenderer($environment);

$targetDir = __DIR__ . '/../docs_php';

if (!file_exists($targetDir)) {
exit(1);
}

$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;
}

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

$lines = explode("\n", $code);
array_splice($lines, 0, 2);
$code = implode("\n", $lines);

$node->setLiteral(trim($code));
}

file_put_contents($file->getPathname(), $markdownRenderer->renderDocument($document));
}

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

0 comments on commit b5d4fb5

Please sign in to comment.