diff options
author | Thomas Lange <code@nerdmind.de> | 2021-07-19 17:50:21 +0200 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2021-07-19 17:58:54 +0200 |
commit | dd4b3d9ebb85c9bc8138212fd7cb207ab154f626 (patch) | |
tree | e005be07809b4644d6974eb59bcfdca7017f3234 /core/namespace/Parsers/EmoticonParser.php | |
parent | 489851d1e7b1d346ff316e7a6721de574322d7d6 (diff) | |
download | blog-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/EmoticonParser.php')
-rw-r--r-- | core/namespace/Parsers/EmoticonParser.php | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/core/namespace/Parsers/EmoticonParser.php b/core/namespace/Parsers/EmoticonParser.php new file mode 100644 index 0000000..4e035c8 --- /dev/null +++ b/core/namespace/Parsers/EmoticonParser.php @@ -0,0 +1,72 @@ +<?php +namespace Parsers; +use Application; + +class EmoticonParser implements ParserInterface { + private $Language; + + #=========================================================================== + # Initialize + #=========================================================================== + public function __construct() { + $this->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('<span title="%s">%s</span>', $explanation, $emoticon); + }, $text); + } +} |