text2html formatter support raw <html> sections

This commit is contained in:
mappu 2014-06-08 20:07:24 +12:00
parent 79c07edb4d
commit a87bfdf687

View File

@ -96,20 +96,50 @@ function hesc($sz) {
} }
function text2html($sz) { function text2html($sz) {
$base = hesc($sz);
$base = preg_replace('~=+(.+)=+~', '<strong>\\1</strong>', $base); $sectionFmt = function($sz) {
$base = preg_replace('~(https?://[^ \\r\\n\\t]+)~i', '<a href="\\1">\\1</a>', $base); $base = hesc($sz);
$btparts = explode('`', $base); $base = preg_replace('~=+(.+)=+~', '<strong>\\1</strong>', $base);
if (count($btparts) > 1 && (count($btparts) % 2)) { $base = preg_replace('~(https?://[^ \\r\\n\\t]+)~i', '<a href="\\1">\\1</a>', $base);
for ($i = 1, $e = count($btparts); $i < $e; $i += 2) {
$btparts[$i] = '<span class="code">'.$btparts[$i].'</span>'; $btparts = explode('`', $base);
if (count($btparts) > 1 && (count($btparts) % 2)) {
for ($i = 1, $e = count($btparts); $i < $e; $i += 2) {
$btparts[$i] = '<span class="code">'.$btparts[$i].'</span>';
}
$base = implode('', $btparts);
} }
$base = implode('', $btparts);
}
return nl2br($base); return nl2br($base);
};
$identity = function($sz) {
return $sz;
};
$splitInside = function($begin, $end, $sz) {
$parts = explode($begin, $sz);
if (count($parts) == 1) return [$sz];
$ret = [$parts[0]];
foreach($parts as $part) {
$inner = explode($end, $part, 2);
$ret = array_merge($ret, $inner);
}
return $ret;
};
$oddEven = function(array $parts, $odd, $even, $join='') {
$ret = [];
for($i = 0, $e = count($parts); $i != $e; ++$i) {
$ret[] = ($i % 2) ? $odd($parts[$i]) : $even($parts[$i]);
}
return implode($join, $ret);
};
$htmlSections = $splitInside('<html>', '</html>', $sz);
return $oddEven($htmlSections, $sectionFmt, $identity);
} }
function array_decimate($array, $total, $partno) { function array_decimate($array, $total, $partno) {