Skip to content

Commit

Permalink
Add codeSelector block format (#59)
Browse files Browse the repository at this point in the history
* feat(reference): codeselector generation

* docs(reference): apiresource
  • Loading branch information
Byidi authored Jan 13, 2023
1 parent 5869d00 commit a361602
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 1 deletion.
63 changes: 63 additions & 0 deletions docs/pages/_document.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,69 @@ export default function Document() {
white-space: normal !important;
word-break: break-word !important;
}
.code-selector {
overflow-x: hidden;
margin: 32px 0;
padding-bottom: 16px;
border-bottom: 1px solid #ccc;
}
.code-selector input {
display: none;
}
.code-selector .code-selector-nav {
display: flex;
align-items: stretch;
list-style: none;
padding: 0;
border-bottom: 1px solid #ccc;
}
.code-selector label {
display: block;
margin-bottom: -1px;
padding: 12px 15px;
border: 1px solid #ccc;
background: #eee;
color: #666;
font-size: 12px;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 1px;
cursor: pointer;
transition: all 0.3s;
}
.code-selector label:hover {
border-top-color: #333;
color: #333;
}
.code-selector .code-selector-content {
display: none;
color: #777;
}
.code-selector input:nth-of-type(1):checked ~ .code-selector-nav label:nth-of-type(1),
.code-selector input:nth-of-type(2):checked ~ .code-selector-nav label:nth-of-type(2),
.code-selector input:nth-of-type(3):checked ~ .code-selector-nav label:nth-of-type(3),
.code-selector input:nth-of-type(4):checked ~ .code-selector-nav label:nth-of-type(4),
.code-selector input:nth-of-type(5):checked ~ .code-selector-nav label:nth-of-type(5) {
border-bottom-color: #fff;
border-top-color: #B721FF;
background: #fff;
color: #222;
}
.code-selector input:nth-of-type(1):checked ~ .code-selector-content:nth-of-type(1),
.code-selector input:nth-of-type(2):checked ~ .code-selector-content:nth-of-type(2),
.code-selector input:nth-of-type(3):checked ~ .code-selector-content:nth-of-type(3),
.code-selector input:nth-of-type(4):checked ~ .code-selector-content:nth-of-type(4),
.code-selector input:nth-of-type(5):checked ~ .code-selector-content:nth-of-type(5) {
display: block !important;
}
`
return (
<Html>
Expand Down
15 changes: 14 additions & 1 deletion docs/src/Command/GuideCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace PDG\Command;

use PDG\Services\Reference\OutputFormatter;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -14,6 +15,14 @@ class GuideCommand extends Command
{
// Regular expression to match comment
private const REGEX = '/^\s*\/\/\s/';

public function __construct(
private readonly OutputFormatter $outputFormatter,
string $name = null
) {
parent::__construct($name);
}

protected function configure(): void
{
$this
Expand Down Expand Up @@ -128,7 +137,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
<a className="anchor" href="#section-$i">&#x00a7;</a>
MD;
$a .= implode('', $section['text'] ?: [\PHP_EOL]);
$text = implode('', $section['text'] ?: [\PHP_EOL]);
$a .= str_contains($text,'[codeSelector]')
? $this->outputFormatter->formatCodeSelector($text)
: $text
;
$a .= <<<MD
</div>
<div className="content">
Expand Down
41 changes: 41 additions & 0 deletions docs/src/Services/Reference/OutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,35 @@ public function linkClasses(\ReflectionType|\ReflectionNamedType $reflectionName
return $name;
}

public function formatCodeSelector(string $content): string
{
$codeSelectorId= \uniqid();
if (false !== \preg_match_all('/```(\w+)/', $content, $languages) && $languages) {
$inputs = '';
$nav = '<ul class="code-selector-nav">'.\PHP_EOL;
foreach($languages[1] as $k => $language){
$defaultChecked = $k === 0 ? 'defaultChecked' : '';
$inputs .= '<input type="radio" id="'.$codeSelectorId.'-'.$language.'" name="'.$codeSelectorId.'-code-tabs" '.$defaultChecked.' />'.\PHP_EOL;
$nav .= '<label for="'.$codeSelectorId.'-'.$language.'">'.$language.'</label>'.\PHP_EOL;
}
$nav .= '</ul>'.\PHP_EOL;
}

$content = preg_replace(
'/\[codeSelector\]([\w\s\S\n]*?)\[\/codeSelector\]/i',
'<div class="code-selector">'.\PHP_EOL.$inputs.$nav.'${1}'.\PHP_EOL.'</div>'.\PHP_EOL,
$content,
);

$content = \preg_replace(
'/(```\w+\n[\w\s\S\n]*?```)/i',
'<div class="code-selector-content">'.\PHP_EOL.'${1}'.\PHP_EOL.'</div>'.\PHP_EOL,
$content,
);

return $content;
}

public function printTextNodes(PhpDocNode $phpDoc, string $content): string
{
$text = array_filter($phpDoc->children, static function (PhpDocChildNode $child): bool {
Expand All @@ -74,6 +103,18 @@ public function printTextNodes(PhpDocNode $phpDoc, string $content): string
$content .= $t.\PHP_EOL;
}

$explodedByCodeBlock = preg_split('/(\[codeSelector\][\s\S\w\n]*?\[\/codeSelector\])/', $content, 0, PREG_SPLIT_DELIM_CAPTURE);

$content = '';
foreach($explodedByCodeBlock as $contentBlock){
if(str_contains($contentBlock, 'codeSelector')){
$content .= $this->formatCodeSelector($contentBlock);
continue;
}

$content .= $contentBlock;
}

return $content;
}

Expand Down
37 changes: 37 additions & 0 deletions src/Metadata/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,43 @@ public function __construct(
protected ?string $condition = null,
protected ?string $controller = null,
protected ?string $class = null,
/**
* The `urlGenerationStrategy` option configures the url generation strategy.
*
* See: [UrlGeneratorInterface::class](/reference/Api/UrlGeneratorInterface)
*
* [codeSelector]
* ```php
* <?php
* // api/src/Entity/Book.php
* use ApiPlatform\Metadata\ApiResource;
* use ApiPlatform\Api\UrlGeneratorInterface;
*
* #[ApiResource(urlGenerationStrategy: UrlGeneratorInterface::ABS_URL)]
* class Book
* {
* // ...
* }
* ```
* ```yaml
* # api/config/api_platform/resources.yaml
* App\Entity\Book:
* urlGenerationStrategy: !php/const ApiPlatform\Api\UrlGeneratorInterface::ABS_URL
* ```
* ```xml
* <?xml version="1.0" encoding="UTF-8" ?>
* <!-- api/config/api_platform/resources.xml -->
*
* <resources
* xmlns="https://api-platform.com/schema/metadata/resources-3.0"
* xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
* xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
* https://api-platform.com/schema/metadata/resources-3.0.xsd">
* <resource class="App\Entity\Book" urlGenerationStrategy="0" />
* </resources>
* ```
* [/codeSelector]
*/
protected ?int $urlGenerationStrategy = null,
protected ?string $deprecationReason = null,
protected ?array $cacheHeaders = null,
Expand Down

0 comments on commit a361602

Please sign in to comment.