diff options
Diffstat (limited to 'ratatoeskr/sys/textprocessors')
-rw-r--r-- | ratatoeskr/sys/textprocessors/HtmlProcessor.php | 20 | ||||
-rw-r--r-- | ratatoeskr/sys/textprocessors/LegacyTextprocessor.php | 32 | ||||
-rw-r--r-- | ratatoeskr/sys/textprocessors/MarkdownProcessor.php | 22 | ||||
-rw-r--r-- | ratatoeskr/sys/textprocessors/PlainTextProcessor.php | 22 | ||||
-rw-r--r-- | ratatoeskr/sys/textprocessors/Textprocessor.php | 19 | ||||
-rw-r--r-- | ratatoeskr/sys/textprocessors/TextprocessorRepository.php | 76 |
6 files changed, 191 insertions, 0 deletions
diff --git a/ratatoeskr/sys/textprocessors/HtmlProcessor.php b/ratatoeskr/sys/textprocessors/HtmlProcessor.php new file mode 100644 index 0000000..c3f691b --- /dev/null +++ b/ratatoeskr/sys/textprocessors/HtmlProcessor.php @@ -0,0 +1,20 @@ +<?php + + +namespace r7r\cms\sys\textprocessors; + +/** + * A simple textprocessor that assumes the input already is HTML. + */ +class HtmlProcessor implements Textprocessor +{ + public function apply(string $input): string + { + return $input; + } + + public function showInBackend(): bool + { + return true; + } +} diff --git a/ratatoeskr/sys/textprocessors/LegacyTextprocessor.php b/ratatoeskr/sys/textprocessors/LegacyTextprocessor.php new file mode 100644 index 0000000..e7e5cc2 --- /dev/null +++ b/ratatoeskr/sys/textprocessors/LegacyTextprocessor.php @@ -0,0 +1,32 @@ +<?php + + +namespace r7r\cms\sys\textprocessors; + +/** + * LegacyTextprocessor is used to wrap an old-style textprocessor into an Textprocessor object. + */ +class LegacyTextprocessor implements Textprocessor +{ + /** @var callable */ + private $fx; + + /** @var bool */ + private $visible_in_backend; + + public function __construct(callable $fx, $visible_in_backend) + { + $this->fx = $fx; + $this->visible_in_backend = (bool)$visible_in_backend; + } + + public function apply(string $input): string + { + return (string)call_user_func($this->fx, $input); + } + + public function showInBackend(): bool + { + return $this->visible_in_backend; + } +} diff --git a/ratatoeskr/sys/textprocessors/MarkdownProcessor.php b/ratatoeskr/sys/textprocessors/MarkdownProcessor.php new file mode 100644 index 0000000..15f2f48 --- /dev/null +++ b/ratatoeskr/sys/textprocessors/MarkdownProcessor.php @@ -0,0 +1,22 @@ +<?php + + +namespace r7r\cms\sys\textprocessors; + +use Michelf\Markdown; + +/** + * A textprocessor that uses markdown to generate HTML. + */ +class MarkdownProcessor implements Textprocessor +{ + public function apply(string $input): string + { + return Markdown::defaultTransform($input); + } + + public function showInBackend(): bool + { + return true; + } +} diff --git a/ratatoeskr/sys/textprocessors/PlainTextProcessor.php b/ratatoeskr/sys/textprocessors/PlainTextProcessor.php new file mode 100644 index 0000000..c8f13f6 --- /dev/null +++ b/ratatoeskr/sys/textprocessors/PlainTextProcessor.php @@ -0,0 +1,22 @@ +<?php + + +namespace r7r\cms\sys\textprocessors; + +use r7r\cms\sys\Esc; + +/** + * A textprocessor that simply escapes the input string. + */ +class PlainTextProcessor implements Textprocessor +{ + public function apply(string $input): string + { + return Esc::esc($input, Esc::HTML_WITH_BR); + } + + public function showInBackend(): bool + { + return true; + } +} diff --git a/ratatoeskr/sys/textprocessors/Textprocessor.php b/ratatoeskr/sys/textprocessors/Textprocessor.php new file mode 100644 index 0000000..90c1347 --- /dev/null +++ b/ratatoeskr/sys/textprocessors/Textprocessor.php @@ -0,0 +1,19 @@ +<?php + +namespace r7r\cms\sys\textprocessors; + +/** + * Interface Textprocessor. + * + * A textprocessor turns an input into HTML. + */ +interface Textprocessor +{ + public function apply(string $input): string; + + /** + * Should this textprocessor be available to the user in the backend? + * @return bool + */ + public function showInBackend(): bool; +} diff --git a/ratatoeskr/sys/textprocessors/TextprocessorRepository.php b/ratatoeskr/sys/textprocessors/TextprocessorRepository.php new file mode 100644 index 0000000..5d918c0 --- /dev/null +++ b/ratatoeskr/sys/textprocessors/TextprocessorRepository.php @@ -0,0 +1,76 @@ +<?php + + +namespace r7r\cms\sys\textprocessors; + +use Exception; + +class TextprocessorRepository +{ + /** @var Textprocessor[] */ + private $textprocessors = []; + + + /** + * Builds a repository with the default textprocessors prepopulated. + * @return self + */ + public static function buildDefault(): self + { + $repo = new self(); + + $repo->register("Markdown", new MarkdownProcessor()); + $repo->register("Plain Text", new PlainTextProcessor()); + $repo->register("HTML", new HtmlProcessor()); + + return $repo; + } + + public function register(string $name, Textprocessor $textprocessor): void + { + $this->textprocessors[$name] = $textprocessor; + } + + public function getTextprocessor(string $name): ?Textprocessor + { + return $this->textprocessors[$name] ?? null; + } + + /** + * @return Textprocessor[] + */ + public function all(): array + { + return $this->textprocessors; + } + + /** + * Apply a textprocessor to the input text. + * + * @param string $input The input text + * @param string $name The name of the textprocessor + * @return string|null Will return null, if the textprocessor was not found + */ + public function apply(string $input, string $name): ?string + { + $textprocessor = $this->getTextprocessor($name); + return $textprocessor === null ? null : $textprocessor->apply($input); + } + + /** + * Like {@see TextprocessorRepository::apply()}, but will throw an exception instead of returning null, if the textprocessor was not found. + * + * @param string $input The input text + * @param string $name The name of the textprocessor + * @return string + * @throws Exception + */ + public function mustApply(string $input, string $name): string + { + $out = $this->apply($input, $name); + if ($out === null) { + throw new Exception("Unknown Textprocessor: $name"); + } + return $out; + } +} |