aboutsummaryrefslogtreecommitdiffstats
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
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
-rw-r--r--core/functions.php108
-rw-r--r--core/namespace/Parsers/ArgumentParser.php30
-rw-r--r--core/namespace/Parsers/EmoticonParser.php72
-rw-r--r--core/namespace/Parsers/MarkdownParser.php37
-rw-r--r--core/namespace/Parsers/ParserInterface.php7
5 files changed, 171 insertions, 83 deletions
diff --git a/core/functions.php b/core/functions.php
index ff52e0c..547f84a 100644
--- a/core/functions.php
+++ b/core/functions.php
@@ -8,6 +8,10 @@ use ORM\Entities\User;
use Template\Template as Template;
use Template\Factory as TemplateFactory;
+use Parsers\ArgumentParser;
+use Parsers\EmoticonParser;
+use Parsers\MarkdownParser;
+
#===============================================================================
# Create generic pagination template
#===============================================================================
@@ -89,28 +93,34 @@ function generateUserItemTemplate(User $User): Template {
# Helper function to reduce duplicate code
#===============================================================================
function generateItemTemplateData(EntityInterface $Entity): array {
- $ATTR = $Entity->getAll(['password']);
- $ATTR = array_change_key_case($ATTR, CASE_UPPER);
+ $ArgumentParser = new ArgumentParser;
+ $MarkdownParser = new MarkdownParser;
+
+ $attribute = $Entity->getAll(['password']);
+ $attribute = array_change_key_case($attribute, CASE_UPPER);
+
+ $text = parseContentTags($Entity->get('body'));
+ $arguments = $ArgumentParser->parse($Entity->get('argv') ?? '');
- $preparsed = parseContentTags($Entity->get('body'));
+ $images = $MarkdownParser->parse($text)['img']['src'] ?? [];
+ $images = array_map('htmlentities', $images);
return [
'URL' => Application::getEntityURL($Entity),
'GUID' => generatePseudoGUID($Entity),
- 'ARGV' => parseArguments($Entity->get('argv')),
-
- 'ATTR' => $ATTR,
+ 'ARGV' => $arguments,
+ 'ATTR' => $attribute,
'PREV' => FALSE,
'NEXT' => FALSE,
'FILE' => [
- 'LIST' => getMarkdownImageURLs($preparsed),
+ 'LIST' => $images,
],
'BODY' => [
- 'TEXT' => function() use($preparsed) {
- return $preparsed;
+ 'TEXT' => function() use($text) {
+ return $text;
},
'HTML' => function() use($Entity) {
return parseEntityContent($Entity);
@@ -188,50 +198,15 @@ function parseContentTags(string $text): string {
# Parse entity content
#===============================================================================
function parseEntityContent(EntityInterface $Entity): string {
- $Parsedown = new Parsedown();
- $Parsedown->setUrlsLinked(FALSE);
-
$text = parseContentTags($Entity->get('body'));
if(Application::get('WRAP_EMOTICONS')) {
- $text = parseUnicodeEmoticons($text);
- }
-
- return $Parsedown->text($text);
-}
-
-#===============================================================================
-# Extract Markdown formatted image URLs
-#===============================================================================
-function getMarkdownImageURLs(string $text): array {
- $pattern = '#\!\[(.*)\][ ]?(?:\n[ ]*)?\((.*)(\s[\'"](.*)[\'"])?\)#U';
- $content = parseContentTags($text);
-
- if(preg_match_all($pattern, $content, $matches)) {
- return array_map('htmlentities', $matches[2]);
- }
-
- return [];
-}
-
-#===============================================================================
-# Parse argument string to array
-#===============================================================================
-function parseArguments(?string $argv): array {
- if($argv) {
- foreach(explode('|', $argv) as $delimeter) {
- $part = explode('=', $delimeter);
-
- $argumentK = $part[0] ?? NULL;
- $argumentV = $part[1] ?? TRUE;
-
- if(preg_match('#^[[:word:]]+$#', $argumentK)) {
- $arguments[strtoupper($argumentK)] = $argumentV;
- }
- }
+ $EmoticonParser = new EmoticonParser;
+ $text = $EmoticonParser->transform($text);
}
- return $arguments ?? [];
+ $MarkdownParser = new MarkdownParser;
+ return $MarkdownParser->transform($text);
}
#===============================================================================
@@ -261,43 +236,10 @@ function parseDatetime($datetime, $format): string {
}
#===============================================================================
-# Get unicode emoticons with their corresponding explanation
+# Get emoticons with their explanations
#===============================================================================
function getUnicodeEmoticons(): array {
- $Language = Application::getLanguage();
-
- return [
- html_entity_decode('&#x1F60A;') => $Language->text('emoticon_1F60A'),
- html_entity_decode('&#x1F61E;') => $Language->text('emoticon_1F61E'),
- html_entity_decode('&#x1F603;') => $Language->text('emoticon_1F603'),
- html_entity_decode('&#x1F61B;') => $Language->text('emoticon_1F61B'),
- html_entity_decode('&#x1F632;') => $Language->text('emoticon_1F632'),
- html_entity_decode('&#x1F609;') => $Language->text('emoticon_1F609'),
- html_entity_decode('&#x1F622;') => $Language->text('emoticon_1F622'),
- html_entity_decode('&#x1F610;') => $Language->text('emoticon_1F610'),
- html_entity_decode('&#x1F635;') => $Language->text('emoticon_1F635'),
- html_entity_decode('&#x1F612;') => $Language->text('emoticon_1F612'),
- html_entity_decode('&#x1F60E;') => $Language->text('emoticon_1F60E'),
- html_entity_decode('&#x1F61F;') => $Language->text('emoticon_1F61F'),
- html_entity_decode('&#x1F602;') => $Language->text('emoticon_1F602'),
- html_entity_decode('&#x1F604;') => $Language->text('emoticon_1F604'),
- ];
-}
-
-#===============================================================================
-# Wrap emoticons in <span> element with "title" attribute for explanation
-#===============================================================================
-function parseUnicodeEmoticons($string): string {
- $emoticon_data = getUnicodeEmoticons();
- $emoticon_list = array_keys($emoticon_data);
- $emoticon_list = implode('|', $emoticon_list);
-
- 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);
- }, $string);
+ return (new EmoticonParser)->getEmoticons();
}
#===============================================================================
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 @@
+<?php
+namespace Parsers;
+
+class ArgumentParser implements ParserInterface {
+
+ #===========================================================================
+ # Parse arguments (*without* duplicates)
+ #===========================================================================
+ public function parse(string $text): array {
+ foreach(explode('|', $text) as $delimiter) {
+ $part = explode('=', $delimiter);
+
+ $argumentK = $part[0] ?? NULL;
+ $argumentV = $part[1] ?? TRUE;
+
+ if(preg_match('#^[[:word:]]+$#', $argumentK)) {
+ $arguments[strtoupper($argumentK)] = $argumentV;
+ }
+ }
+
+ return $arguments ?? [];
+ }
+
+ #===========================================================================
+ # Transform arguments (not implemented)
+ #===========================================================================
+ public function transform(string $text): string {
+ return '';
+ }
+}
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('&#x1F60A;') => $Language->text('emoticon_1F60A'),
+ html_entity_decode('&#x1F61E;') => $Language->text('emoticon_1F61E'),
+ html_entity_decode('&#x1F603;') => $Language->text('emoticon_1F603'),
+ html_entity_decode('&#x1F61B;') => $Language->text('emoticon_1F61B'),
+ html_entity_decode('&#x1F632;') => $Language->text('emoticon_1F632'),
+ html_entity_decode('&#x1F609;') => $Language->text('emoticon_1F609'),
+ html_entity_decode('&#x1F622;') => $Language->text('emoticon_1F622'),
+ html_entity_decode('&#x1F610;') => $Language->text('emoticon_1F610'),
+ html_entity_decode('&#x1F635;') => $Language->text('emoticon_1F635'),
+ html_entity_decode('&#x1F612;') => $Language->text('emoticon_1F612'),
+ html_entity_decode('&#x1F60E;') => $Language->text('emoticon_1F60E'),
+ html_entity_decode('&#x1F61F;') => $Language->text('emoticon_1F61F'),
+ html_entity_decode('&#x1F602;') => $Language->text('emoticon_1F602'),
+ html_entity_decode('&#x1F604;') => $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);
+ }
+}
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);
+ }
+}
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 @@
+<?php
+namespace Parsers;
+
+interface ParserInterface {
+ public function parse(string $text): array;
+ public function transform(string $text): string;
+}