1<?php
2
3//	This file is part of the "jQuery.Syntax" project, and is distributed under the MIT License.
4//	See <jquery.syntax.js> for licensing details.
5//	Copyright (c) 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
6
7if(!defined('DOKU_INC')) die();
8if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
9
10require_once DOKU_PLUGIN.'syntax.php';
11
12class syntax_plugin_jquerysyntax_code extends DokuWiki_Syntax_Plugin {
13	var $syntax = "";
14
15	function getInfo() {
16		return array(
17			'author' => 'Samuel Williams',
18			'email'  => 'samuel.williams@oriontransfer.co.nz',
19			'date'   => '2012-06-01',
20			'name'   => 'jQuery.Syntax',
21			'desc'   => "Extreme Client-side Syntax Highlighting. Replaces DokuWiki's <code> handler.",
22			'url'    => 'http://www.oriontransfer.co.nz/software/jquery-syntax',
23		);
24	}
25
26	function getType() {
27		return 'protected';
28	}
29
30	function getPType() {
31		return 'block';
32	}
33
34	// must return a number lower than returned by native 'code' mode (200)
35	function getSort() {
36		return 100;
37	}
38
39	// Connect pattern to lexer
40	function connectTo($mode) {
41		$this->Lexer->addEntryPattern('<code(?=[^\r\n]*?>.*?</code>)', $mode, 'plugin_jquerysyntax_code');
42	}
43
44	function postConnect() {
45		$this->Lexer->addExitPattern('</code>', 'plugin_jquerysyntax_code');
46	}
47
48	// Process DokuWiki Source
49	function handle($match, $state, $pos, &$handler) {
50		switch ($state) {
51			case DOKU_LEXER_ENTER:
52			$this->syntax = substr($match, 1);
53			return false;
54
55			case DOKU_LEXER_UNMATCHED:
56			// will include everything from <code ... to ... </code >
57			// e.g. ... [lang] [|title] > [content]
58			list($attr, $content) = preg_split('/>/u',$match,2);
59			list($lang, $title) = preg_split('/\|/u',$attr,2);
60
61			return array($this->syntax, trim($lang), trim($title), $content);
62		}
63		return false;
64	}
65
66	// Create output HTML
67	function render($mode, &$renderer, $data) {
68		if($mode == 'xhtml') {
69			if (count($data) == 4) {
70				list($syntax, $lang, $title, $content) = $data;
71
72				if ($title)
73					$renderer->doc .= "<div class='$syntax'><p>".$renderer->_xmlEntities($title)."</p>";
74
75				if ($syntax == 'code')
76					$renderer->doc .= "<pre class=\"syntax brush-$lang\">".$renderer->_xmlEntities($content)."</pre>";
77				else
78					$renderer->file($content);
79
80				if ($title)
81					$renderer->doc .= "</div>";
82			}
83
84			return true;
85		}
86
87		return false;
88	}
89}
90