+
+ private static function justify($text, $n)
+ {
+ $arr = explode("\n", wordwrap($text, $n));
+ $arr = array_map('trim', $arr);
+ $res = '';
+ foreach ($arr as $key => $line) {
+ $nxl = isset($arr[$key+1]) ? trim($arr[$key+1]) : '';
+ $nxl_split = preg_split('! +!u', $nxl);
+ $nxw_len = count($nxl_split) ? mb_strlen($nxl_split[0]) : 0;
+ $line = trim($line);
+
+ if (mb_strlen($line)+1+$nxw_len < $n) {
+ $res .= "$line\n";
+ continue;
+ }
+
+ if (preg_match('![.:;]$!u',$line)) {
+ $res .= "$line\n";
+ continue;
+ }
+
+ $tmp = preg_split('! +!u', trim($line));
+ $words = count($tmp);
+ if ($words <= 1) {
+ $res .= "$line\n";
+ continue;
+ }
+
+ $len = array_sum(array_map('mb_strlen', $tmp));
+ $empty = $n - $len;
+ $sw = floatval($empty) / floatval($words-1);
+
+ $cur = 0;
+ $l = '';
+ foreach ($tmp as $word) {
+ $l .= $word;
+ $cur += $sw + strlen($word); // Use strlen here instead of mb_strlen because it is used by str_pad
+ // which is not multibyte compatible
+ $l = str_pad($l, intval($cur + 0.5));
+ }
+ $res .= trim($l)."\n";
+ }
+ return trim($res);
+ }
+
+
+ public static function WikiToText($wiki, $just=false, $indent=0, $width=68, $title=false)
+ {
+ if (!$title) {
+ $oldrule12 = MiniWiki::$replacementHTML[MiniWiki::$title_index];
+ MiniWiki::$replacementHTML[MiniWiki::$title_index] = "'$0'";
+ }
+ //$text = trim($wiki);
+ //foreach (MiniWiki::$patternsWiki as $key=>$pattern) {
+ // echo $key . " - " . $pattern . "\n";
+ // $text = preg_replace($pattern, MiniWiki::$replacementText[$key], $text);
+ //}
+ $text = preg_replace(MiniWiki::$patternsWiki, MiniWiki::$replacementText, trim($wiki));
+ if (!$title) {
+ MiniWiki::$replacementHTML[MiniWiki::$title_index] = $oldrule12;
+ }
+ $text = $just ? MiniWiki::justify($text, $width - $indent) : wordwrap($text, $width - $indent);
+ if($indent) {
+ $ind = str_pad('',$indent);
+ $text = $ind.str_replace("\n","\n$ind",$text);
+ }
+ return $text;
+ }
+
+ static public function help($with_title = false)
+ {
+ if (!$with_title) {
+ $info12 = MiniWiki::$info[MiniWiki::$title_index];
+ unset(MiniWiki::$info[MiniWiki::$title_index]);
+ }
+
+ $res = array();
+ foreach (MiniWiki::$info as $value) {
+ $res[$value] = MiniWiki::wikiToHtml($value, true);
+ }
+
+ if (!$with_title) {
+ MiniWiki::$info[MiniWiki::$title_index] = $info12;
+ }
+ return $res;
+ }
+}