mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Replacing all function to TransformableRenderable
This commit is contained in:
parent
a9f41548d3
commit
e9dec33dc6
@ -15,7 +15,7 @@ final class Container implements TransformableRenderable
|
|||||||
/**
|
/**
|
||||||
* @param Renderable[] $Contents
|
* @param Renderable[] $Contents
|
||||||
*/
|
*/
|
||||||
public function __construct($Contents)
|
public function __construct($Contents = [])
|
||||||
{
|
{
|
||||||
$this->Contents = $Contents;
|
$this->Contents = $Contents;
|
||||||
}
|
}
|
||||||
@ -67,4 +67,18 @@ final class Container implements TransformableRenderable
|
|||||||
$this->Contents
|
$this->Contents
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function replacingAll(string $search, Renderable $Replacement): Renderable
|
||||||
|
{
|
||||||
|
return new Container(\array_map(
|
||||||
|
function (Renderable $R) use ($search, $Replacement): Renderable {
|
||||||
|
if (! $R instanceof TransformableRenderable) {
|
||||||
|
return $R;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $R->replacingAll($search, $Replacement);
|
||||||
|
},
|
||||||
|
$this->Contents
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -214,4 +214,22 @@ final class Element implements TransformableRenderable
|
|||||||
$this->Contents
|
$this->Contents
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function replacingAll(string $search, Renderable $Replacement): Renderable
|
||||||
|
{
|
||||||
|
if (! isset($this->Contents)) {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new self($this->name, $this->attributes, \array_map(
|
||||||
|
function (Renderable $R) use ($search, $Replacement): Renderable {
|
||||||
|
if (! $R instanceof TransformableRenderable) {
|
||||||
|
return $R;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $R->replacingAll($search, $Replacement);
|
||||||
|
},
|
||||||
|
$this->Contents
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,4 +41,50 @@ final class Text implements TransformableRenderable
|
|||||||
{
|
{
|
||||||
return $Transform($this->text);
|
return $Transform($this->text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function replacingAll(string $search, Renderable $Replacement): Renderable
|
||||||
|
{
|
||||||
|
$searchLen = \strlen($search);
|
||||||
|
|
||||||
|
if ($searchLen < 1) {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = \preg_match_all(
|
||||||
|
'/\b'.\preg_quote($search, '/').'\b/',
|
||||||
|
$this->text,
|
||||||
|
$matches,
|
||||||
|
\PREG_OFFSET_CAPTURE
|
||||||
|
);
|
||||||
|
|
||||||
|
if (empty($result)) {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
$lastEndPos = 0;
|
||||||
|
|
||||||
|
$Container = new Container;
|
||||||
|
|
||||||
|
foreach ($matches[0] as $match) {
|
||||||
|
$pos = $match[1];
|
||||||
|
$endPos = $pos + $searchLen;
|
||||||
|
|
||||||
|
if ($pos !== $lastEndPos) {
|
||||||
|
$Container = $Container->adding(
|
||||||
|
new Text(\substr($this->text, $lastEndPos, $pos - $lastEndPos))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$Container = $Container->adding($Replacement);
|
||||||
|
$lastEndPos = $endPos;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (\strlen($this->text) -1 !== $lastEndPos) {
|
||||||
|
$Container = $Container->adding(
|
||||||
|
new Text(\substr($this->text, $lastEndPos))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $Container;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,4 +22,10 @@ interface TransformableRenderable extends Renderable
|
|||||||
* @return Renderable
|
* @return Renderable
|
||||||
*/
|
*/
|
||||||
public function transformingContent(\Closure $Transform): Renderable;
|
public function transformingContent(\Closure $Transform): Renderable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Similar to transformingContent, but replace the string $search in text content
|
||||||
|
* with the renderable $Replacement and return the result.
|
||||||
|
*/
|
||||||
|
public function replacingAll(string $search, Renderable $Replacement): Renderable;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user