]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiToHtml.php
Oops. Syntax error in prev commit
[SourceForge/phpwiki.git] / lib / WikiToHtml.php
1 <?php //-*-php-*-
2 rcs_id('$Id: WikiToHtml.php,v 1.3 2006-06-19 17:33:06 jeannicolas Exp $');
3 /*
4  * Copyright(c) STMicroelectronics, 2006
5  *
6  * Originally written by Jean-Nicolas GEREONE, STMicroelectronics, 2006. 
7
8  This file is part of PhpWiki.
9
10  PhpWiki is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  PhpWiki is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with PhpWiki; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 class WikiToHtml {
26   function WikiToHtml ($wikitext, &$request) {
27         $this->_wikitext = $wikitext;
28         $this->_request =& $request;
29         $this->_html = "";
30         $this->html_content = "";
31     }
32
33     function send() {
34         $this->convert();
35         echo $this->html_content;
36     }
37
38     function convert() {
39         require_once("lib/BlockParser.php");       
40         $xmlcontent = TransformText($this->_wikitext, 2.0, $this->_request->getArg('pagename')); 
41         $this->_html = $xmlcontent->AsXML();
42
43         $this->replace_inside_html();
44     }
45
46     function replace_inside_html() {
47         $this->clean_links();
48         $this->clean_plugin_name();
49         $this->replace_known_plugins();
50         $this->replace_unknown_plugins();
51         //      $this->replace_tags();
52         $this->clean_plugin();
53
54         $this->html_content = $this->_html;
55     }
56
57     // Draft function to replace RichTable
58     // by a html table
59     // Works only on one plugin for the moment
60     function replace_known_plugins() {
61       // If match a plugin
62       $pattern = '/\&lt\;\?plugin\s+RichTable(.*)\?\&gt\;/Umsi';
63       $replace_string = "replace_rich_table";       
64       $this->_html = preg_replace_callback($pattern,
65                                            $replace_string,
66                                            $this->_html);
67     }
68     
69     // Replace unknown plugins by keyword Wikitext { tag }
70     function replace_unknown_plugins() {
71         $pattern = '/(\&lt\;\?plugin[^?]*\?\&gt\;)/Usi';
72         $replace_string = 
73           '<p><div style="background-color:#D3D3D3;font-size:smaller;">Wikitext {
74  <br> \1 <br>}</div><br></p>';
75        
76         $this->_html = preg_replace($pattern,
77                                     $replace_string,
78                                     $this->_html);
79     }
80
81     // Clean links to keep only <a href="link">name</a>
82     function clean_links() {
83       // Existing links
84       $pattern = '/\<a href\=\"index.php\?pagename\=(\w+)\"([^>])*\>/Umsi';      
85       $replace_string = '<a href="\1">';      
86       $this->_html = preg_replace($pattern,
87                                   $replace_string,
88                                   $this->_html) ;
89       // Non existing links
90         $pattern = '/\<a href\=\"index.php\?pagename\=([^"]*)(&amp;action){1}([^>])*\>/Umsi';
91         $replace_string = '<a href="\1">';
92         
93         $this->_html = preg_replace($pattern,
94                                     $replace_string,
95                                     $this->_html) ;
96
97         // Clean underline 
98         $pattern = '/\<u\>(.*)\<\/u\>(\<a href="(.*))[?"]{1}.*\>.*\<\/a\>/Umsi';
99         $replace_string = 
100           '<span>\2" style="color:blue;">\1</a></span>';
101         
102         $this->_html = preg_replace($pattern,
103                                     $replace_string,
104                                     $this->_html) ;
105     }
106     
107     // Put unknown tags in Wikitext {}
108     function replace_tags() {
109       // Replace old table format ( non plugin )
110       $pattern = '/(\ {0,4}(?:\S.*)?\|\S+\s*$.*?\<\/p\>)/ms';
111       $replace_string = 
112         '<p><div style="background-color:#D3D3D3;font-size:smaller;">Wikitext {
113  <br> \1 <br>}</div><br></p>';
114       
115       $this->_html = preg_replace($pattern,
116                                   $replace_string,
117                                   $this->_html);
118
119 }
120     
121     // Replace \n by <br> only in 
122     // <?plugin ? > tag to keep formatting
123     function clean_plugin() {
124         $pattern = '/(\&lt\;\?plugin.*\?\&gt\;)/Umsei';
125         $replace_string = 'preg_replace("/\n/Ums","<br>","\1")';
126         
127         $this->_html = preg_replace($pattern,
128                                     $replace_string,
129                                     $this->_html) ; 
130
131     }
132
133     function clean_plugin_name() {
134         // Remove plugin name converted in a link
135         $pattern = '/(\&lt\;\?plugin\s)\<span.*\>\<span\>\<a href=.*\>(\w+)\<\/a\><\/span\><\/span>([^?]*\?\&gt\;)/Umsi';
136         $replace_string = '\1 \2 \3';
137         $this->_html = preg_replace($pattern,
138                                     $replace_string,
139                                     $this->_html) ; 
140     } 
141 }
142
143 // This is called to replace the RichTable plugin by an html table
144 // $matched contains html <p> tags so 
145 // they are deleted before the conversion.
146 function replace_rich_table($matched) {
147   $plugin = $matched[1];
148
149   $unknown_options = "/colspan|rowspan|width|height/";
150   
151   // if the plugin contains one of the options bellow
152   // it won't be converted
153   if (preg_match($unknown_options,$plugin))
154     return $matched[0]."\n";   
155   else {
156     //Replace unused <p...>
157     $pattern = '/\<p.*\>/Umsi';
158     $replace_string = "";
159     
160     $plugin = preg_replace($pattern,
161                            $replace_string,
162                            $plugin) ; 
163     
164     //replace unused </p> by \n
165     $pattern = '/\<\/p\>/Umsi';
166     $replace_string = "\n";
167     
168     $plugin = preg_replace($pattern,
169                            $replace_string,
170                            $plugin) ; 
171     
172     $plugin = "<?plugin RichTable ".$plugin." ?>";
173     
174     require_once("lib/BlockParser.php");       
175     $xmlcontent = TransformText($plugin, 2.0, $GLOBALS['request']->getArg('pagename')); 
176     return $xmlcontent->AsXML();
177   }
178 }
179
180 // $Log: not supported by cvs2svn $
181 // Revision 1.2  2006/06/05 08:10:19  rurban
182 // stylistic fixup: clarify request argument
183 //
184
185 ?>