/[drupal]/contributions/sandbox/alaa/bidi.module
ViewVC logotype

Contents of /contributions/sandbox/alaa/bidi.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Thu Mar 3 15:59:48 2005 UTC (4 years, 8 months ago) by alaa
Branch: MAIN
File MIME type: text/x-php
handful of little hacks
1 <?php // -*-php-*-
2 /*
3 * Original module by Alaa Abd El Fatah.
4 * Mohammed Sameer: My Modifications Copyright (c) 2004 Mohammed Sameer,
5 * under the GNU GPL v2 or later.
6 * Mohammed Sameer: 2004 10 30
7 * * Ported to drupal 4.5 API.
8 */
9
10 setlocale(LC_ALL, "ar_EG.UTF-8");
11
12 function bidi_help($section) {
13 $output = "";
14
15 switch ($section) {
16 case 'admin/modules#description':
17 $output = t("Automaticaly sets line direction");
18 break;
19 }
20
21 return $output;
22 }
23
24
25 function bidi_filter($op, $delta = 0, $format = -1, $text = "") {
26 switch ($op) {
27 case 'list':
28 return array(0 => t('BiDi'));
29 // case "name":
30 // return t("Bidi filter");
31 case 'description':
32 return t("Bidi filter");
33 case "process":
34 return _bidi_filter_process($text);
35 case "settings":
36 return _bidi_filter_settings($text);
37 default:
38 return $text;
39 }
40 }
41
42
43 //get dir needs to be split into a single include file so it can be used on theme and other places
44 function _get_dir($text) {
45 $text = strtolower($text);
46 $len = strlen($text);
47
48 $arabic = 0;
49 $english = 0;
50
51 //constants
52 $a = ord('a');
53 $z = ord('z');
54
55 //arabic UTF-8 letters have one of these values in the 1st byte
56 $ar1 = 0xd8;
57 $ar2 = 0xd9;
58
59 // this calculates the dominant language in the text, most bidi implementations just use the first letter (which may be more efficient)
60 for ($i = 0; $i < $len; ++$i) {
61 $bin = ord($text[$i]);
62
63 if ($bin == $ar1 || $bin == $ar2) {
64 ++$arabic;
65 ++$i;
66 } else if ($bin >= $a && $bin <= $z) {
67 ++$english;
68 }
69 }
70
71 //should this return a bool instead?
72 if ($english || $arabic) {
73 if ($english > $arabic)
74 return ' class="left2right"';
75 else
76 return ' class="right2left"';
77 } else {
78 return "";
79 }
80 }
81
82
83 function _bidi_filter_process($text) {
84 $output = "";
85
86 $len = strlen($text);
87 $i = 0;
88
89
90 while ($i < $len) {
91 //find next tag
92 $e = strpos(substr($text, $i), "<");
93 if ($e === FALSE) break;
94
95 $output.=substr($text, $i, $e);
96
97 $i += $e;
98
99 //find end of next tag
100 $e = strcspn(substr($text, $i), "/> ");
101 $tag = substr($text, $i+1, $e-1);
102
103 $output.=substr($text, $i, $e);
104 $i += $e;
105
106
107 switch ($tag) {
108 //single tag entity
109 case "img":
110 case "br":
111 case "hr":
112 continue;
113 break;
114
115 //nested cases
116 case "ul":
117 case "ol":
118 case "dl":
119 case "div":
120 $e = 0;
121 $j = 1; //stack counter
122
123 // if $j = 0 then stack is empty
124 while ($j > 0) {
125 $e += strpos(substr($text, $i+$e) , $tag);
126
127 //yeah I know I'm assuming certain sizes for tags, sue me
128 if ($text[$i+$e-1] == '<') { //new nested block
129 ++$j;
130 } else if ($text[$i+$e-1] == '/') { //end of block
131 --$j;
132 }
133 $e += 2; //skip current tag
134 }
135 $content = substr($text, $i+1, $e);
136 $output.=_get_dir(strip_tags($content));
137 $e += strlen($tag);
138 $output.= substr($text, $i, $e);
139 $i += $e;
140 break;
141
142
143 //block elements these get out attention
144 case "p":
145 case "blockquote":
146 case "pre":
147 case "h1":
148 case "h2":
149 case "h3":
150 case "h4":
151 case "h5":
152 case "h6":
153
154 //nested lists are proken better think of a fix
155 case "li":
156 $close_tag = "</".$tag.">";
157 //breaks on extra whitespaces, maybe I should use regexps instead
158 $e = strpos(substr($text, $i), $close_tag);
159 $content = substr($text, $i+1, $e-1);
160 //calculate directions
161 $output.= _get_dir(strip_tags($content));
162 $e += strlen($close_tag);
163 $output.= substr($text, $i, $e);
164 $i += $e;
165
166 break;
167
168 //non block tags we are not handeling
169 default:
170 $close_tag = "</".$tag.">";
171 $e = strpos(substr($text, $i), $close_tag) + strlen($close_tag);
172 $output.= substr($text, $i, $e);
173 $i += $e;
174 }
175
176 }
177
178 //any missing content
179 $output .= substr($text, $i);
180
181
182 return $output;
183 }
184
185
186 function _bidi_filter_settings() {
187 return form_group(t("Bidi filter"), t("automatic bidi"));
188 }
189
190 function bidi_filter_tips() {
191 if ($long)
192 {
193
194 }
195 else {
196 return t("You may write mixed Arabic and English freely, line direction will be computed automaticaly");
197 }
198 }
199
200 ?>

  ViewVC Help
Powered by ViewVC 1.1.2