Work in progress.

This commit is contained in:
flash 2018-07-15 04:15:12 +02:00
parent af60ea5693
commit d52cb3af63
8 changed files with 70 additions and 112 deletions

View file

@ -5,6 +5,7 @@
&__sidebar {
width: 300px;
margin-left: 2px;
flex: 0 0 auto;
}
&__main {

View file

@ -1,16 +1,24 @@
<?php
namespace Misuzu\Parsers\BBCode;
use Misuzu\Parsers\Parser;
use Misuzu\Parsers\ParserInterface;
final class BBCodeParser extends Parser
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class BBCodeParser implements ParserInterface
{
private static $instance;
public static function instance(): BBCodeParser
{
return is_null(static::$instance) ? (static::$instance = new BBCodeParser()) : static::$instance;
}
private $tags = [];
public function __construct(array $tags = [])
{
parent::__construct();
if (empty($tags)) {
$tags = [
// Advanced markup
@ -48,4 +56,9 @@ final class BBCodeParser extends Parser
return $text;
}
public function parseLine(string $line): string
{
return $this->parseText($line);
}
}

View file

@ -3,18 +3,15 @@ namespace Misuzu\Parsers;
use Parsedown;
final class MarkdownParser extends Parser
class MarkdownParser extends Parsedown implements ParserInterface
{
private $parsedown;
public function __construct()
{
parent::__construct();
$this->parsedown = new Parsedown;
}
public function parseText(string $text): string
{
return $this->parsedown->text($text);
return $this->text($text);
}
public function parseLine(string $line): string
{
return $this->line($line);
}
}

View file

@ -1,34 +0,0 @@
<?php
namespace Misuzu\Parsers;
abstract class Parser
{
private static $instances = [];
public function __construct()
{
self::$instances[static::class] = $this;
}
public static function getInstance(): ?Parser
{
if (!array_key_exists(static::class, self::$instances)) {
return null;
}
return self::$instances[static::class];
}
public static function getOrCreateInstance(...$args): Parser
{
$instance = static::getInstance();
if ($instance === null) {
$instance = new static(...$args);
}
return $instance;
}
abstract public function parseText(string $text): string;
}

View file

@ -0,0 +1,8 @@
<?php
namespace Misuzu\Parsers;
interface ParserInterface
{
public function parseText(string $text): string;
public function parseLine(string $line): string;
}

View file

@ -80,9 +80,9 @@ function zalgo_run(
return $text;
}
$going_up = has_flag($direction, MSZ_ZALGO_DIR_UP);
$going_mid = has_flag($direction, MSZ_ZALGO_DIR_MID);
$going_down = has_flag($direction, MSZ_ZALGO_DIR_DOWN);
$going_up = ($direction & MSZ_ZALGO_DIR_UP) > 0;
$going_mid = ($direction & MSZ_ZALGO_DIR_MID) > 0;
$going_down = ($direction & MSZ_ZALGO_DIR_DOWN) > 0;
$str = '';

View file

@ -1,28 +1,12 @@
<?php
// both of these are provided by illuminate/database already
// but i feel like it makes sense to add these definitions regardless
if (!function_exists('starts_with')) {
function starts_with(string $string, string $text): bool
{
return substr($string, 0, strlen($text)) === $text;
}
}
if (!function_exists('ends_with')) {
function ends_with(string $string, string $text): bool
{
return substr($string, 0 - strlen($text)) === $text;
}
}
function json_encode_m($obj): string
function starts_with(string $string, string $text): bool
{
if (!headers_sent()) {
header('Content-Type: application/json; charset=utf-8');
}
return substr($string, 0, strlen($text)) === $text;
}
return json_encode($obj);
function ends_with(string $string, string $text): bool
{
return substr($string, 0 - strlen($text)) === $text;
}
function set_cookie_m(string $name, string $value, int $expires): void
@ -54,20 +38,6 @@ function dechex_pad(int $value, int $padding = 2): string
return str_pad(dechex($value), $padding, '0', STR_PAD_LEFT);
}
function array_rand_value(array $array, $fallback = null)
{
if (!$array) {
return $fallback;
}
return $array[array_rand($array)];
}
function has_flag(int $flags, int $flag): bool
{
return ($flags & $flag) > 0;
}
function byte_symbol($bytes, $decimal = false)
{
if ($bytes < 1) {
@ -84,27 +54,29 @@ function byte_symbol($bytes, $decimal = false)
return sprintf("%.2f %s%sB", $bytes, $symbol, $symbol !== '' && !$decimal ? 'i' : '');
}
// this should be rewritten to only load the database once per Application instance.
// for now this will do since the only time this function is called is once during registration.
// also make sure an instance of Application with config exists before calling this!
function get_country_code(string $ipAddr, string $fallback = 'XX'): string
{
global $_msz_geoip;
try {
$app = \Misuzu\Application::getInstance();
$config = $app->getConfig();
if (!$_msz_geoip) {
$app = \Misuzu\Application::getInstance();
$config = $app->getConfig();
if ($config === null) {
return $fallback;
if ($config === null) {
return $fallback;
}
$database_path = $config->get('GeoIP', 'database_path');
if ($database_path === null) {
return $fallback;
}
$_msz_geoip = new \GeoIp2\Database\Reader($database_path);
}
$database_path = $config->get('GeoIP', 'database_path');
if ($database_path === null) {
return $fallback;
}
$geoip = new \GeoIp2\Database\Reader($database_path);
$record = $geoip->country($ipAddr);
$record = $_msz_geoip->country($ipAddr);
return $record->country->isoCode;
} catch (\Exception $e) {
@ -223,12 +195,12 @@ function pdo_prepare_array(array $keys, bool $useKeys = false, string $format =
function parse_markdown(string $text): string
{
return \Misuzu\Parsers\MarkdownParser::getOrCreateInstance()->parseText($text);
return \Misuzu\Parsers\MarkdownParser::instance()->parseText($text);
}
function parse_bbcode(string $text): string
{
return \Misuzu\Parsers\BBCode\BBCodeParser::getOrCreateInstance()->parseText($text);
return \Misuzu\Parsers\BBCode\BBCodeParser::instance()->parseText($text);
}
function render_error(int $code, string $template = 'errors.%d'): string

View file

@ -6,8 +6,7 @@
<div class="container__content news__preview__content">
<div class="news__preview__text">
{{ post.post_text|first_paragraph|raw }}
<p><b><i><a href="/news.php?p={{ post.post_id }}">View full post</a></i></b></p>
{{ post.post_text|first_paragraph|md|raw }}
</div>
<div class="news__preview__info">
@ -15,12 +14,14 @@
{{ post.created_at|time_diff }}
</time>
<a class="news__preview__user" href="/profile.php?u={{ post.user_id }}">
<div class="news__preview__user__name" style="{{ post.user_colour|html_colour }}">
{{ post.username }}
</div>
<div class="avatar news__preview__user__avatar" style="background-image:url('/profile.php?u={{ post.user_id }}&amp;m=avatar')"></div>
</a>
{% if post.user_id is not null %}
<a class="news__preview__user" href="/profile.php?u={{ post.user_id }}">
<div class="news__preview__user__name" style="{{ post.user_colour|html_colour }}">
{{ post.username }}
</div>
<div class="avatar news__preview__user__avatar" style="background-image:url('/profile.php?u={{ post.user_id }}&amp;m=avatar')"></div>
</a>
{% endif %}
</div>
</div>
</div>