Implement custom parsers/renderers with autowiring for snip content
This commit is contained in:
44
src/Service/SnipParser/Generic/GenericParser.php
Normal file
44
src/Service/SnipParser/Generic/GenericParser.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\SnipParser\Generic;
|
||||
|
||||
use App\Service\SnipParser\AbstractParser;
|
||||
use League\Pipeline\PipelineBuilder;
|
||||
|
||||
class GenericParser extends AbstractParser
|
||||
{
|
||||
public static function getName(): string
|
||||
{
|
||||
return 'generic';
|
||||
}
|
||||
|
||||
public function __construct(
|
||||
private readonly UrlReferenceStage $referenceStage,
|
||||
private readonly IncludeReferenceStage $includeStage,
|
||||
) {}
|
||||
|
||||
public function parseView(string $content): string
|
||||
{
|
||||
$builder = new PipelineBuilder();
|
||||
$pipeline = $builder
|
||||
->add(new HtmlEscapeStage())
|
||||
->add(new ReplaceStage(PHP_EOL, '<br>'))
|
||||
->add(new ReplaceBlocksStage('<pre><code class="hljs">', '</code></pre>', '```'))
|
||||
->add(new ReplaceBlocksStage('<code class="hljs">', '</code>', '``'))
|
||||
->add($this->referenceStage)
|
||||
->add($this->includeStage)
|
||||
->build()
|
||||
;
|
||||
|
||||
return $pipeline->process($content);
|
||||
}
|
||||
|
||||
public function parseRaw(string $content): string
|
||||
{
|
||||
return str_replace(
|
||||
['```', '``'],
|
||||
'',
|
||||
$content
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user