1<?php 2/** 3 * DokuPrism Plugin - Code highlighter using [prismjs.com] library 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Adam Mnemnonic <adam85mn@gmail.com> 7 */ 8if (!defined('DOKU_INC')) die(); 9 10 11/** 12 * All DokuWiki plugins to extend the parser/rendering mechanism need to inherit from this class 13 */ 14class syntax_plugin_dokuprism_code extends DokuWiki_Syntax_Plugin { 15 16 function getType() { 17 return 'protected'; 18 } 19 20 function getSort() { 21 return 199; // <-- native 'code'/'file' sort=200 22 } 23 24 public function preConnect() { 25 $this->mode = substr(get_class($this), 7); // syntax mode, drop 'syntax_' from class name 26 27 $this->pattern[1] = '<file\b.*?>(?=.*?</file>)'; 28 $this->pattern[2] = '</file>'; 29 30 $this->pattern[11] = '<code\b.*?>(?=.*?</code>)'; 31 $this->pattern[12] = '</code>'; 32 } 33 34 public function connectTo($mode) 35 { 36 if ($this->getConf('override_file')) $this->Lexer->addEntryPattern($this->pattern[1], $mode, $this->mode); 37 if ($this->getConf('override_code')) $this->Lexer->addEntryPattern($this->pattern[11], $mode, $this->mode); 38 } 39 40 public function postConnect() 41 { 42 if ($this->getConf('override_file')) $this->Lexer->addExitPattern($this->pattern[2], $this->mode); 43 if ($this->getConf('override_code')) $this->Lexer->addExitPattern($this->pattern[12], $this->mode); 44 } 45 46 47 function handle($match, $state, $pos, Doku_Handler $handler){ 48 switch ($state) { 49 case DOKU_LEXER_ENTER: 50 $match = trim(substr($match, 5, -1)); //5 = strlen("<code") 51 break; 52 } 53 return array($match, $state, $pos); 54 } 55 56 57 58 59 function render($format, Doku_Renderer $renderer, $data) { 60 list($match, $state, $pos) = $data; 61 if($format == 'xhtml'){ 62 switch ($state) { 63 case DOKU_LEXER_ENTER: 64 $language = $renderer->_xmlEntities($match); 65 $renderer->doc .= '<pre class="plain"><code class="language-'.$language.'">'; 66 break; 67 case DOKU_LEXER_UNMATCHED: 68 $renderer->doc .= trim($renderer->_xmlEntities($match)); 69 break; 70 case DOKU_LEXER_EXIT: 71 $renderer->doc .= '</code></pre>'; 72 break; 73 case DOKU_LEXER_MATCHED: 74 case DOKU_LEXER_SPECIAL: 75 break; 76 } 77 return true; 78 } 79 return false; 80 } 81} 82?> 83