" . $label . "";
}, $str);
$str = preg_replace_callback('/\{\{(.+?)\}\}/', function($m) { # {{post tags here}} ;search post with tags
return "" . $m[1] . "";
}, $str);
# Miscellaneous single line tags subtitution.
$str = preg_replace([
'/\[b\](.+?)\[\/b\]/',
'/\[i\](.+?)\[\/i\]/',
'/(post #(\d+))/i',
'/(forum #(\d+))/i',
'/(comment #(\d+))/i',
'/(pool #(\d+))/i',
# Single line spoiler tags.
'/\[spoilers?\](.+?)\[\/spoilers?\]/',
'/\[spoilers?=(.+?)\](.+?)\[\/spoilers?\]/',
# Multi line spoiler tags.
'/\[spoilers?\]/',
'/\[spoilers?=(.+?)\]/',
'/\[\/spoilers?\]/',
# Quote.
'/\[quote\]/',
'/\[\/quote\]/',
], [
'\1',
'\1',
'\1',
'\1',
'\1',
'\1',
'spoiler\1',
'\1\2',
'spoiler
',
'
\1',
'
',
'
',
'
',
], $str);
$str = self::parseurl($str);
return preg_replace([
# Extraneous newlines before closing div are unnecessary.
'/\n+(<\/div>)/',
# So are after headers, lists, and blockquotes.
'/(<\/(ul|h\d+|blockquote)>)\n+/',
# And after opening blockquote.
'/(
)\n+/',
'/\n/',
], [
'\1',
'\1',
'\1',
'
',
], $str);
}
static public function parseline($str, &$state)
{
if (preg_match('/\d/', end($state)) || preg_match('/^\*+\s+/', $str)) {
return self::parselist($str, $state);
} elseif (preg_match('/^(h[1-6])\.\s*(.+)\n*/', $str, $m)) {
return "<${m[1]}>${m[2]}${m[1]}>";
} else {
return $str;
}
}
static public function parselist($str, &$state)
{
$html = '';
if (!preg_match('/\d/', end($state))) {
$state[] = "1";
$html .= '
';
} else {
$n = substr_count((preg_match('/^\*+\s+/', $str, $m) ? $m[0] : ''), '*');
$last = (int)end($state);
if ($n < $last) {
$html .= str_repeat('
', $last - $n);
array_pop($state);
$state[] = (string)$n;
} elseif ($n > $last) {
$html .= '
';
array_pop($state);
$state[] = (string)($last + 1);
}
if (!preg_match('/^\*+\s+/', $str)) {
array_pop($state);
return $html . self::parseline($str, $state);
}
}
$html .= preg_replace('/\*+\s+(.+)\n*/', '- \1', $str);
return $html;
}
static public function parseurl($str)
{
# Basic URL pattern
$url = '(h?ttps?:\/\/\[?(:{0,2}[\w\-]+)((:{1,2}|\.)[\w\-]+)*\]?(:\d+)*(\/[^\s\n<]*)*)';
# Substitute url tag in this form:
return preg_replace([
'/(^|[\s\(>])' . $url . '/', # url
'/<<\s*' . $url . '\s*\|\s*(.+?)\s*>>/', # <>
'/(^|[\s>])"(.+?)":' . $url . '/', # "label":url
'/<<\s*' . $url . '\s*>>/', # <>
'/\2',
'\7',
'\1\2',
'\1',
'