]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WysiwygEdit/Wikiwyg.php
use the new _DEBUG_REMOTE flag. simplify default utf-8 charset conversion, not requir...
[SourceForge/phpwiki.git] / lib / WysiwygEdit / Wikiwyg.php
1 <?php
2 rcs_id('$Id: Wikiwyg.php,v 1.8 2007-01-02 13:20:57 rurban Exp $');
3 /**
4  * Wikiwyg is compatible with most internet browsers which
5  * include: IE 5.5+ (Windows), Firefox 1.0+, Mozilla 1.3+
6  * and Netscape 7+.
7  *
8  * Download: http://openjsan.org/doc/i/in/ingy/Wikiwyg/
9  * Suggested installation into themes/default/Wikiwyg/
10  *
11  * @package WysiwygEdit
12  * @author  Reini Urban, based on a patch by Jean-Nicolas GEREONE, STMicroelectronics, 2006
13  * Current maintainer: Sabri LABBENE, STMicroelectronics, 2006
14  */
15
16 require_once("lib/WysiwygEdit.php");
17
18 class WysiwygEdit_Wikiwyg extends WysiwygEdit {
19
20     function WysiwygEdit_Wikiwyg() {
21         global $request, $LANG;
22         $this->_transformer_tags = false;
23         $this->BasePath = DATA_PATH.'/themes/default/Wikiwyg';
24         $this->_htmltextid = "edit:content";
25         $this->_wikitextid = "editareawiki";
26         $script_url = deduce_script_name();
27         if ((DEBUG & _DEBUG_REMOTE) and isset($_GET['start_debug']))
28             $script_url .= ("?start_debug=".$_GET['start_debug']);
29         $this->_jsdefault = "
30 var base_url = '".DATA_PATH."';
31 var data_url = '$this->BasePath';
32 var script_url = '$script_url';
33 var pagename = '".$request->getArg('pagename')."';
34 ";
35     }
36
37     function Head($name='edit[content]') {
38         global $WikiTheme;
39         foreach (array("Wikiwyg.js","Wikiwyg/Toolbar.js","Wikiwyg/Preview.js","Wikiwyg/Wikitext.js",
40                        "Wikiwyg/Wysiwyg.js","Wikiwyg/Phpwiki.js","Wikiwyg/HTML.js",
41                        "Wikiwyg/Toolbar.js") as $js) {
42             $WikiTheme->addMoreHeaders
43                 (Javascript('', array('src' => $this->BasePath . '/' . $js,
44                                       'language' => 'JavaScript')));
45         }
46         $doubleClickToEdit = ($GLOBALS['request']->getPref('doubleClickEdit') or ENABLE_DOUBLECLICKEDIT) 
47             ? 'true' : 'false';
48         return JavaScript($this->_jsdefault . "
49 window.onload = function() {
50    var wikiwyg = new Wikiwyg.Phpwiki();
51    var config = {
52             doubleClickToEdit:  $doubleClickToEdit,
53             javascriptLocation: base_url+'/themes/default/Wikiwyg/',
54             toolbar: {
55                 imagesLocation: base_url+'/themes/default/Wikiwyg/images/',
56                 controlLayout: [
57                        'save','preview','save_button','|',
58                        'mode_selector', '/',
59                        'p','|',
60                        'h2', 'h3', 'h4','|',
61                        'bold', 'italic', '|',
62                        'sup', 'sub', '|',
63                        'toc',
64                        'wikitext','|',
65                        'pre','|',
66                        'ordered', 'unordered','hr','|',
67                        'link','|',
68                        'table'
69                        ],
70                 styleSelector: [
71                        'label', 'p', 'h2', 'h3', 'h4', 'pre'
72                                 ], 
73                 controlLabels: {
74                        save:     '"._("Apply changes")."',
75                        cancel:   '"._("Exit toolbar")."',
76                        h2:       '"._("Title 1")."',
77                        h3:       '"._("Title 2")."',
78                        h4:       '"._("Title 3")."',
79                        verbatim: '"._("Verbatim")."',
80                        toc:   '"._("Table of content")."', 
81                        wikitext:   '"._("Insert Wikitext section")."', 
82                        sup:      '"._("Sup")."', 
83                        sub:      '"._("Sub")."',
84                        preview:  '"._("Preview")."',   
85                        save_button:'"._("Save")."'   
86                       }
87             },
88             wysiwyg: {
89                 iframeId: 'iframe0'
90             },
91             wikitext: {
92               supportCamelCaseLinks: true
93             }
94    };
95    var div = document.getElementById(\"" . $this->_htmltextid . "\");
96    wikiwyg.createWikiwygArea(div, config);
97    wikiwyg_divs.push(wikiwyg);
98    wikiwyg.editMode();
99 }");
100     }
101
102     function Textarea ($textarea, $wikitext, $name='edit[content]') {
103         $htmltextid = $this->_htmltextid;
104         $textarea->SetAttr('id', $htmltextid);
105         $iframe0 = new RawXml('<iframe id="iframe0" height="0" width="0" frameborder="0"></iframe>');
106         $out = HTML(HTML::div(array('class' => 'hint'), 
107                               _("Warning: This Wikiwyg editor has only Beta quality!")),
108                     $textarea,
109                     $iframe0,
110                     "\n");
111         return $out;
112     }
113
114     /**
115      * Handler to convert the Wiki Markup to HTML before editing.
116      * This will be converted back by WysiwygEdit_ConvertAfter if required.
117      *  *text* => '<b>text<b>'
118      */
119     function ConvertBefore($text) {
120         return $text;
121     }
122
123     /* 
124      * No special PHP HTML->Wikitext conversion needed. This is done in js thanksfully. 
125      * Avoided in editpage.php: PageEditor->getContent
126      */
127     function ConvertAfter($text) {
128         return TransformInline($text);
129     }
130 }
131
132 class WikiToHtml {
133   function WikiToHtml ($wikitext, &$request) {
134         $this->_wikitext = $wikitext;
135         $this->_request =& $request;
136         $this->_html = "";
137         $this->html_content = "";
138     }
139
140     function send() {
141         $this->convert();
142         echo $this->html_content;
143     }
144
145     function convert() {
146         require_once("lib/BlockParser.php");       
147         $xmlcontent = TransformText($this->_wikitext, 2.0, $this->_request->getArg('pagename')); 
148         $this->_html = $xmlcontent->AsXML();
149
150         $this->replace_inside_html();
151     }
152
153     function replace_inside_html() {
154         global $charset;
155
156         $this->clean_links();
157         $this->clean_plugin_name();
158         $this->replace_known_plugins();
159         $this->replace_unknown_plugins();
160         // $this->replace_tags();
161         $this->clean_plugin();
162
163         if ($charset != 'utf-8') {
164             if ($charset == 'iso-8959-1') {
165                 $this->_html = utf8_decode($this->_html);
166             } else {    
167                 // check for iconv support
168                 loadPhpExtension("iconv");
169                 $this->_html = iconv("UTF-8", $charset, $this->_html);
170             }
171         }
172         $this->html_content = $this->_html;
173     }
174
175     // Draft function to replace RichTable
176     // by a html table
177     // Works only on one plugin for the moment
178     function replace_known_plugins() {
179       // If match a plugin
180       $pattern = '/\&lt\;\?plugin\s+RichTable(.*)\?\&gt\;/Umsi';
181       $replace_string = "replace_rich_table";       
182       $this->_html = preg_replace_callback($pattern,
183                                            $replace_string,
184                                            $this->_html);
185     }
186     
187     // Replace unknown plugins by keyword Wikitext { tag }
188     function replace_unknown_plugins() {
189         $pattern = '/(\&lt\;\?plugin[^?]*\?\&gt\;)/Usi';
190         $replace_string = 
191           '<p><div style="background-color:#D3D3D3;font-size:smaller;">Wikitext {
192  <br> \1 <br>}</div><br></p>';
193        
194         $this->_html = preg_replace($pattern,
195                                     $replace_string,
196                                     $this->_html);
197     }
198
199     // Clean links to keep only <a href="link">name</a>
200     function clean_links() {
201         // Existing links
202         // FIXME: use VIRTUAL_PATH
203         $pattern = '/\<a href\=\"index.php\?pagename\=(\w+)\"([^>])*\>/Umsi';      
204         $replace_string = '<a href="\1">';      
205         $this->_html = preg_replace($pattern,
206                                     $replace_string,
207                                     $this->_html) ;
208         // Non existing links
209         $pattern = '/\<a href\=\"index.php\?pagename\=([^"]*)(&amp;action){1}([^>])*\>/Umsi';
210         $replace_string = '<a href="\1">';
211         
212         $this->_html = preg_replace($pattern,
213                                     $replace_string,
214                                     $this->_html) ;
215
216         // Clean underline 
217         $pattern = '/\<u\>(.*)\<\/u\>(\<a href="(.*))[?"]{1}.*\>.*\<\/a\>/Umsi';
218         $replace_string = 
219             '<span>\2" style="color:blue;">\1</a></span>';
220         
221         $this->_html = preg_replace($pattern,
222                                     $replace_string,
223                                     $this->_html) ;
224     }
225     
226     // Put unknown tags in Wikitext {}
227     function replace_tags() {
228         // Replace old table format ( non plugin )
229         $pattern = '/(\ {0,4}(?:\S.*)?\|\S+\s*$.*?\<\/p\>)/ms';
230         $replace_string = 
231             '<p><div style="background-color:#D3D3D3;font-size:smaller;">Wikitext {
232  <br> \1 <br>}</div><br></p>';
233       
234         $this->_html = preg_replace($pattern,
235                                     $replace_string,
236                                     $this->_html);
237 }
238     
239     // Replace \n by <br> only in 
240     // <?plugin ? > tag to keep formatting
241     function clean_plugin() {
242         $pattern = '/(\&lt\;\?plugin.*\?\&gt\;)/Umsei';
243         $replace_string = 'preg_replace("/\n/Ums","<br>","\1")';
244         
245         $this->_html = preg_replace($pattern,
246                                     $replace_string,
247                                     $this->_html) ; 
248
249     }
250
251     function clean_plugin_name() {
252         // Remove plugin name converted in a link
253         $pattern = '/(\&lt\;\?plugin\s)\<span.*\>\<span\>\<a href=.*\>(\w+)\<\/a\><\/span\><\/span>([^?]*\?\&gt\;)/Umsi';
254         $replace_string = '\1 \2 \3';
255         $this->_html = preg_replace($pattern,
256                                     $replace_string,
257                                     $this->_html) ; 
258     } 
259 }
260
261 // This is called to replace the RichTable plugin by an html table
262 // $matched contains html <p> tags so 
263 // they are deleted before the conversion.
264 function replace_rich_table($matched) {
265     $plugin = $matched[1];
266
267     $unknown_options = "/colspan|rowspan|width|height/";
268   
269     // if the plugin contains one of the options bellow
270     // it won't be converted
271     if (preg_match($unknown_options,$plugin))
272         return $matched[0]."\n";   
273     else {
274         //Replace unused <p...>
275         $pattern = '/\<p.*\>/Umsi';
276         $replace_string = "";
277     
278         $plugin = preg_replace($pattern,
279                                $replace_string,
280                                $plugin) ; 
281     
282         //replace unused </p> by \n
283         $pattern = '/\<\/p\>/Umsi';
284         $replace_string = "\n";
285     
286         $plugin = preg_replace($pattern,
287                                $replace_string,
288                                $plugin) ; 
289     
290         $plugin = "<?plugin RichTable ".$plugin." ?>";
291     
292         require_once("lib/BlockParser.php"); 
293         $xmlcontent = TransformText($plugin, 2.0, $GLOBALS['request']->getArg('pagename')); 
294         return $xmlcontent->AsXML();
295   }
296 }
297
298 /*
299  $Log: not supported by cvs2svn $
300  Revision 1.7  2006/12/22 16:53:38  rurban
301  Try to dl() load the iconv extension, if not already loaded
302
303  Revision 1.6  2006/08/25 22:42:51  rurban
304  warn user about beta quality, not to save wrong edits
305
306  Revision 1.5  2006/06/28 14:28:14  jeannicolas
307  Add preview and save button on the toolbar.
308  Fix an IE issue in wikitext mode.
309
310  Revision 1.4  2006/06/19 17:33:06  jeannicolas
311  Add button to insert table of content plugin
312  Add button to insert wikitext section in wysiwyg mode
313
314  Fix internet explorer issue in wikitext mode. The toolbar in this mode didn't work.
315
316  Revision 1.3  2006/05/31 19:59:57  jeannicolas
317  Added wysiwyg_editor 1.1b
318
319  Revision 1.2  2006/05/14 17:52:20  rurban
320  fix syntax error. delete a left-over attempt to add CSS links also. 
321  We did put everything into phpwiki.css for browser compatibility.
322
323  Revision 1.1  2006/05/13 19:59:55  rurban
324  added wysiwyg_editor-1.3a feature by Jean-Nicolas GEREONE <jean-nicolas.gereone@st.com>
325  converted wysiwyg_editor-1.3a js to WysiwygEdit framework
326  changed default ENABLE_WYSIWYG = true and added WYSIWYG_BACKEND = Wikiwyg
327
328
329 */
330
331 // Local Variables:
332 // mode: php
333 // tab-width: 8
334 // c-basic-offset: 4
335 // c-hanging-comment-ender-p: nil
336 // indent-tabs-mode: nil
337 // End:
338 ?>