1<?php 2/** 3 * Highlight Plugin: Allows user-defined colored highlighting 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Joseph Nahmias <joe@nahmias.net> 7 * @link http://www.dokuwiki.org/plugin:highlight 8 * @version 3.1 9 */ 10 11if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 13require_once(DOKU_PLUGIN.'syntax.php'); 14 15/** 16 * All DokuWiki plugins to extend the parser/rendering mechanism 17 * need to inherit from this class 18 */ 19class syntax_plugin_highlight extends DokuWiki_Syntax_Plugin { 20 21 22 // What kind of syntax are we? 23 function getType(){ return 'formatting'; } 24 25 // What kind of syntax do we allow (optional) 26 function getAllowedTypes() { 27 return array('formatting', 'substition', 'disabled'); 28 } 29 30 // What about paragraphs? (optional) 31 function getPType(){ return 'normal'; } 32 33 // Where to sort in? 34 function getSort(){ return 90; } 35 36 37 // Connect pattern to lexer 38 function connectTo($mode) { 39 $this->Lexer->addEntryPattern('(?i)<hi(?: .+?)?>(?=.+</hi>)',$mode,'plugin_highlight'); 40 } 41 function postConnect() { 42 $this->Lexer->addExitPattern('(?i)</hi>','plugin_highlight'); 43 } 44 45 46 // Handle the match 47 function handle($match, $state, $pos, Doku_Handler $handler){ 48 switch ($state) { 49 case DOKU_LEXER_ENTER : 50 preg_match("/(?i)<hi (.+?)>/", $match, $color); // get the color 51 if ( $this->_isValid($color[1]) ) return array($state, $color[1]); 52 break; 53 case DOKU_LEXER_MATCHED : 54 break; 55 case DOKU_LEXER_UNMATCHED : 56 return array($state, $match); 57 break; 58 case DOKU_LEXER_EXIT : 59 break; 60 case DOKU_LEXER_SPECIAL : 61 break; 62 } 63 return array($state, "#ff0"); 64 } 65 66 // Create output 67 function render($mode, Doku_Renderer $renderer, $data) { 68 if($mode == 'xhtml'){ 69 list($state, $color) = $data; 70 switch ($state) { 71 case DOKU_LEXER_ENTER : 72 $renderer->doc .= "<span style=\"background-color: $color\">"; 73 break; 74 case DOKU_LEXER_MATCHED : 75 break; 76 case DOKU_LEXER_UNMATCHED : 77 $renderer->doc .= $renderer->_xmlEntities($color); 78 break; 79 case DOKU_LEXER_EXIT : 80 $renderer->doc .= "</span>"; 81 break; 82 case DOKU_LEXER_SPECIAL : 83 break; 84 } 85 return true; 86 } 87 return false; 88 } 89 90 // validate color value $c 91 // this is cut price validation - only to ensure the basic format is 92 // correct and there is nothing harmful 93 // three basic formats "colorname", "#fff[fff]", "rgb(255[%],255[%],255[%])" 94 function _isValid($c) { 95 96 $c = trim($c); 97 98 $pattern = "/ 99 (^[a-zA-Z]+$)| #colorname - not verified 100 (^\#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$)| #colorvalue 101 (^rgb\(([0-9]{1,3}%?,){2}[0-9]{1,3}%?\)$) #rgb triplet 102 /x"; 103 104 return (preg_match($pattern, $c)); 105 106 } 107} 108 109//Setup VIM: ex: et ts=4 sw=4 enc=utf-8 : 110