diff options
author | Jonas Kohl | 2024-10-12 19:54:01 +0200 |
---|---|---|
committer | Jonas Kohl | 2024-10-12 19:54:01 +0200 |
commit | 0c45ffac2d8648fec4fa5e935754f4d34952983b (patch) | |
tree | ea5c300f6fa43f4dc4357807277f2fc33811f896 /src/index.php | |
parent | 8121e98210ada254bb146097f04c7e0345a2e206 (diff) |
Add rich text supportv0.5.0
Diffstat (limited to 'src/index.php')
-rw-r--r-- | src/index.php | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/index.php b/src/index.php index 6e1522c..4fe060c 100644 --- a/src/index.php +++ b/src/index.php @@ -12,7 +12,7 @@ use mystic\forum\utils\RequestUtils; header_remove("X-Powered-By"); -const MYSTICBB_VERSION = "0.4.6"; +const MYSTICBB_VERSION = "0.5.0"; if (($_SERVER["HTTP_USER_AGENT"] ?? "") === "") { http_response_code(403); @@ -163,6 +163,29 @@ function reArrayFiles(&$file_post) { return $file_ary; } +const TAGS_REGEX = '/\[(b|i|u|s|\\^|_|spoiler)\](.+?)\[\/\1\]/s'; + +function expandTags(string $contents): string { + return preg_replace_callback(TAGS_REGEX, function(array $m) { + $randomId = "target-" . bin2hex(random_bytes(8)); + $inner = $m[2]; + $tag = [ + "b" => ["<strong>", "</strong>" ], + "i" => ["<em>", "</em>" ], + "s" => ["<del>", "</del>" ], + "^" => ["<sup>", "</sup>" ], + "_" => ["<sub>", "</sub>" ], + "spoiler" => [ "<div class=\"panel panel-default\"><div class=\"panel-heading\"><div class=\"panel-title\"><a role=\"button\" data-toggle=\"collapse\" href=\"#$randomId\"><i class=\"fa fa-eye\"></i> Spoiler</a></div></div><div id=\"$randomId\" class=\"panel-collapse collapse\"><div class=\"panel-body\">", "</div></div></div>" ], + ][$m[1]] ?? $m[1]; + if (preg_match(TAGS_REGEX, $inner)) + $inner = expandTags($inner); + if (!is_array($tag)) $tag = [ "<$tag>", "</$tag>" ]; + $tagsBefore = $tag[0]; + $tagsAfter = $tag[1]; + return "{$tagsBefore}{$inner}{$tagsAfter}"; + }, $contents); +} + function renderPost(string $contents): string { $contents = preg_replace('~\R~u', "\n", $contents); $contents = trim($contents); @@ -197,6 +220,7 @@ function renderPost(string $contents): string { } else { $contents .= implode("\n", $lineBuf); } + $contents = expandTags($contents); return $contents; } |