Browse Source

improve escape method

main
ghost 9 months ago
parent
commit
3a159fcf4b
  1. 61
      src/controller/room.php

61
src/controller/room.php

@ -276,7 +276,7 @@ class Room
'/[\n\r]+/', '/[\n\r]+/',
PHP_EOL, PHP_EOL,
// Ignore markup // Ignore markup
$this->_plain( $this->_escape(
$post['value'] $post['value']
) )
) )
@ -296,7 +296,7 @@ class Room
preg_replace( preg_replace(
'/[\n\r]+/', '/[\n\r]+/',
PHP_EOL, PHP_EOL,
$this->_plain( $this->_escape(
$record['value'] $record['value']
) )
) )
@ -327,7 +327,7 @@ class Room
preg_replace( preg_replace(
'/[\n\r]+/', '/[\n\r]+/',
PHP_EOL, PHP_EOL,
$this->_plain( $this->_escape(
$record['value'] $record['value']
) )
), ),
@ -421,19 +421,20 @@ class Room
return $texts[(($number % 100) > 4 && ($number % 100) < 20) ? 2 : $cases[min($number % 10, 5)]]; return $texts[(($number % 100) > 4 && ($number % 100) < 20) ? 2 : $cases[min($number % 10, 5)]];
} }
private function _plain(string $value) private function _escape(string $value)
{ {
return trim( // Process each line
str_replace( $lines = [];
[
'#', foreach ((array) explode(PHP_EOL, $value) as $line)
'##', {
'###', // Trim extra separators
'=>', $line = trim(
'>', $line
'*', );
'```'
], // Process each tag on line beginning
foreach (
[ [
'#', '#',
'##', '##',
@ -442,9 +443,35 @@ class Room
'>', '>',
'*', '*',
'```' '```'
], ] as $tag
$value
) )
{
// Escape tags
$line = preg_replace(
sprintf(
'/^(\%s)/',
$tag
),
'[$1]',
$line
);
}
// Escape inline *
$line = preg_replace(
'/[*]{1}([^*]+)[*]{1}/',
'[*]$2[*]',
$line
);
// Merge lines
$lines[] = $line;
}
// Merge lines
return implode(
PHP_EOL,
$lines
); );
} }

Loading…
Cancel
Save