File: Wikiwyg.php

package info (click to toggle)
phpwiki 1.3.14-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 15,716 kB
  • ctags: 23,548
  • sloc: php: 88,295; sql: 1,476; sh: 1,378; perl: 765; makefile: 602; awk: 28
file content (344 lines) | stat: -rwxr-xr-x 11,464 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
rcs_id('$Id: Wikiwyg.php,v 1.9 2007/01/07 18:44:00 rurban Exp $');
/**
 * Wikiwyg is compatible with most internet browsers which
 * include: IE 5.5+ (Windows), Firefox 1.0+, Mozilla 1.3+
 * and Netscape 7+.
 *
 * Download: http://openjsan.org/doc/i/in/ingy/Wikiwyg/
 * Suggested installation into themes/default/Wikiwyg/
 *
 * @package WysiwygEdit
 * @author  Reini Urban, based on a patch by Jean-Nicolas GEREONE, STMicroelectronics, 2006
 * Current maintainer: Sabri LABBENE, STMicroelectronics, 2006
 */

require_once("lib/WysiwygEdit.php");

class WysiwygEdit_Wikiwyg extends WysiwygEdit {

    function WysiwygEdit_Wikiwyg() {
        global $request, $LANG;
        $this->_transformer_tags = false;
	$this->BasePath = DATA_PATH.'/themes/default/Wikiwyg';
	$this->_htmltextid = "edit-content";
        $this->_wikitextid = "editareawiki";
	$script_url = deduce_script_name();
	if ((DEBUG & _DEBUG_REMOTE) and isset($_GET['start_debug']))
	    $script_url .= ("?start_debug=".$_GET['start_debug']);
    	$this->_jsdefault = "
var base_url = '".DATA_PATH."';
var data_url = '$this->BasePath';
var script_url = '$script_url';
var pagename = '".$request->getArg('pagename')."';
";
    }

    function Head($name='edit[content]') {
        global $WikiTheme;
        foreach (array("Wikiwyg.js","Wikiwyg/Toolbar.js","Wikiwyg/Preview.js","Wikiwyg/Wikitext.js",
                       "Wikiwyg/Wysiwyg.js","Wikiwyg/Phpwiki.js","Wikiwyg/HTML.js",
                       "Wikiwyg/Toolbar.js") as $js) {
            $WikiTheme->addMoreHeaders
                (Javascript('', array('src' => $this->BasePath . '/' . $js,
                                      'language' => 'JavaScript')));
        }
        $doubleClickToEdit = ($GLOBALS['request']->getPref('doubleClickEdit') or ENABLE_DOUBLECLICKEDIT) 
            ? 'true' : 'false';
        return JavaScript($this->_jsdefault . "
window.onload = function() {
   var wikiwyg = new Wikiwyg.Phpwiki();
   var config = {
            doubleClickToEdit:  $doubleClickToEdit,
            javascriptLocation: base_url+'/themes/default/Wikiwyg/',
            toolbar: {
	        imagesLocation: base_url+'/themes/default/Wikiwyg/images/',
		controlLayout: [
		       'save','preview','save_button','|',
                       'mode_selector', '/',
		       'p','|',
		       'h2', 'h3', 'h4','|',
		       'bold', 'italic', '|',
                       'sup', 'sub', '|',
                       'toc',
                       'wikitext','|',
		       'pre','|',
		       'ordered', 'unordered','hr','|',
		       'link','|',
                       'table'
		       ],
		styleSelector: [
		       'label', 'p', 'h2', 'h3', 'h4', 'pre'
				], 
		controlLabels: {
	               save:     '"._("Apply changes")."',
		       cancel:   '"._("Exit toolbar")."',
		       h2:       '"._("Title 1")."',
		       h3:       '"._("Title 2")."',
		       h4:       '"._("Title 3")."',
		       verbatim: '"._("Verbatim")."',
                       toc:   '"._("Table of content")."', 
                       wikitext:   '"._("Insert Wikitext section")."', 
                       sup:      '"._("Sup")."', 
                       sub:      '"._("Sub")."',
                       preview:  '"._("Preview")."',   
                       save_button:'"._("Save")."'   
	              }
            },
            wysiwyg: {
                iframeId: 'iframe0'
            },
	    wikitext: {
	      supportCamelCaseLinks: true
	    }
   };
   var div = document.getElementById(\"" . $this->_htmltextid . "\");
   wikiwyg.createWikiwygArea(div, config);
   wikiwyg_divs.push(wikiwyg);
   wikiwyg.editMode();
}");
    }

    function Textarea ($textarea, $wikitext, $name='edit[content]') {
        $htmltextid = $this->_htmltextid;
        $textarea->SetAttr('id', $htmltextid);
        $iframe0 = new RawXml('<iframe id="iframe0" height="0" width="0" frameborder="0"></iframe>');
        $out = HTML(HTML::div(array('class' => 'hint'), 
                              _("Warning: This Wikiwyg editor has only Beta quality!")),
                    $textarea,
                    $iframe0,
		    "\n");
	return $out;
    }

    /**
     * Handler to convert the Wiki Markup to HTML before editing.
     * This will be converted back by WysiwygEdit_ConvertAfter if required.
     *  *text* => '<b>text<b>'
     */
    function ConvertBefore($text) {
        return $text;
    }

    /* 
     * No special PHP HTML->Wikitext conversion needed. This is done in js thanksfully. 
     * Avoided in editpage.php: PageEditor->getContent
     */
    function ConvertAfter($text) {
        return TransformInline($text);
    }
}

class WikiToHtml {
  function WikiToHtml ($wikitext, &$request) {
        $this->_wikitext = $wikitext;
	$this->_request =& $request;
	$this->_html = "";
	$this->html_content = "";
    }

    function send() {
        $this->convert();
	echo $this->html_content;
    }

    function convert() {
        require_once("lib/BlockParser.php");       
	$xmlcontent = TransformText($this->_wikitext, 2.0, $this->_request->getArg('pagename')); 
	$this->_html = $xmlcontent->AsXML();

	$this->replace_inside_html();
    }

    function replace_inside_html() {
	global $charset;

	$this->clean_links();
        $this->clean_plugin_name();
        $this->replace_known_plugins();
        $this->replace_unknown_plugins();
	// $this->replace_tags();
	$this->clean_plugin();

	if ($charset != 'utf-8') {
 	    if ($charset == 'iso-8959-1') {
 	        $this->_html = utf8_decode($this->_html);
	    } else {    
                // check for iconv support
                loadPhpExtension("iconv");
	        $this->_html = iconv("UTF-8", $charset, $this->_html);
 	    }
        }
	$this->html_content = $this->_html;
    }

    // Draft function to replace RichTable
    // by a html table
    // Works only on one plugin for the moment
    function replace_known_plugins() {
      // If match a plugin
      $pattern = '/\&lt\;\?plugin\s+RichTable(.*)\?\&gt\;/Umsi';
      $replace_string = "replace_rich_table";       
      $this->_html = preg_replace_callback($pattern,
					   $replace_string,
					   $this->_html);
    }
    
    // Replace unknown plugins by keyword Wikitext { tag }
    function replace_unknown_plugins() {
        $pattern = '/(\&lt\;\?plugin[^?]*\?\&gt\;)/Usi';
	$replace_string = 
	  '<p><div style="background-color:#D3D3D3;font-size:smaller;">Wikitext {
 <br> \1 <br>}</div><br></p>';
       
	$this->_html = preg_replace($pattern,
				    $replace_string,
				    $this->_html);
    }

    // Clean links to keep only <a href="link">name</a>
    function clean_links() {
        // Existing links
        // FIXME: use VIRTUAL_PATH
        $pattern = '/\<a href\=\"index.php\?pagename\=(\w+)\"([^>])*\>/Umsi';      
        $replace_string = '<a href="\1">';      
        $this->_html = preg_replace($pattern,
                                    $replace_string,
                                    $this->_html) ;
        // Non existing links
        $pattern = '/\<a href\=\"index.php\?pagename\=([^"]*)(&amp;action){1}([^>])*\>/Umsi';
        $replace_string = '<a href="\1">';
	
        $this->_html = preg_replace($pattern,
                                    $replace_string,
                                    $this->_html) ;

        // Clean underline 
        $pattern = '/\<u\>(.*)\<\/u\>(\<a href="(.*))[?"]{1}.*\>.*\<\/a\>/Umsi';
        $replace_string = 
            '<span>\2" style="color:blue;">\1</a></span>';
	
        $this->_html = preg_replace($pattern,
                                    $replace_string,
                                    $this->_html) ;
    }
    
    // Put unknown tags in Wikitext {}
    function replace_tags() {
        // Replace old table format ( non plugin )
        $pattern = '/(\ {0,4}(?:\S.*)?\|\S+\s*$.*?\<\/p\>)/ms';
        $replace_string = 
            '<p><div style="background-color:#D3D3D3;font-size:smaller;">Wikitext {
 <br> \1 <br>}</div><br></p>';
      
        $this->_html = preg_replace($pattern,
                                    $replace_string,
                                    $this->_html);
}
    
    // Replace \n by <br> only in 
    // <?plugin ? > tag to keep formatting
    function clean_plugin() {
        $pattern = '/(\&lt\;\?plugin.*\?\&gt\;)/Umsei';
	$replace_string = 'preg_replace("/\n/Ums","<br>","\1")';
	
	$this->_html = preg_replace($pattern,
				    $replace_string,
				    $this->_html) ; 

    }

    function clean_plugin_name() {
	// Remove plugin name converted in a link
	$pattern = '/(\&lt\;\?plugin\s)\<span.*\>\<span\>\<a href=.*\>(\w+)\<\/a\><\/span\><\/span>([^?]*\?\&gt\;)/Umsi';
 	$replace_string = '\1 \2 \3';
 	$this->_html = preg_replace($pattern,
 				    $replace_string,
 				    $this->_html) ; 
    } 
}

// This is called to replace the RichTable plugin by an html table
// $matched contains html <p> tags so 
// they are deleted before the conversion.
function replace_rich_table($matched) {
    $plugin = $matched[1];

    $unknown_options = "/colspan|rowspan|width|height/";
  
    // if the plugin contains one of the options bellow
    // it won't be converted
    if (preg_match($unknown_options,$plugin))
        return $matched[0]."\n";   
    else {
        //Replace unused <p...>
        $pattern = '/\<p.*\>/Umsi';
        $replace_string = "";
    
        $plugin = preg_replace($pattern,
                               $replace_string,
                               $plugin) ; 
    
        //replace unused </p> by \n
        $pattern = '/\<\/p\>/Umsi';
        $replace_string = "\n";
    
        $plugin = preg_replace($pattern,
                               $replace_string,
                               $plugin) ; 
    
        $plugin = "<?plugin RichTable ".$plugin." ?>";
    
        require_once("lib/BlockParser.php"); 
        $xmlcontent = TransformText($plugin, 2.0, $GLOBALS['request']->getArg('pagename')); 
        return $xmlcontent->AsXML();
  }
}

/*
 $Log: Wikiwyg.php,v $
 Revision 1.9  2007/01/07 18:44:00  rurban
 Improve id: edit: to edit-

 Revision 1.8  2007/01/02 13:20:57  rurban
 use the new _DEBUG_REMOTE flag. simplify default utf-8 charset conversion, not requiring iconv

 Revision 1.7  2006/12/22 16:53:38  rurban
 Try to dl() load the iconv extension, if not already loaded

 Revision 1.6  2006/08/25 22:42:51  rurban
 warn user about beta quality, not to save wrong edits

 Revision 1.5  2006/06/28 14:28:14  jeannicolas
 Add preview and save button on the toolbar.
 Fix an IE issue in wikitext mode.

 Revision 1.4  2006/06/19 17:33:06  jeannicolas
 Add button to insert table of content plugin
 Add button to insert wikitext section in wysiwyg mode

 Fix internet explorer issue in wikitext mode. The toolbar in this mode didn't work.

 Revision 1.3  2006/05/31 19:59:57  jeannicolas
 Added wysiwyg_editor 1.1b

 Revision 1.2  2006/05/14 17:52:20  rurban
 fix syntax error. delete a left-over attempt to add CSS links also. 
 We did put everything into phpwiki.css for browser compatibility.

 Revision 1.1  2006/05/13 19:59:55  rurban
 added wysiwyg_editor-1.3a feature by Jean-Nicolas GEREONE <jean-nicolas.gereone@st.com>
 converted wysiwyg_editor-1.3a js to WysiwygEdit framework
 changed default ENABLE_WYSIWYG = true and added WYSIWYG_BACKEND = Wikiwyg


*/

// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>