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:
parent
a48c451d6c
commit
8fc49f6b0a
@ -3,8 +3,9 @@
|
||||
namespace Erusev\Parsedown\Html\Renderables;
|
||||
|
||||
use Erusev\Parsedown\Html\Renderable;
|
||||
use Erusev\Parsedown\Html\TransformableRenderable;
|
||||
|
||||
final class Container implements Renderable
|
||||
final class Container implements TransformableRenderable
|
||||
{
|
||||
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
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,9 @@ namespace Erusev\Parsedown\Html\Renderables;
|
||||
use Erusev\Parsedown\Html\Renderable;
|
||||
use Erusev\Parsedown\Html\Sanitisation\CharacterFilter;
|
||||
use Erusev\Parsedown\Html\Sanitisation\Escaper;
|
||||
use Erusev\Parsedown\Html\TransformableRenderable;
|
||||
|
||||
final class Element implements Renderable
|
||||
final class Element implements TransformableRenderable
|
||||
{
|
||||
use CanonicalStateRenderable;
|
||||
|
||||
@ -191,4 +192,26 @@ final class Element implements Renderable
|
||||
|
||||
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
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -4,8 +4,9 @@ namespace Erusev\Parsedown\Html\Renderables;
|
||||
|
||||
use Erusev\Parsedown\Html\Renderable;
|
||||
use Erusev\Parsedown\Html\Sanitisation\Escaper;
|
||||
use Erusev\Parsedown\Html\TransformableRenderable;
|
||||
|
||||
final class Text implements Renderable
|
||||
final class Text implements TransformableRenderable
|
||||
{
|
||||
use CanonicalStateRenderable;
|
||||
|
||||
@ -31,4 +32,13 @@ final class Text implements Renderable
|
||||
{
|
||||
return Escaper::htmlElementValueEscapingDoubleQuotes($this->text);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Closure(string):Renderable $Transform
|
||||
* @return Renderable
|
||||
*/
|
||||
public function transformingContent(\Closure $Transform): Renderable
|
||||
{
|
||||
return $Transform($this->text);
|
||||
}
|
||||
}
|
||||
|
25
src/Html/TransformableRenderable.php
Normal file
25
src/Html/TransformableRenderable.php
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user