typo
[banana.git] / include / wrapper.inc.php
1 <?php
2 /********************************************************************************
3 * include/wrapper.inc.php : text wrapper
4 * -------------------------
5 *
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
9
10 /** recursive function deligne : extracts structure of a text
11 * @param $_text STRING text
12 * @return ARRAY recursive structure
13 * @see dowrap
14 * @see part_wrap
15 * @see wrap
16 */
17
18 function deligne($_text) {
19 // lines => array of source text
20 $lines = split("\n",$_text);
21 // array => final result
22 $array = array();
23
24 // build a part
25 $part = "";
26 // we process non quoted text at the beginning of the post
27 $quoted = false;
28 for ($i=0; $i<count($lines);$i++) {
29 $l = $lines[$i];
30 if ($quoted) {
31 if ((!preg_match("/^(>|\||:) ?/",$l)) || ($i+1==count($lines))) {
32 // line $i normal text, and former line is quoted
33 if ($i+1==count($lines))
34 // last line : updates $part
35 $part .= "$l\n";
36 // updates array and switch to non-quoted text
37 $array[] = deligne($part);
38 $quoted = false;
39 $part = $l."\n";
40 } else {
41 // line $i and former line are quoted : updates $part
42 $part .= preg_replace("/^(>|\||:) ?/","",$l)."\n";
43 }
44 } else {
45 if ((preg_match("/^(>|\||:) ?/",$l)) || ($i+1==count($lines))) {
46 // line $i quoted text, and former line is normal
47 if ($i+1==count($lines))
48 // last line : updates $part
49 $part .= "$l\n";
50 // updates array and switch to quoted text
51 $array[] = substr($part,0,-1);
52 $quoted = true;
53 $part = preg_replace("/^(>|\||:) ?/","",$l)."\n";
54 } else {
55 // line $i and former line are normal : updates $part
56 $part .= $l."\n";
57 }
58 }
59 }
60 return $array;
61 }
62
63 /** wrapping function : "intelligent wrap"
64 * @param $_text STRING text to wrap
65 * @param $_maxls INTEGER source text wrap length
66 * @param $_maxld INTEGER destination text wrap length
67 * @return STRING wrapped text
68 * @see dowrap
69 * @see deligne
70 * @see wrap
71 */
72
73 function part_wrap($_text,$_maxls,$_maxld=72) {
74 // lines => array of source text lines
75 $lines = split("\n",$_text);
76 // last => size of last line processed
77 $last = -1;
78 // result => final result
79 $result = "";
80 foreach ($lines as $l) {
81 if ($last != -1) {
82 // skip first line
83 if (strpos($l," ")+$last<$_maxls) {
84 // if first word is too short, this line begins a new paragraph
85 $result .= "\n";
86 } else {
87 // if not, continue on the same line
88 $result .= " ";
89 }
90 }
91 $result .= chop($l);
92 $last = strlen($l);
93 }
94 return wordwrap($result,$_maxld,"\n",0);
95 }
96
97 /** recursive function : rewrap quoted text
98 * @param $_mixed ARRAY structure
99 * @param $_maxls INTEGER source wrap length
100 * @param $_maxld INTEGER destination wrap length
101 * @param $_prefix STRING prefix for destination text
102 * @return STRING wrapped text
103 * @see deligne
104 * @see part_wrap
105 * @see wrap
106 */
107
108 function dowrap($_mixed,$_maxls=72,$_maxld=72,$_prefix="") {
109 // result => final result
110 $result = "";
111 // array : it is quoted text. loop on content to process
112 if (is_array($_mixed)) {
113 foreach ($_mixed as $d) {
114 $result.=dowrap($d,$_maxls,$_maxld,$_prefix.">");
115 }
116 // text : "normal" text. return wrapped text
117 } else {
118 // computes destination text size [Assume no software wraps text under 65 chars]
119 $maxls = max(max(array_map("strlen",split("\n",$_mixed))),65);
120 // wraps text
121 $preresult = part_wrap($_mixed,$_maxls,$_maxld-strlen($_prefix." "));
122 // adds prefix
123 $lines = split("\n",$preresult);
124 foreach ($lines as $l) {
125 $result .= $_prefix.(strlen($_prefix)>0?" ":"")."$l\n";
126 }
127 // $result = substr($result,0,-1);
128 }
129 return $result;
130 }
131
132
133 /** wrap function
134 * @param $_text STRING text
135 * @param $_prefix STRING prefix (eg. quotes)
136 * @param $_length STRING wrap length
137 * @return STRING wrapped text
138 * @see deligne
139 * @see part_wrap
140 * @see dowrap
141 */
142
143 function wrap($_text,$_prefix="",$_length=72) {
144 $lines = split("\n",$_text);
145 $max = max(array_map("strlen",$lines));
146 $delig = deligne($_text);
147 $result = "";
148
149 foreach ($delig as $d) {
150 $result .= dowrap($d,$max,$_length,$_prefix);
151 }
152
153 return $result;
154 }
155
156 ?>
157
158