1<?php
2/**
3 * Syntax editor highlight plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Emanuele <emanuele45@interfree.it>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'action.php');
14
15class action_plugin_codehighlight extends DokuWiki_Action_Plugin {
16
17    /**
18     * return some info
19     */
20    function getInfo(){
21      return array(
22        'author' => 'Emanuele',
23        'email'  => 'emanuele45@interfree.it',
24        'date'   => '2009-02-21',
25        'name'   => 'CodeHighlight',
26        'desc'   => 'Highlight code (php, javascript, and so on) syntax in DokuWiki editor',
27        'url'    => 'http://lacroa.altervista.org/dokucount/',
28      );
29    }
30
31    /**
32     * register the eventhandlers
33     */
34    function register(&$controller){
35
36		$controller->register_hook('TPL_METAHEADER_OUTPUT',
37                                   'BEFORE',
38                                   $this,
39                                   'codepress_script_file');
40
41        // old hook
42        $controller->register_hook('HTML_EDITFORM_INJECTION',
43                                   'AFTER',
44                                   $this,
45                                   'handle_editform_output',
46                                   array('editform' => true, 'oldhook' => true));
47
48        // new hook
49        $controller->register_hook('HTML_EDITFORM_OUTPUT',
50                                   'AFTER',
51                                   $this,
52                                   'handle_editform_output',
53                                   array('editform' => true, 'oldhook' => false));
54    }
55
56    /**
57     * Javascript to handle the editor
58     */
59	function codepress_script_file(&$event, $param) {
60		global $lang;
61		$event->data["script"][] = array (
62		  "type" => "text/javascript",
63		  "src" => "lib/plugins/codehighlight/cp/codepress.js",
64		  "charset" => "utf-8",
65		  "_data" => "<!-- -->",
66		);
67		$event->data["script"][] = array (
68		  "type" => "text/javascript",
69		  "charset" => "utf-8",
70		  "_data" => "
71		  function startEditor(){
72
73		  $('edbtn__save').style.display=\"none\";
74		  $('edbtn__preview').style.display=\"none\";
75		  $('hlc_languages').style.display = \"\";
76		  if($('repl_edbtn__preview') != null){
77			  $('repl_edbtn__save').style.display = \"\"
78			  $('repl_edbtn__preview').style.display = \"\"
79			  wiki__text.toggleEditor();
80		  } else {
81			  $('wiki__text').className += ' codepress php linenumbers-off';
82			  CodePress.run();
83			  $('edbtn__save').parentNode.innerHTML = '<button name=\"do[preview]\" value=\"".$lang['btn_preview']."\" class=\"button\" id=\"repl_edbtn__preview\" accesskey=\"p\" tabindex=\"5\" title=\"".$lang['btn_preview']." [ALT+P]\">".$lang['btn_preview']."</button>' + $('edbtn__save').parentNode.innerHTML;
84			  $('edbtn__save').parentNode.innerHTML = '<button name=\"do[save]\" value=\"".$lang['btn_save']."\" class=\"button\" id=\"repl_edbtn__save\" accesskey=\"s\" tabindex=\"4\" title=\"".$lang['btn_save']." [ALT+S]\">".$lang['btn_save']."</button> ' + $('edbtn__save').parentNode.innerHTML;
85			  $('repl_edbtn__preview').onclick = function () {
86			  wiki__text.toggleEditor();
87			  $('dw__editform').submit();
88			  };
89			  $('repl_edbtn__save').onclick = function () {
90			  wiki__text.toggleEditor();
91			  $('dw__editform').submit();
92			  };
93		  }
94		  $('hlc_toggle').innerHTML = '".$this->getLang('chl_off_highl')."'
95		  $('hlc_toggle').onclick = function(){stopEditor();};
96		  }
97		  function stopEditor(){
98		  wiki__text.toggleEditor();
99		  $('hlc_languages').style.display = \"none\";
100		  $('edbtn__save').style.display=\"\";
101		  $('edbtn__preview').style.display=\"\";
102		  $('repl_edbtn__save').style.display = \"none\"
103		  $('repl_edbtn__preview').style.display = \"none\"
104		  $('hlc_toggle').innerHTML = 'Highlight code'
105		  $('hlc_toggle').onclick = function(){startEditor();};
106		  }
107		  ",
108		);
109		$event->data["style"][] = array (
110			"type" => "text/css",
111			"_data" => ".hidden-code {display:none;}",
112		);
113	}
114
115
116    /**
117     * HTML code inserted after the editor (toggle editor and codepress controls)
118     */
119    function handle_editform_output(&$event, $param){
120		$out = '
121		<div id="cpeditor">
122		<button id="hlc_toggle" onclick="startEditor()">'.$this->getLang('chl_highl_code').'</button>
123<div style="display:none" id="hlc_languages">
124	<em>'.$this->getLang('chl_choose_lang').':</em>
125	<button onclick="wiki__text.edit(\'\',\'php\')">PHP</button>
126	<button onclick="wiki__text.edit(\'\',\'javascript\')">JavaScript</button>
127	<button onclick="wiki__text.edit(\'\',\'java\')">Java</button>
128	<button onclick="wiki__text.edit(\'\',\'perl\')">Perl</button>
129	<button onclick="wiki__text.edit(\'\',\'sql\')">SQL</button>
130	<button onclick="wiki__text.edit(\'\',\'html\')">HTML</button>
131	<button onclick="wiki__text.edit(\'\',\'css\')">CSS</button>
132	<button onclick="wiki__text.toggleLineNumbers()">'.$this->getLang('chl_toggle_ln').'</button>
133	<button onclick="wiki__text.toggleAutoComplete()">'.$this->getLang('chl_toggle_ac').'</button>
134</div></div>';
135		echo $out;
136
137    }
138}
139
140