diff --git a/misuzu.php b/misuzu.php index dae0df08..c8a6ed7e 100644 --- a/misuzu.php +++ b/misuzu.php @@ -40,6 +40,7 @@ require_once __DIR__ . '/src/Forum/perms.php'; require_once __DIR__ . '/src/Forum/post.php'; require_once __DIR__ . '/src/Forum/topic.php'; require_once __DIR__ . '/src/Forum/validate.php'; +require_once __DIR__ . '/src/Parsers/parse.php'; require_once __DIR__ . '/src/Users/login_attempt.php'; require_once __DIR__ . '/src/Users/profile.php'; require_once __DIR__ . '/src/Users/relations.php'; diff --git a/public/forum/posting.php b/public/forum/posting.php index 0899b9b7..dcc1e6d3 100644 --- a/public/forum/posting.php +++ b/public/forum/posting.php @@ -126,7 +126,7 @@ if ($postRequest) { $app->getUserId(), IPAddress::remote()->getString(), $postText, - MSZ_FORUM_POST_PARSER_BBCODE + MSZ_PARSER_BBCODE ); forum_topic_mark_read($app->getUserId(), $topicId, $forum['forum_id']); diff --git a/public/settings.php b/public/settings.php index ed195a73..261721a3 100644 --- a/public/settings.php +++ b/public/settings.php @@ -114,13 +114,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!$perms['edit_about']) { $settingsErrors[] = "You're not allowed to edit your about page."; } else { - $aboutParser = (int)($_POST['about']['parser'] ?? MSZ_FORUM_POST_PARSER_PLAIN); + $aboutParser = (int)($_POST['about']['parser'] ?? MSZ_PARSER_PLAIN); $aboutText = $_POST['about']['text'] ?? ''; // TODO: this is disgusting (move this into a user_set_about function or some shit) while (true) { // TODO: take parser shit out of forum_post - if (!forum_post_is_valid_parser($aboutParser)) { + if (!parser_is_valid($aboutParser)) { $settingsErrors[] = 'Invalid parser specified.'; break; } diff --git a/src/Forum/post.php b/src/Forum/post.php index b20459ea..4cde54a0 100644 --- a/src/Forum/post.php +++ b/src/Forum/post.php @@ -1,27 +1,13 @@ parseText($text); + + case MSZ_PARSER_BBCODE: + return BBCodeParser::instance()->parseText($text); + + case MSZ_PARSER_PLAIN: + return $text; + } +} + +function parse_line(string $line, int $parser): string +{ + if (!parser_is_valid($parser)) { + return ''; + } + + switch ($parser) { + case MSZ_PARSER_MARKDOWN: + return MarkdownParser::instance()->parseLine($line); + + case MSZ_PARSER_BBCODE: + return BBCodeParser::instance()->parseLine($line); + + case MSZ_PARSER_PLAIN: + return $line; + } +} diff --git a/templates/changelog/change.twig b/templates/changelog/change.twig index 602f4717..37e3406b 100644 --- a/templates/changelog/change.twig +++ b/templates/changelog/change.twig @@ -62,7 +62,7 @@
{% if change.change_text|length >= 1 %} - {{ change.change_text|parse_text('md')|raw }} + {{ change.change_text|parse_text(constant('MSZ_PARSER_MARKDOWN'))|raw }} {% else %}

This change has no additional notes.

{% endif %} diff --git a/templates/forum/macros.twig b/templates/forum/macros.twig index 069419cf..ac3a14e6 100644 --- a/templates/forum/macros.twig +++ b/templates/forum/macros.twig @@ -292,13 +292,7 @@
- {% if post.post_parse == 2 %} - {{ post.post_text|escape|parse_text('md')|raw }} - {% elseif post.post_parse == 1 %} - {{ post.post_text|escape|parse_text('bb')|raw }} - {% else %} - {{ post.post_text|escape }} - {% endif %} + {{ post.post_text|escape|parse_text(post.post_parse)|raw }}
diff --git a/templates/info/view.twig b/templates/info/view.twig index 954bd6bd..5ea38915 100644 --- a/templates/info/view.twig +++ b/templates/info/view.twig @@ -8,7 +8,7 @@ {{ document.title }}
- {{ document.content|parse_text('md')|raw }} + {{ document.content|parse_text(constant('MSZ_PARSER_MARKDOWN'))|raw }}
{% endblock %} diff --git a/templates/news/macros.twig b/templates/news/macros.twig index c10421ba..d74d9f89 100644 --- a/templates/news/macros.twig +++ b/templates/news/macros.twig @@ -7,7 +7,7 @@
- {{ post.post_text|first_paragraph|parse_text('md')|raw }} + {{ post.post_text|first_paragraph|parse_text(constant('MSZ_PARSER_MARKDOWN'))|raw }}
{% if post.user_id is not null %} diff --git a/templates/news/post.twig b/templates/news/post.twig index 359d5892..992447d2 100644 --- a/templates/news/post.twig +++ b/templates/news/post.twig @@ -13,7 +13,7 @@
- {{ post.post_text|parse_text('md')|raw }} + {{ post.post_text|parse_text(constant('MSZ_PARSER_MARKDOWN'))|raw }}
diff --git a/templates/user/profile.twig b/templates/user/profile.twig index e5c81da9..df527112 100644 --- a/templates/user/profile.twig +++ b/templates/user/profile.twig @@ -181,13 +181,7 @@ About {{ profile.username }}
- {% if profile.user_about_parser == 2 %} - {{ profile.user_about_content|escape|parse_text('md')|raw }} - {% elseif profile.user_about_parser == 1 %} - {{ profile.user_about_content|escape|parse_text('bb')|raw }} - {% else %} - {{ profile.user_about_content|escape }} - {% endif %} + {{ profile.user_about_content|escape|parse_text(profile.user_about_parser)|raw }}
{% endif %} diff --git a/utility.php b/utility.php index ef81fba8..d0c068bc 100644 --- a/utility.php +++ b/utility.php @@ -20,6 +20,15 @@ function array_test(array $array, callable $func): bool return true; } +function array_apply(array $array, callable $func): array +{ + for ($i = 0; $i < count($array); $i++) { + $array[$i] = $func($array[$i]); + } + + return $array; +} + function set_cookie_m(string $name, string $value, int $expires): void { setcookie( @@ -255,16 +264,6 @@ function pdo_prepare_array(array $keys, bool $useKeys = false, string $format = return implode(', ', $parts); } -function parse_markdown(string $text): string -{ - return \Misuzu\Parsers\MarkdownParser::instance()->parseText($text); -} - -function parse_bbcode(string $text): string -{ - return \Misuzu\Parsers\BBCode\BBCodeParser::instance()->parseText($text); -} - function is_local_url(string $url): bool { $length = mb_strlen($url); @@ -281,38 +280,6 @@ function is_local_url(string $url): bool return starts_with($url, $prefix); } -function parse_text(string $text, string $parser): string -{ - switch (mb_strtolower($parser)) { - case 'md': - case 'markdown': - return \Misuzu\Parsers\MarkdownParser::instance()->parseText($text); - - case 'bb': - case 'bbcode': - return \Misuzu\Parsers\BBCode\BBCodeParser::instance()->parseText($text); - - default: - return $text; - } -} - -function parse_line(string $line, string $parser): string -{ - switch (mb_strtolower($parser)) { - case 'md': - case 'markdown': - return \Misuzu\Parsers\MarkdownParser::instance()->parseLine($line); - - case 'bb': - case 'bbcode': - return \Misuzu\Parsers\BBCode\BBCodeParser::instance()->parseLine($line); - - default: - return $line; - } -} - function render_error(int $code, string $template = 'errors.%d'): string { return render_info(null, $code, $template);