Browse Source

update post formatter

main
ghost 10 months ago
parent
commit
ea8ef9c4da
  1. 174
      src/controller/room.php
  2. 3
      src/view/post.gemini

174
src/controller/room.php

@ -90,7 +90,7 @@ class Room
[ [
$namespace['name'], $namespace['name'],
$namespace['total'], $namespace['total'],
$this->_url( $this->_link(
sprintf( sprintf(
'/room/%s', '/room/%s',
$namespace['namespace'] $namespace['namespace']
@ -178,8 +178,8 @@ class Room
file_get_contents( file_get_contents(
__DIR__ . '/../../logo.ascii' __DIR__ . '/../../logo.ascii'
), ),
$this->_url(), $this->_link(),
$this->_url( // @TODO $this->_link( // @TODO
sprintf( sprintf(
'/room/%s/post', '/room/%s/post',
$namespace $namespace
@ -262,27 +262,9 @@ class Room
{ {
if ($post['txid'] == $quote) if ($post['txid'] == $quote)
{ {
// Strip folding $quote = $this->_quote(
$quote = '>' . $post['value'],
trim( true
preg_replace(
'/^@([A-z0-9]{64}[\n\r]?)/',
null,
// Add quote after each new line
str_replace(
PHP_EOL,
PHP_EOL . '>',
preg_replace(
'/[\n\r]+/',
PHP_EOL,
// Ignore markup
$this->_escape(
$post['value']
)
)
)
),
'>'
); );
break; break;
@ -293,12 +275,8 @@ class Room
$record['value'] = preg_replace( $record['value'] = preg_replace(
'/^@([A-z0-9]{64})/', '/^@([A-z0-9]{64})/',
null, null,
preg_replace( $this->_escape(
'/[\n\r]+/', $record['value']
PHP_EOL,
$this->_escape(
$record['value']
)
) )
); );
} }
@ -308,39 +286,64 @@ class Room
$time = $matches[1]; $time = $matches[1];
// Build final view and send to response // Build final view and send to response
return str_replace( return
// Allow up to 2 line separators
preg_replace(
[ [
'{txid}', '/[\n\r]{1}/',
'{time}', '/[\n\r]{3,}/'
'{author}',
'{quote}',
'{message}',
'{reply}'
], ],
[ [
$record['txid'], PHP_EOL,
$this->_ago( PHP_EOL . PHP_EOL
$matches[1] ],
),
'@' . $matches[2], // Apply macros
$quote, str_replace(
preg_replace( [
'/[\n\r]+/', '{time}',
PHP_EOL, '{author}',
'{quote}',
'{message}',
'{links}'
],
[
// time
$this->_ago(
$matches[1]
),
// author
'@' . $matches[2],
// quote
$quote,
// message
$this->_escape( $this->_escape(
$record['value'] $record['value']
),
// links
implode(
PHP_EOL,
[
$this->_link(
sprintf(
'/room/%s/reply/%s',
$namespace,
$record['txid'],
),
_('Reply'),
true
)
]
) )
), ],
$this->_url( // @TODO file_get_contents(
sprintf( __DIR__ . '/../view/post.gemini'
'/room/%s/reply/%s',
$namespace,
$record['txid'],
)
) )
],
file_get_contents(
__DIR__ . '/../view/post.gemini'
) )
); );
} }
@ -469,15 +472,66 @@ class Room
} }
// Merge lines // Merge lines
return implode( return trim(
PHP_EOL, implode(
$lines PHP_EOL,
$lines
)
);
}
private function _quote(string $value, ?bool $tag = false)
{
// Escape special chars
$value = $this->_escape(
$value
);
// Remove mention ID from quote
$value = preg_replace(
'/^@([A-z0-9]{64})/',
null,
$this->_escape(
$value
)
);
// Process each line
$lines = [];
foreach ((array) explode(PHP_EOL, $value) as $line)
{
// Skip empty lines
if (empty($line))
{
continue;
}
// Append quote tag if requested
if ($tag)
{
$line = '> ' . $line;
}
$lines[] = $line;
}
// Merge lines
return trim(
implode(
PHP_EOL,
$lines
)
); );
} }
private function _url(?string $path = null, ?string $name = null) private function _link(?string $path = null, ?string $name = null, ?bool $tag = false)
{ {
return return
(
$tag ? '=> ' : null
)
.
( (
$this->_config->gemini->server->port == 1965 ? $this->_config->gemini->server->port == 1965 ?
sprintf( sprintf(

3
src/view/post.gemini

@ -1,5 +1,6 @@
### {author} [{time}] ### {author} [{time}]
{quote} {quote}
{message} {message}
=> {reply} Reply {links}

Loading…
Cancel
Save