From dd4b3d9ebb85c9bc8138212fd7cb207ab154f626 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Mon, 19 Jul 2021 17:50:21 +0200 Subject: Add and use new parser/transformer classes Classes: * Parsers\ArgumentParser * Parsers\EmoticonParser * Parsers\MarkdownParser Interfaces: * Parsers\ParserInterface --- core/namespace/Parsers/ArgumentParser.php | 30 +++++++++++++ core/namespace/Parsers/EmoticonParser.php | 72 ++++++++++++++++++++++++++++++ core/namespace/Parsers/MarkdownParser.php | 37 +++++++++++++++ core/namespace/Parsers/ParserInterface.php | 7 +++ 4 files changed, 146 insertions(+) create mode 100644 core/namespace/Parsers/ArgumentParser.php create mode 100644 core/namespace/Parsers/EmoticonParser.php create mode 100644 core/namespace/Parsers/MarkdownParser.php create mode 100644 core/namespace/Parsers/ParserInterface.php (limited to 'core/namespace') diff --git a/core/namespace/Parsers/ArgumentParser.php b/core/namespace/Parsers/ArgumentParser.php new file mode 100644 index 0000000..ab32fe1 --- /dev/null +++ b/core/namespace/Parsers/ArgumentParser.php @@ -0,0 +1,30 @@ +Language = Application::getLanguage(); + } + + #=========================================================================== + # Get emoticons with their explanations + #=========================================================================== + public function getEmoticons(): array { + $Language = $this->Language; + + return [ + html_entity_decode('😊') => $Language->text('emoticon_1F60A'), + html_entity_decode('😞') => $Language->text('emoticon_1F61E'), + html_entity_decode('😃') => $Language->text('emoticon_1F603'), + html_entity_decode('😛') => $Language->text('emoticon_1F61B'), + html_entity_decode('😲') => $Language->text('emoticon_1F632'), + html_entity_decode('😉') => $Language->text('emoticon_1F609'), + html_entity_decode('😢') => $Language->text('emoticon_1F622'), + html_entity_decode('😐') => $Language->text('emoticon_1F610'), + html_entity_decode('😵') => $Language->text('emoticon_1F635'), + html_entity_decode('😒') => $Language->text('emoticon_1F612'), + html_entity_decode('😎') => $Language->text('emoticon_1F60E'), + html_entity_decode('😟') => $Language->text('emoticon_1F61F'), + html_entity_decode('😂') => $Language->text('emoticon_1F602'), + html_entity_decode('😄') => $Language->text('emoticon_1F604'), + ]; + } + + #=========================================================================== + # Parse occurring emoticons (*without* duplicates) + #=========================================================================== + public function parse(string $text): array { + $emoticon_data = $this->getEmoticons(); + $emoticon_list = array_keys($emoticon_data); + $emoticon_list = implode('|', $emoticon_list); + + preg_match_all("#($emoticon_list)#", $text, $matches); + + foreach($matches[1] as $emoticon) { + $emoticons[$emoticon] = $emoticon_data[$emoticon]; + } + + return $emoticons ?? []; + } + + #=========================================================================== + # Wrap emoticons inside a titled span element + #=========================================================================== + public function transform(string $text): string { + $emoticon_data = $this->getEmoticons(); + $emoticon_list = array_keys($emoticon_data); + $emoticon_list = implode('|', $emoticon_list); + + # TODO: Do not wrap emoticons if they occur inside a code block + return preg_replace_callback("#($emoticon_list)#", function($matches) + use($emoticon_data) { + $emoticon = $matches[1]; + $explanation = $emoticon_data[$emoticon]; + return sprintf('%s', $explanation, $emoticon); + }, $text); + } +} 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 @@ +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); + } +} diff --git a/core/namespace/Parsers/ParserInterface.php b/core/namespace/Parsers/ParserInterface.php new file mode 100644 index 0000000..8dd94e2 --- /dev/null +++ b/core/namespace/Parsers/ParserInterface.php @@ -0,0 +1,7 @@ +