1<?php
2/**
3 * DokuWiki Plugin konsole (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Fabrice DEJAIGHER <fabrice@chtiland.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC'))
11{
12	die();
13}
14
15if (!defined('DOKU_LF'))
16{
17	define('DOKU_LF', "\n");
18}
19if (!defined('DOKU_TAB'))
20{
21	define('DOKU_TAB', "\t");
22}
23if (!defined('DOKU_PLUGIN'))
24{
25	define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
26}
27
28require_once DOKU_PLUGIN.'syntax.php';
29
30class syntax_plugin_konsole extends DokuWiki_Syntax_Plugin
31{
32
33    var $types_user = array(
34	'user' => 'konsoleuser',
35	'root' => 'konsoleroot',
36	'admin' => 'konsoleroot'
37    );
38
39    var $type_defaut = 'konsoleuser';
40
41    var $motsclefs = array('alias','apropos','awk','basename','bash','bc','bg','builtin','bzip2','cal','cat','cd','cfdisk','chgrp','chmod','chown','chroot',
42			'cksum','clear','cmp','comm','command','cp','cron','crontab','csplit','cut','date','dc','dd','ddrescue','declare','df',
43			'diff','diff3','dig','dir','dircolors','dirname','dirs','du','echo','egrep','eject','enable','env','ethtool','eval',
44			'exec','exit','expand','export','expr','false','fdformat','fdisk','fg','fgrep','file','find','fmt','fold','format',
45			'free','fsck','ftp','gawk','getopts','grep','groups','gzip','hash','head','history','hostname','id','ifconfig',
46			'import','install','join','kill','less','let','ln','local','locate','logname','logout','look','lpc','lpr','lprint',
47			'lprintd','lprintq','lprm','ls','lsof','make','man','mkdir','mkfifo','mkisofs','mknod','more','mount','mtools',
48			'mv','netstat','nice','nl','nohup','nslookup','open','op','passwd','paste','pathchk','ping','popd','pr','printcap',
49			'printenv','printf','ps','pushd','pwd','quota','quotacheck','quotactl','ram','rcp','read','readonly','renice',
50			'remsync','rm','rmdir','rsync','screen','scp','sdiff','sed','select','seq','set','sftp','shift','shopt','shutdown',
51			'sleep','sort','source','split','ssh','strace','su','sudo','sum','symlink','sync','tail','tar','tee','test','time',
52			'times','touch','top','traceroute','trap','tr','true','tsort','tty','type','ulimit','umask','umount','unalias',
53			'uname','unexpand','uniq','units','unset','unshar','useradd','usermod','users','uuencode','uudecode','v','vdir',
54			'vi','watch','wc','whereis','which','who','whoami','Wget','xargs','yes');
55
56    public function getType()
57	{
58        return 'container';
59    }
60
61    public function getPType()
62	{
63        return 'normal';
64    }
65    public function getAllowedTypes()
66	{
67        return array('container','substition','protected','disabled','formatting','paragraphs');
68    }
69
70    public function getSort()
71	{
72        return 195;
73    }
74
75
76    public function connectTo($mode)
77	{
78        $this->Lexer->addEntryPattern('<konsole.*?>(?=.*?</konsole>)',$mode,'plugin_konsole');
79    }
80
81    public function postConnect()
82	{
83        $this->Lexer->addExitPattern('</konsole>','plugin_konsole');
84    }
85
86    public function handle($match, $state, $pos, Doku_Handler $handler)
87	{
88		switch ($state)
89		{
90			case DOKU_LEXER_ENTER : // Type
91				$type_user = strtolower(trim(substr($match,8,-1)));
92
93				foreach ($this->types_user as $type_possible => $classe)
94				{
95					if($type_user == $type_possible)
96					{
97						$type_user = strtoupper($type_user);
98						$retour = $classe.','.$type_user;
99						return array($state,$retour);
100					}
101				}
102				return array($state,$this->type_defaut);
103
104
105
106			case DOKU_LEXER_UNMATCHED :	// Contenu du terminal
107				$divreturn = $match;
108
109				foreach ($this->motsclefs as $motclef)
110				{
111					$motclef_hi = '<b>'.$motclef.'</b>';
112					$divreturn = str_replace(" $motclef ", " $motclef_hi ", $divreturn);
113					$divreturn = preg_replace("/\s$motclef$/", " $motclef_hi", $divreturn);
114				}
115
116
117				return array($state,$divreturn);
118
119
120
121			default : // Sinon
122				return array($state);
123
124
125		}
126
127
128    }
129
130    public function render($mode, Doku_Renderer $renderer, $indata)
131	{
132		list($state, $data) = $indata;
133
134		if($mode == 'xhtml')
135		{
136			switch ($state)
137			{
138				case DOKU_LEXER_ENTER :
139					$arr_retour = explode(',',$data);
140					$nom_user = $arr_retour[1];
141							if(empty($nom_user))
142							{
143								$nom_user='USER';
144							}
145					$classe = $arr_retour[0];
146					$renderer->doc.= '<div class="konsole">';
147					$renderer->doc.= '<div class="konsole_top_left"></div><div class="konsole_top">'.$nom_user.'</div><div class="konsole_top_right"></div>';
148					$renderer->doc .= '<div class="konsole_left"></div><div class="'.$classe.'">';
149				break;
150
151				case DOKU_LEXER_UNMATCHED :
152					$renderer->doc .= $data;
153				break;
154
155				case DOKU_LEXER_EXIT :
156
157					$renderer->doc .= '</div>'; // KonsoleUSER/ROOT
158					$renderer->doc.='<div class="konsole_right"></div>';
159					$renderer->doc.= '<div class="konsole_bottom_left"></div><div class="konsole_bottom"></div><div class="konsole_bottom_right"></div>';
160					$renderer->doc.='</div>'; // konsole
161
162				break;
163			}
164
165			return true;
166		}
167		else
168		{
169			return false;
170		}
171
172
173    }
174}
175