From a87bfdf6878c29dbbe50476d645f52dad16078fa Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 8 Jun 2014 20:07:24 +1200 Subject: [PATCH] text2html formatter support raw sections --- rebuild.php | 54 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/rebuild.php b/rebuild.php index 96a59e3..3236845 100644 --- a/rebuild.php +++ b/rebuild.php @@ -96,20 +96,50 @@ function hesc($sz) { } function text2html($sz) { - $base = hesc($sz); - - $base = preg_replace('~=+(.+)=+~', '\\1', $base); - $base = preg_replace('~(https?://[^ \\r\\n\\t]+)~i', '\\1', $base); - - $btparts = explode('`', $base); - if (count($btparts) > 1 && (count($btparts) % 2)) { - for ($i = 1, $e = count($btparts); $i < $e; $i += 2) { - $btparts[$i] = ''.$btparts[$i].''; + + $sectionFmt = function($sz) { + $base = hesc($sz); + + $base = preg_replace('~=+(.+)=+~', '\\1', $base); + $base = preg_replace('~(https?://[^ \\r\\n\\t]+)~i', '\\1', $base); + + $btparts = explode('`', $base); + if (count($btparts) > 1 && (count($btparts) % 2)) { + for ($i = 1, $e = count($btparts); $i < $e; $i += 2) { + $btparts[$i] = ''.$btparts[$i].''; + } + $base = implode('', $btparts); } - $base = implode('', $btparts); - } + + 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('', '', $sz); + return $oddEven($htmlSections, $sectionFmt, $identity); - return nl2br($base); } function array_decimate($array, $total, $partno) {