Rearranging scripts to reduce the hassle of updating local application whenever scrip...
[akelos.git] / lib / AkConverters / AkHtmlToRtf.php
blob3644bd9114b8517979b602a4335801409ef29fbe
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
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 // +----------------------------------------------------------------------+
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>
18 class AkHtmlToRtf
20 var $font_face = 0;
21 var $font_size = 24;
23 // Convert special characters to ASCII
24 function escapeCharacter($character)
26 $escaped = '';
27 if(ord($character) >= 0x00 && ord($character) < 0x20){
28 $escaped = "\\'".dechex(ord($character));
31 if ((ord($character) >= 0x20 && ord($character) < 0x80) || ord($character) == 0x09 || ord($character) == 0x0A){
32 $escaped = $character;
35 if (ord($character) >= 0x80 and ord($character) < 0xFF){
36 $escaped = "\\'".dechex(ord($character));
39 switch(ord($character)) {
40 case 0x5C:
41 case 0x7B:
42 case 0x7D:
43 $escaped = "\\".$character;
44 break;
47 return $escaped;
50 function specialCharacters($text)
52 $text_buffer = '';
53 for($i = 0; $i < strlen($text); $i++){
54 $text_buffer .= $this->escapeCharacter($text[$i]);
56 return $text_buffer;
59 function convert()
61 $this->source = str_replace(
62 array('<ul>','<UL>','<ol>','<OL>','</ul>','</UL>','</ol>','</OL>'),
63 '',
64 $this->source
66 $this->source = $this->specialCharacters($this->source);
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 "
87 return preg_replace(array_keys($rules),array_values($rules), $this->source);