Moved parser utility functions to their own file.

This commit is contained in:
flash 2018-09-21 10:56:52 +02:00
parent e30ff28af8
commit 65e70ce1f8
12 changed files with 73 additions and 78 deletions

View file

@ -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';

View file

@ -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']);

View file

@ -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;
}

View file

@ -1,27 +1,13 @@
<?php
use Misuzu\Database;
define('MSZ_FORUM_POST_PARSER_PLAIN', 0);
define('MSZ_FORUM_POST_PARSER_BBCODE', 1);
define('MSZ_FORUM_POST_PARSER_MARKDOWN', 2);
define('MSZ_FORUM_POST_PARSERS', [
MSZ_FORUM_POST_PARSER_PLAIN,
MSZ_FORUM_POST_PARSER_BBCODE,
MSZ_FORUM_POST_PARSER_MARKDOWN,
]);
function forum_post_is_valid_parser(int $parser): bool
{
return in_array($parser, MSZ_FORUM_POST_PARSERS);
}
function forum_post_create(
int $topicId,
int $forumId,
int $userId,
string $ipAddress,
string $text,
int $parser = MSZ_FORUM_POST_PARSER_PLAIN
int $parser = MSZ_PARSER_PLAIN
): int {
$createPost = Database::prepare('
INSERT INTO `msz_forum_posts`

53
src/Parsers/parse.php Normal file
View file

@ -0,0 +1,53 @@
<?php
use Misuzu\Parsers\MarkdownParser;
use Misuzu\Parsers\BBCode\BBCodeParser;
define('MSZ_PARSER_PLAIN', 0);
define('MSZ_PARSER_BBCODE', 1);
define('MSZ_PARSER_MARKDOWN', 2);
define('MSZ_PARSERS', [
MSZ_PARSER_PLAIN,
MSZ_PARSER_BBCODE,
MSZ_PARSER_MARKDOWN,
]);
function parser_is_valid(int $parser): bool
{
return in_array($parser, MSZ_PARSERS, true);
}
function parse_text(string $text, int $parser): string
{
if (!parser_is_valid($parser)) {
return '';
}
switch ($parser) {
case MSZ_PARSER_MARKDOWN:
return MarkdownParser::instance()->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;
}
}

View file

@ -62,7 +62,7 @@
<div class="changelog__change__column changelog__change__column--change-info">
<div class="changelog__change__text">
{% if change.change_text|length >= 1 %}
{{ change.change_text|parse_text('md')|raw }}
{{ change.change_text|parse_text(constant('MSZ_PARSER_MARKDOWN'))|raw }}
{% else %}
<p>This change has no additional notes.</p>
{% endif %}

View file

@ -292,13 +292,7 @@
</a>
</div>
<div class="forum__post__content__text">
{% 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 }}
</div>
</div>
</div>

View file

@ -8,7 +8,7 @@
{{ document.title }}
</div>
<div class="container__content">
{{ document.content|parse_text('md')|raw }}
{{ document.content|parse_text(constant('MSZ_PARSER_MARKDOWN'))|raw }}
</div>
</div>
{% endblock %}

View file

@ -7,7 +7,7 @@
<div class="news__preview__container">
<div class="news__preview__content">
<div class="news__preview__text">
{{ post.post_text|first_paragraph|parse_text('md')|raw }}
{{ post.post_text|first_paragraph|parse_text(constant('MSZ_PARSER_MARKDOWN'))|raw }}
</div>
{% if post.user_id is not null %}

View file

@ -13,7 +13,7 @@
<div class="news__post__content">
<div class="news__post__text">
{{ post.post_text|parse_text('md')|raw }}
{{ post.post_text|parse_text(constant('MSZ_PARSER_MARKDOWN'))|raw }}
</div>
<div class="news__sidebar news__post__details">

View file

@ -181,13 +181,7 @@
About {{ profile.username }}
</div>
<div class="container__content profile__about__content">
{% 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 }}
</div>
</div>
{% endif %}

View file

@ -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);