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