Moved parser utility functions to their own file.
This commit is contained in:
parent
e30ff28af8
commit
65e70ce1f8
12 changed files with 73 additions and 78 deletions
|
@ -40,6 +40,7 @@ require_once __DIR__ . '/src/Forum/perms.php';
|
||||||
require_once __DIR__ . '/src/Forum/post.php';
|
require_once __DIR__ . '/src/Forum/post.php';
|
||||||
require_once __DIR__ . '/src/Forum/topic.php';
|
require_once __DIR__ . '/src/Forum/topic.php';
|
||||||
require_once __DIR__ . '/src/Forum/validate.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/login_attempt.php';
|
||||||
require_once __DIR__ . '/src/Users/profile.php';
|
require_once __DIR__ . '/src/Users/profile.php';
|
||||||
require_once __DIR__ . '/src/Users/relations.php';
|
require_once __DIR__ . '/src/Users/relations.php';
|
||||||
|
|
|
@ -126,7 +126,7 @@ if ($postRequest) {
|
||||||
$app->getUserId(),
|
$app->getUserId(),
|
||||||
IPAddress::remote()->getString(),
|
IPAddress::remote()->getString(),
|
||||||
$postText,
|
$postText,
|
||||||
MSZ_FORUM_POST_PARSER_BBCODE
|
MSZ_PARSER_BBCODE
|
||||||
);
|
);
|
||||||
forum_topic_mark_read($app->getUserId(), $topicId, $forum['forum_id']);
|
forum_topic_mark_read($app->getUserId(), $topicId, $forum['forum_id']);
|
||||||
|
|
||||||
|
|
|
@ -114,13 +114,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
if (!$perms['edit_about']) {
|
if (!$perms['edit_about']) {
|
||||||
$settingsErrors[] = "You're not allowed to edit your about page.";
|
$settingsErrors[] = "You're not allowed to edit your about page.";
|
||||||
} else {
|
} else {
|
||||||
$aboutParser = (int)($_POST['about']['parser'] ?? MSZ_FORUM_POST_PARSER_PLAIN);
|
$aboutParser = (int)($_POST['about']['parser'] ?? MSZ_PARSER_PLAIN);
|
||||||
$aboutText = $_POST['about']['text'] ?? '';
|
$aboutText = $_POST['about']['text'] ?? '';
|
||||||
|
|
||||||
// TODO: this is disgusting (move this into a user_set_about function or some shit)
|
// TODO: this is disgusting (move this into a user_set_about function or some shit)
|
||||||
while (true) {
|
while (true) {
|
||||||
// TODO: take parser shit out of forum_post
|
// TODO: take parser shit out of forum_post
|
||||||
if (!forum_post_is_valid_parser($aboutParser)) {
|
if (!parser_is_valid($aboutParser)) {
|
||||||
$settingsErrors[] = 'Invalid parser specified.';
|
$settingsErrors[] = 'Invalid parser specified.';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +1,13 @@
|
||||||
<?php
|
<?php
|
||||||
use Misuzu\Database;
|
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(
|
function forum_post_create(
|
||||||
int $topicId,
|
int $topicId,
|
||||||
int $forumId,
|
int $forumId,
|
||||||
int $userId,
|
int $userId,
|
||||||
string $ipAddress,
|
string $ipAddress,
|
||||||
string $text,
|
string $text,
|
||||||
int $parser = MSZ_FORUM_POST_PARSER_PLAIN
|
int $parser = MSZ_PARSER_PLAIN
|
||||||
): int {
|
): int {
|
||||||
$createPost = Database::prepare('
|
$createPost = Database::prepare('
|
||||||
INSERT INTO `msz_forum_posts`
|
INSERT INTO `msz_forum_posts`
|
||||||
|
|
53
src/Parsers/parse.php
Normal file
53
src/Parsers/parse.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -62,7 +62,7 @@
|
||||||
<div class="changelog__change__column changelog__change__column--change-info">
|
<div class="changelog__change__column changelog__change__column--change-info">
|
||||||
<div class="changelog__change__text">
|
<div class="changelog__change__text">
|
||||||
{% if change.change_text|length >= 1 %}
|
{% if change.change_text|length >= 1 %}
|
||||||
{{ change.change_text|parse_text('md')|raw }}
|
{{ change.change_text|parse_text(constant('MSZ_PARSER_MARKDOWN'))|raw }}
|
||||||
{% else %}
|
{% else %}
|
||||||
<p>This change has no additional notes.</p>
|
<p>This change has no additional notes.</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
|
@ -292,13 +292,7 @@
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="forum__post__content__text">
|
<div class="forum__post__content__text">
|
||||||
{% if post.post_parse == 2 %}
|
{{ post.post_text|escape|parse_text(post.post_parse)|raw }}
|
||||||
{{ 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 %}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
{{ document.title }}
|
{{ document.title }}
|
||||||
</div>
|
</div>
|
||||||
<div class="container__content">
|
<div class="container__content">
|
||||||
{{ document.content|parse_text('md')|raw }}
|
{{ document.content|parse_text(constant('MSZ_PARSER_MARKDOWN'))|raw }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
<div class="news__preview__container">
|
<div class="news__preview__container">
|
||||||
<div class="news__preview__content">
|
<div class="news__preview__content">
|
||||||
<div class="news__preview__text">
|
<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>
|
</div>
|
||||||
|
|
||||||
{% if post.user_id is not null %}
|
{% if post.user_id is not null %}
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
|
|
||||||
<div class="news__post__content">
|
<div class="news__post__content">
|
||||||
<div class="news__post__text">
|
<div class="news__post__text">
|
||||||
{{ post.post_text|parse_text('md')|raw }}
|
{{ post.post_text|parse_text(constant('MSZ_PARSER_MARKDOWN'))|raw }}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="news__sidebar news__post__details">
|
<div class="news__sidebar news__post__details">
|
||||||
|
|
|
@ -181,13 +181,7 @@
|
||||||
About {{ profile.username }}
|
About {{ profile.username }}
|
||||||
</div>
|
</div>
|
||||||
<div class="container__content profile__about__content">
|
<div class="container__content profile__about__content">
|
||||||
{% if profile.user_about_parser == 2 %}
|
{{ profile.user_about_content|escape|parse_text(profile.user_about_parser)|raw }}
|
||||||
{{ 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 %}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
51
utility.php
51
utility.php
|
@ -20,6 +20,15 @@ function array_test(array $array, callable $func): bool
|
||||||
return true;
|
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
|
function set_cookie_m(string $name, string $value, int $expires): void
|
||||||
{
|
{
|
||||||
setcookie(
|
setcookie(
|
||||||
|
@ -255,16 +264,6 @@ function pdo_prepare_array(array $keys, bool $useKeys = false, string $format =
|
||||||
return implode(', ', $parts);
|
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
|
function is_local_url(string $url): bool
|
||||||
{
|
{
|
||||||
$length = mb_strlen($url);
|
$length = mb_strlen($url);
|
||||||
|
@ -281,38 +280,6 @@ function is_local_url(string $url): bool
|
||||||
return starts_with($url, $prefix);
|
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
|
function render_error(int $code, string $template = 'errors.%d'): string
|
||||||
{
|
{
|
||||||
return render_info(null, $code, $template);
|
return render_info(null, $code, $template);
|
||||||
|
|
Loading…
Add table
Reference in a new issue