aboutsummaryrefslogtreecommitdiffstats
path: root/core/namespace/Parsers/MarkdownParser.php
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2021-07-19 17:50:21 +0200
committerThomas Lange <code@nerdmind.de>2021-07-19 17:58:54 +0200
commitdd4b3d9ebb85c9bc8138212fd7cb207ab154f626 (patch)
treee005be07809b4644d6974eb59bcfdca7017f3234 /core/namespace/Parsers/MarkdownParser.php
parent489851d1e7b1d346ff316e7a6721de574322d7d6 (diff)
downloadblog-dd4b3d9ebb85c9bc8138212fd7cb207ab154f626.tar.gz
blog-dd4b3d9ebb85c9bc8138212fd7cb207ab154f626.tar.xz
blog-dd4b3d9ebb85c9bc8138212fd7cb207ab154f626.zip
Add and use new parser/transformer classes
Classes: * Parsers\ArgumentParser * Parsers\EmoticonParser * Parsers\MarkdownParser Interfaces: * Parsers\ParserInterface
Diffstat (limited to 'core/namespace/Parsers/MarkdownParser.php')
-rw-r--r--core/namespace/Parsers/MarkdownParser.php37
1 files changed, 37 insertions, 0 deletions
diff --git a/core/namespace/Parsers/MarkdownParser.php b/core/namespace/Parsers/MarkdownParser.php
new file mode 100644
index 0000000..27a18ad
--- /dev/null
+++ b/core/namespace/Parsers/MarkdownParser.php
@@ -0,0 +1,37 @@
+<?php
+namespace Parsers;
+use Parsedown;
+
+class MarkdownParser implements ParserInterface {
+ private $Parsedown;
+
+ #===========================================================================
+ # Initialize
+ #===========================================================================
+ public function __construct() {
+ $this->Parsedown = new Parsedown();
+ $this->Parsedown->setUrlsLinked(FALSE);
+ }
+
+ #===========================================================================
+ # Parse Markdown (currently only images)
+ #===========================================================================
+ public function parse(string $text): array {
+ $image = '#\!\[(.*)\]\((.*)(?:\s[\'"](.*)[\'"])?\)#U';
+
+ if(preg_match_all($image, $text, $matches)) {
+ $data['img']['src'] = $matches[2];
+ $data['img']['alt'] = $matches[1];
+ $data['img']['title'] = $matches[3];
+ }
+
+ return $data ?? [];
+ }
+
+ #===========================================================================
+ # Transform Markdown to HTML
+ #===========================================================================
+ public function transform(string $text): string {
+ return $this->Parsedown->text($text);
+ }
+}