From 3ee590a9b814a935db5304da75455d1d31c76f40 Mon Sep 17 00:00:00 2001 From: x99bachelart Date: Wed, 3 Sep 2003 12:07:26 +0000 Subject: [PATCH] wrapper --- include/wrapper.inc.php | 158 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 include/wrapper.inc.php diff --git a/include/wrapper.inc.php b/include/wrapper.inc.php new file mode 100644 index 0000000..502e116 --- /dev/null +++ b/include/wrapper.inc.php @@ -0,0 +1,158 @@ + array of source text + $lines = split("\n",$_text); +// array => final result + $array = array(); + +// build a part + $part = ""; +// we process non quoted text at the beginning of the post + $quoted = false; + for ($i=0; $i|\||:) ?/",$l)) || ($i+1==count($lines))) { +// line $i normal text, and former line is quoted + if ($i+1==count($lines)) +// last line : updates $part + $part .= "$l\n"; +// updates array and switch to non-quoted text + $array[] = deligne($part); + $quoted = false; + $part = $l."\n"; + } else { +// line $i and former line are quoted : updates $part + $part .= preg_replace("/^(>|\||:) ?/","",$l)."\n"; + } + } else { + if ((preg_match("/^(>|\||:) ?/",$l)) || ($i+1==count($lines))) { +// line $i quoted text, and former line is normal + if ($i+1==count($lines)) +// last line : updates $part + $part .= "$l\n"; +// updates array and switch to quoted text + $array[] = substr($part,0,-1); + $quoted = true; + $part = preg_replace("/^(>|\||:) ?/","",$l)."\n"; + } else { +// line $i and former line are normal : updates $part + $part .= $l."\n"; + } + } + } + return $array; +} + +/** wrapping function : "intelligent wrap" + * @param $_text STRING text to wrap + * @param $_maxls INTEGER source text wrap length + * @param $_maxld INTEGER destination text wrap length + * @return STRING wrapped text + * @see dowrap + * @see deligne + * @see wrap + */ + +function part_wrap($_text,$_maxls,$_maxld=72) { +// lines => array of source text lines + $lines = split("\n",$_text); +// last => size of last line processed + $last = -1; +// result => final result + $result = ""; + foreach ($lines as $l) { + if ($last != -1) { +// skip first line + if (strpos($l," ")+$last<$_maxls) { +// if first word is too short, this line begins a new paragraph + $result .= "\n"; + } else { +// if not, continue on the same line + $result .= " "; + } + } + $result .= chop($l); + $last = strlen($l); + } + return wordwrap($result,$_maxld,"\n",0); +} + +/** recursive function : rewrap quoted text + * @param $_mixed ARRAY structure + * @param $_maxls INTEGER source wrap length + * @param $_maxld INTEGER destination wrap length + * @param $_prefix STRING prefix for destination text + * @return STRING wrapped text + * @see deligne + * @see part_wrap + * @see wrap + */ + +function dowrap($_mixed,$_maxls=72,$_maxld=72,$_prefix="") { +// result => final result + $result = ""; +// array : it is quoted text. loop on content to process + if (is_array($_mixed)) { + foreach ($_mixed as $d) { + $result.=dowrap($d,$_maxls,$_maxld,$_prefix.">"); + } +// text : "normal" text. return wrapped text + } else { +// computes destination text size [Assume no software wraps text under 65 chars] + $maxls = max(max(array_map("strlen",split("\n",$_mixed))),65); +// wraps text + $preresult = part_wrap($_mixed,$_maxls,$_maxld-strlen($_prefix." ")); +// adds prefix + $lines = split("\n",$preresult); + foreach ($lines as $l) { + $result .= $prefix.(strlen($_prefix)>0?" ":"")."$l\n"; + } + // $result = substr($result,0,-1); + } + return $result; +} + + +/** wrap function + * @param $_text STRING text + * @param $_prefix STRING prefix (eg. quotes) + * @param $_length STRING wrap length + * @return STRING wrapped text + * @see deligne + * @see part_wrap + * @see dowrap + */ + +function wrap($_text,$_prefix="",$_length=72) { + $lines = split("\n",$_text); + $max = max(array_map("strlen",$lines)); + $delig = deligne($_text); + $result = ""; + + foreach ($delig as $d) { + $result .= dowrap($d,$max,$_length,$_prefix); + } + + return $result; +} + +?> + + -- 2.1.4