1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

Add TransformableRenderable capability

This commit is contained in:
Aidan Woods 2021-10-12 21:58:24 +01:00
parent a48c451d6c
commit 8fc49f6b0a
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
4 changed files with 80 additions and 3 deletions

View File

@ -3,8 +3,9 @@
namespace Erusev\Parsedown\Html\Renderables; namespace Erusev\Parsedown\Html\Renderables;
use Erusev\Parsedown\Html\Renderable; use Erusev\Parsedown\Html\Renderable;
use Erusev\Parsedown\Html\TransformableRenderable;
final class Container implements Renderable final class Container implements TransformableRenderable
{ {
use CanonicalStateRenderable; use CanonicalStateRenderable;
@ -44,4 +45,22 @@ final class Container implements Renderable
'' ''
); );
} }
/**
* @param \Closure(string):Renderable $Transform
* @return Renderable
*/
public function transformingContent(\Closure $Transform): Renderable
{
return new Container(\array_map(
function (Renderable $R) use ($Transform): Renderable {
if (! $R instanceof TransformableRenderable) {
return $R;
}
return $R->transformingContent($Transform);
},
$this->Contents
));
}
} }

View File

@ -5,8 +5,9 @@ namespace Erusev\Parsedown\Html\Renderables;
use Erusev\Parsedown\Html\Renderable; use Erusev\Parsedown\Html\Renderable;
use Erusev\Parsedown\Html\Sanitisation\CharacterFilter; use Erusev\Parsedown\Html\Sanitisation\CharacterFilter;
use Erusev\Parsedown\Html\Sanitisation\Escaper; use Erusev\Parsedown\Html\Sanitisation\Escaper;
use Erusev\Parsedown\Html\TransformableRenderable;
final class Element implements Renderable final class Element implements TransformableRenderable
{ {
use CanonicalStateRenderable; use CanonicalStateRenderable;
@ -191,4 +192,26 @@ final class Element implements Renderable
return $html; return $html;
} }
/**
* @param \Closure(string):Renderable $Transform
* @return Renderable
*/
public function transformingContent(\Closure $Transform): Renderable
{
if (! isset($this->Contents)) {
return $this;
}
return new self($this->name, $this->attributes, \array_map(
function (Renderable $R) use ($Transform): Renderable {
if (! $R instanceof TransformableRenderable) {
return $R;
}
return $R->transformingContent($Transform);
},
$this->Contents
));
}
} }

View File

@ -4,8 +4,9 @@ namespace Erusev\Parsedown\Html\Renderables;
use Erusev\Parsedown\Html\Renderable; use Erusev\Parsedown\Html\Renderable;
use Erusev\Parsedown\Html\Sanitisation\Escaper; use Erusev\Parsedown\Html\Sanitisation\Escaper;
use Erusev\Parsedown\Html\TransformableRenderable;
final class Text implements Renderable final class Text implements TransformableRenderable
{ {
use CanonicalStateRenderable; use CanonicalStateRenderable;
@ -31,4 +32,13 @@ final class Text implements Renderable
{ {
return Escaper::htmlElementValueEscapingDoubleQuotes($this->text); return Escaper::htmlElementValueEscapingDoubleQuotes($this->text);
} }
/**
* @param \Closure(string):Renderable $Transform
* @return Renderable
*/
public function transformingContent(\Closure $Transform): Renderable
{
return $Transform($this->text);
}
} }

View File

@ -0,0 +1,25 @@
<?php
namespace Erusev\Parsedown\Html;
interface TransformableRenderable extends Renderable
{
/**
* Takes a closure $Transform which will provide a transformation of
* a "contained text" into Renderables.
*
* In order for TransformableRenderable to make sense, a Renderable must
* have:
* 1. Some concept of "contained text". $Transform can be applied
* piece-wise if your container contains logically disjoint sections
* of text.
* 2. A generic mechanism for containing other Renderables, or replacing
* the current renderable with a container.
*
* It is acceptable to only partially transform "contained text".
*
* @param \Closure(string):Renderable $Transform
* @return Renderable
*/
public function transformingContent(\Closure $Transform): Renderable;
}