]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WysiwygEdit/Wikiwyg.php
Remove the mode selector from wikiwyg toolbar.
[SourceForge/phpwiki.git] / lib / WysiwygEdit / Wikiwyg.php
1 <?php
2 rcs_id('$Id: Wikiwyg.php,v 1.11 2007-07-17 14:38:40 labbenes 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         if ($GLOBALS['request']->getArg('mode') && $GLOBALS['request']->getArg('mode') == 'wysiwyg'){
49             return JavaScript($this->_jsdefault . "
50             window.onload = function() {
51             var wikiwyg = new Wikiwyg.Phpwiki();
52             var config = {
53             doubleClickToEdit:  $doubleClickToEdit,
54             javascriptLocation: base_url+'/themes/default/Wikiwyg/',
55             toolbar: {
56                 imagesLocation: base_url+'/themes/default/Wikiwyg/images/',
57                 controlLayout: [
58                        'save','preview','save_button','|',
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
103     function Textarea ($textarea, $wikitext, $name='edit[content]') {
104         global $request;
105     
106         $htmltextid = $this->_htmltextid;
107         $textarea->SetAttr('id', $htmltextid);
108         $iframe0 = new RawXml('<iframe id="iframe0" height="0" width="0" frameborder="0"></iframe>');
109         if ($request->getArg('mode') and $request->getArg('mode') == 'wysiwyg'){
110             $out = HTML(HTML::div(array('class' => 'hint'), 
111                                   _("Warning: This Wikiwyg editor has only Beta quality!")),
112                         $textarea,
113                         $iframe0,
114                         "\n");
115         }else{
116             $out = HTML($textarea, $iframe0, "\n");
117         }
118         return $out;
119     }
120
121     /**
122      * Handler to convert the Wiki Markup to HTML before editing.
123      * This will be converted back by WysiwygEdit_ConvertAfter if required.
124      *  *text* => '<b>text<b>'
125      */
126     function ConvertBefore($text) {
127         return $text;
128     }
129
130     /* 
131      * No special PHP HTML->Wikitext conversion needed. This is done in js thanksfully. 
132      * Avoided in editpage.php: PageEditor->getContent
133      */
134     function ConvertAfter($text) {
135         return TransformInline($text);
136     }
137 }
138
139 class WikiToHtml {
140   function WikiToHtml ($wikitext, &$request) {
141         $this->_wikitext = $wikitext;
142         $this->_request =& $request;
143         $this->_html = "";
144         $this->html_content = "";
145     }
146
147     function send() {
148         $this->convert();
149         echo $this->html_content;
150     }
151
152     function convert() {
153         require_once("lib/BlockParser.php");       
154         $xmlcontent = TransformText($this->_wikitext, 2.0, $this->_request->getArg('pagename')); 
155         $this->_html = $xmlcontent->AsXML();
156
157         $this->replace_inside_html();
158     }
159
160     function replace_inside_html() {
161         global $charset;
162
163         $this->clean_links();
164         $this->clean_plugin_name();
165         $this->replace_known_plugins();
166         $this->replace_unknown_plugins();
167         // $this->replace_tags();
168         $this->clean_plugin();
169
170         if ($charset != 'utf-8') {
171             if ($charset == 'iso-8959-1') {
172                 $this->_html = utf8_decode($this->_html);
173             } else {    
174                 // check for iconv support
175                 loadPhpExtension("iconv");
176                 $this->_html = iconv("UTF-8", $charset, $this->_html);
177             }
178         }
179         $this->html_content = $this->_html;
180     }
181
182     // Draft function to replace RichTable
183     // by a html table
184     // Works only on one plugin for the moment
185     function replace_known_plugins() {
186       // If match a plugin
187       $pattern = '/\&lt\;\?plugin\s+RichTable(.*)\?\&gt\;/Umsi';
188       $replace_string = "replace_rich_table";       
189       $this->_html = preg_replace_callback($pattern,
190                                            $replace_string,
191                                            $this->_html);
192     }
193     
194     // Replace unknown plugins by keyword Wikitext { tag }
195     function replace_unknown_plugins() {
196         $pattern = '/(\&lt\;\?plugin[^?]*\?\&gt\;)/Usi';
197         $replace_string = 
198           '<p><div style="background-color:#D3D3D3;font-size:smaller;">Wikitext {
199  <br> \1 <br>}</div><br></p>';
200        
201         $this->_html = preg_replace($pattern,
202                                     $replace_string,
203                                     $this->_html);
204     }
205
206     // Clean links to keep only <a href="link">name</a>
207     function clean_links() {
208         // Existing links
209         // FIXME: use VIRTUAL_PATH
210         $pattern = '/\<a href\=\"index.php\?pagename\=(\w+)\"([^>])*\>/Umsi';      
211         $replace_string = '<a href="\1">';      
212         $this->_html = preg_replace($pattern,
213                                     $replace_string,
214                                     $this->_html) ;
215         // Non existing links
216         $pattern = '/\<a href\=\"index.php\?pagename\=([^"]*)(&amp;action){1}([^>])*\>/Umsi';
217         $replace_string = '<a href="\1">';
218         
219         $this->_html = preg_replace($pattern,
220                                     $replace_string,
221                                     $this->_html) ;
222
223         // Clean underline 
224         $pattern = '/\<u\>(.*)\<\/u\>(\<a href="(.*))[?"]{1}.*\>.*\<\/a\>/Umsi';
225         $replace_string = 
226             '<span>\2" style="color:blue;">\1</a></span>';
227         
228         $this->_html = preg_replace($pattern,
229                                     $replace_string,
230                                     $this->_html) ;
231     }
232     
233     // Put unknown tags in Wikitext {}
234     function replace_tags() {
235         // Replace old table format ( non plugin )
236         $pattern = '/(\ {0,4}(?:\S.*)?\|\S+\s*$.*?\<\/p\>)/ms';
237         $replace_string = 
238             '<p><div style="background-color:#D3D3D3;font-size:smaller;">Wikitext {
239  <br> \1 <br>}</div><br></p>';
240       
241         $this->_html = preg_replace($pattern,
242                                     $replace_string,
243                                     $this->_html);
244 }
245     
246     // Replace \n by <br> only in 
247     // <?plugin ? > tag to keep formatting
248     function clean_plugin() {
249         $pattern = '/(\&lt\;\?plugin.*\?\&gt\;)/Umsei';
250         $replace_string = 'preg_replace("/\n/Ums","<br>","\1")';
251         
252         $this->_html = preg_replace($pattern,
253                                     $replace_string,
254                                     $this->_html) ; 
255
256     }
257
258     function clean_plugin_name() {
259         // Remove plugin name converted in a link
260         $pattern = '/(\&lt\;\?plugin\s)\<span.*\>\<span\>\<a href=.*\>(\w+)\<\/a\><\/span\><\/span>([^?]*\?\&gt\;)/Umsi';
261         $replace_string = '\1 \2 \3';
262         $this->_html = preg_replace($pattern,
263                                     $replace_string,
264                                     $this->_html) ; 
265     } 
266 }
267
268 // This is called to replace the RichTable plugin by an html table
269 // $matched contains html <p> tags so 
270 // they are deleted before the conversion.
271 function replace_rich_table($matched) {
272     $plugin = $matched[1];
273
274     $unknown_options = "/colspan|rowspan|width|height/";
275   
276     // if the plugin contains one of the options bellow
277     // it won't be converted
278     if (preg_match($unknown_options,$plugin))
279         return $matched[0]."\n";   
280     else {
281         //Replace unused <p...>
282         $pattern = '/\<p.*\>/Umsi';
283         $replace_string = "";
284     
285         $plugin = preg_replace($pattern,
286                                $replace_string,
287                                $plugin) ; 
288     
289         //replace unused </p> by \n
290         $pattern = '/\<\/p\>/Umsi';
291         $replace_string = "\n";
292     
293         $plugin = preg_replace($pattern,
294                                $replace_string,
295                                $plugin) ; 
296     
297         $plugin = "<?plugin RichTable ".$plugin." ?>";
298     
299         require_once("lib/BlockParser.php"); 
300         $xmlcontent = TransformText($plugin, 2.0, $GLOBALS['request']->getArg('pagename')); 
301         return $xmlcontent->AsXML();
302   }
303 }
304
305 /*
306  $Log: not supported by cvs2svn $
307  Revision 1.9  2007/01/07 18:44:00  rurban
308  Improve id: edit: to edit-
309
310  Revision 1.8  2007/01/02 13:20:57  rurban
311  use the new _DEBUG_REMOTE flag. simplify default utf-8 charset conversion, not requiring iconv
312
313  Revision 1.7  2006/12/22 16:53:38  rurban
314  Try to dl() load the iconv extension, if not already loaded
315
316  Revision 1.6  2006/08/25 22:42:51  rurban
317  warn user about beta quality, not to save wrong edits
318
319  Revision 1.5  2006/06/28 14:28:14  jeannicolas
320  Add preview and save button on the toolbar.
321  Fix an IE issue in wikitext mode.
322
323  Revision 1.4  2006/06/19 17:33:06  jeannicolas
324  Add button to insert table of content plugin
325  Add button to insert wikitext section in wysiwyg mode
326
327  Fix internet explorer issue in wikitext mode. The toolbar in this mode didn't work.
328
329  Revision 1.3  2006/05/31 19:59:57  jeannicolas
330  Added wysiwyg_editor 1.1b
331
332  Revision 1.2  2006/05/14 17:52:20  rurban
333  fix syntax error. delete a left-over attempt to add CSS links also. 
334  We did put everything into phpwiki.css for browser compatibility.
335
336  Revision 1.1  2006/05/13 19:59:55  rurban
337  added wysiwyg_editor-1.3a feature by Jean-Nicolas GEREONE <jean-nicolas.gereone@st.com>
338  converted wysiwyg_editor-1.3a js to WysiwygEdit framework
339  changed default ENABLE_WYSIWYG = true and added WYSIWYG_BACKEND = Wikiwyg
340
341
342 */
343
344 // Local Variables:
345 // mode: php
346 // tab-width: 8
347 // c-basic-offset: 4
348 // c-hanging-comment-ender-p: nil
349 // indent-tabs-mode: nil
350 // End:
351 ?>