root/trunk/lib/AkConverters/AkHtmlToRtf.php

Revision 1397, 3.1 kB (checked in by bermi, 6 months ago)

COnverting converters to PHP5

Line 
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4 // +----------------------------------------------------------------------+
5 // | Akelos Framework - http://www.akelos.org                             |
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
8 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
9 // +----------------------------------------------------------------------+
10
11 /**
12  * @package ActiveSupport
13  * @subpackage Converters
14  * @author Bermi Ferrer <bermi a.t akelos c.om>
15  * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
16  * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
17  */
18 class AkHtmlToRtf
19 {
20     public $font_face = 0;
21     public $font_size = 24;
22
23     // Convert special characters to ASCII
24     public function escapeCharacter($character)
25     {
26         $escaped = '';
27         if(ord($character) >= 0x00 && ord($character) < 0x20){
28             $escaped = "\\'".dechex(ord($character));
29         }
30
31         if ((ord($character) >= 0x20 && ord($character) < 0x80) || ord($character) == 0x09 || ord($character) == 0x0A){
32             $escaped = $character;
33         }
34
35         if (ord($character) >= 0x80 and ord($character) < 0xFF){
36             $escaped = "\\'".dechex(ord($character));
37         }
38
39         switch(ord($character)) {
40             case 0x5C:
41             case 0x7B:
42             case 0x7D:
43             $escaped = "\\".$character;
44             break;
45         }
46
47         return $escaped;
48     }
49
50     public function specialCharacters($text)
51     {
52         $text_buffer = '';
53         for($i = 0; $i < strlen($text); $i++){
54             $text_buffer .= $this->escapeCharacter($text[$i]);
55         }
56         return $text_buffer;
57     }
58
59     public function convert()
60     {
61         $this->source = str_replace(
62         array('<ul>','<UL>','<ol>','<OL>','</ul>','</UL>','</ol>','</OL>'),
63         '',
64         $this->source
65         );
66         $this->source = $this->specialCharacters($this->source);
67         
68         $rules = array(
69         "/<LI>(.*?)<\/LI>/mi"=> "\\f3\\'B7\\tab\\f{$this->font_face} \\1\\par",
70         "/<P>(.*?)<\/P>/mi" => "\\1\\par ",
71         "/<STRONG>(.*?)<\/STRONG>/mi" => "\\b \\1\\b0 ",
72         "/<B>(.*?)<\/B>/mi" => "\\b \\1\\b0 ",
73         "/<EM>(.*?)<\/EM>/mi" => "\\i \\1\\i0 ",
74         "/<U>(.*?)<\/U>/mi" => "\\ul \\1\\ul0 ",
75         "/<STRIKE>(.*?)<\/STRIKE>/mi" => "\\strike \\1\\strike0 ",
76         "/<SUB>(.*?)<\/SUB>/mi" => "{\\sub \\1}",
77         "/<SUP>(.*?)<\/SUP>/mi" => "{\\super \\1}",
78         "/<H1>(.*?)<\/H1>/mi" => "\\fs48\\b \\1\\b0\\fs{$this->font_size}\\par ",
79         "/<H2>(.*?)<\/H2>/mi" => "\\fs36\\b \\1\\b0\\fs{$this->font_size}\\par ",
80         "/<H3>(.*?)<\/H3>/mi" => "\\fs27\\b \\1\\b0\\fs{$this->font_size}\\par ",
81         "/<HR(.*?)>/i" => "\\brdrb\\brdrs\\brdrw30\\brsp20 \\pard\\par ",
82         "/<BR(.*?)>/i" => "\\par ",
83         "/<TAB(.*?)>/i" => "\\tab ",
84         "/\\n/" => "\\par "
85         );
86         
87         return preg_replace(array_keys($rules),array_values($rules), $this->source);
88
89     }
90
91 }
92
93 ?>
94
Note: See TracBrowser for help on using the browser.