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