aboutsummaryrefslogtreecommitdiff
path: root/ratatoeskr/sys/textprocessors
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2020-09-25 21:18:18 +0200
committerLaria Carolin Chabowski <laria@laria.me>2020-09-25 21:28:55 +0200
commit378881378aab5454c84cb1fecbbcc675f64dc27f (patch)
treef8bbe239c2a87cc61bec05b987099e4401dd2693 /ratatoeskr/sys/textprocessors
parent78c0350b3b7fc025ba565e19bfa195b68e95bb88 (diff)
downloadratatoeskr-cms-378881378aab5454c84cb1fecbbcc675f64dc27f.tar.gz
ratatoeskr-cms-378881378aab5454c84cb1fecbbcc675f64dc27f.tar.bz2
ratatoeskr-cms-378881378aab5454c84cb1fecbbcc675f64dc27f.zip
Refactor textprocessors
They are now managed by TextprocessorRepository and are instances of Textprocessor. This replaces the global $textprocessors variable.
Diffstat (limited to 'ratatoeskr/sys/textprocessors')
-rw-r--r--ratatoeskr/sys/textprocessors/HtmlProcessor.php20
-rw-r--r--ratatoeskr/sys/textprocessors/LegacyTextprocessor.php32
-rw-r--r--ratatoeskr/sys/textprocessors/MarkdownProcessor.php22
-rw-r--r--ratatoeskr/sys/textprocessors/PlainTextProcessor.php22
-rw-r--r--ratatoeskr/sys/textprocessors/Textprocessor.php19
-rw-r--r--ratatoeskr/sys/textprocessors/TextprocessorRepository.php76
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;
+ }
+}