-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #357 from Roave/support-embedding-src-files
Adds support for embedding a file in code blocks
- Loading branch information
Showing
8 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Roave\DocbookTool\Formatter; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Roave\DocbookTool\DocbookPage; | ||
use Safe\Exceptions\SafeExceptionInterface; | ||
|
||
use function htmlentities; | ||
use function implode; | ||
use function preg_replace_callback; | ||
use function Safe\file_get_contents; | ||
use function sprintf; | ||
|
||
use const ENT_QUOTES; | ||
|
||
final class InlineCodeFromFile implements PageFormatter | ||
{ | ||
private const ALLOWED_CODE_TYPES = ['json']; | ||
|
||
public function __construct(private string $contentPath, private readonly LoggerInterface $logger) | ||
{ | ||
} | ||
|
||
/** @throws SafeExceptionInterface */ | ||
public function __invoke(DocbookPage $page): DocbookPage | ||
{ | ||
$this->logger->debug(sprintf('[%s] Checking if source code files can be inlined in %s', self::class, $page->slug())); | ||
|
||
return $page->withReplacedContent( | ||
preg_replace_callback( | ||
sprintf( | ||
'/{{src-(%s):([a-zA-Z0-9\/.-]+)}}/', | ||
implode('|', self::ALLOWED_CODE_TYPES), | ||
), | ||
function (array $m) use ($page): string { | ||
/** @var array{1: string, 2: string} $m */ | ||
$this->logger->debug(sprintf('[%s] Inlining source code file "%s" of type "%s" in page "%s"', self::class, $m[2], $m[1], $page->slug())); | ||
|
||
$sourceCode = file_get_contents($this->contentPath . '/' . $m[2]); | ||
|
||
return sprintf( | ||
'<pre><code class="lang-%s">%s</code></pre>', | ||
htmlentities($m[1], ENT_QUOTES), | ||
htmlentities($sourceCode, ENT_QUOTES), | ||
); | ||
}, | ||
$page->content(), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"example": "json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Roave\DocbookToolUnitTest\Formatter; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\NullLogger; | ||
use Roave\DocbookTool\DocbookPage; | ||
use Roave\DocbookTool\Formatter\InlineCodeFromFile; | ||
|
||
/** @covers \Roave\DocbookTool\Formatter\InlineCodeFromFile */ | ||
final class InlineCodeFromFileTest extends TestCase | ||
{ | ||
public function testExternalSourceCodeIsInlined(): void | ||
{ | ||
$markdown = <<<'MD' | ||
Here is some markdown | ||
{{src-json:example.json}} | ||
More markdown | ||
MD; | ||
|
||
$page = DocbookPage::fromSlugAndContent('/faked.md', 'slug', $markdown); | ||
|
||
$formattedPage = (new InlineCodeFromFile(__DIR__ . '/../../fixture/docbook', new NullLogger()))($page); | ||
|
||
self::assertSame( | ||
<<<'MD' | ||
Here is some markdown | ||
<pre><code class="lang-json">{ | ||
"example": "json" | ||
} | ||
</code></pre> | ||
More markdown | ||
MD, | ||
$formattedPage->content(), | ||
); | ||
} | ||
} |