-
Notifications
You must be signed in to change notification settings - Fork 5
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 #251 from Roave/support-markdown-images
New feature: support images in markdown
- Loading branch information
Showing
16 changed files
with
345 additions
and
69 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Roave\DocbookTool\Formatter; | ||
|
||
use Roave\DocbookTool\DocbookPage; | ||
use RuntimeException; | ||
|
||
use function array_key_exists; | ||
use function base64_encode; | ||
use function getimagesize; | ||
use function is_array; | ||
use function is_string; | ||
use function preg_replace_callback; | ||
use function Safe\file_get_contents; | ||
use function sprintf; | ||
|
||
final class InlineExternalImages implements PageFormatter | ||
{ | ||
public function __construct(private readonly string $docbookPath) | ||
{ | ||
} | ||
|
||
/** @throws RuntimeException */ | ||
public function __invoke(DocbookPage $page): DocbookPage | ||
{ | ||
return $page->withReplacedContent( | ||
preg_replace_callback( | ||
'/!\[([^]]+)]\(([^)]*?)\)/', | ||
function (array $m) { | ||
/** @var array{1: string, 2: string} $m */ | ||
$altText = $m[1]; | ||
$imagePath = $m[2]; | ||
|
||
$fullImagePath = $this->docbookPath . '/' . $imagePath; | ||
|
||
$imageContent = file_get_contents($fullImagePath); | ||
|
||
$imageInfo = getimagesize($fullImagePath); | ||
|
||
if (! is_array($imageInfo) || ! array_key_exists('mime', $imageInfo) || ! is_string($imageInfo['mime'])) { | ||
throw new RuntimeException('Unable to determine mime type of ' . $fullImagePath); | ||
} | ||
|
||
return sprintf( | ||
'![%s](data:%s;base64,%s)', | ||
$altText, | ||
$imageInfo['mime'], | ||
base64_encode($imageContent), | ||
); | ||
}, | ||
$page->content(), | ||
), | ||
); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.