1<?php 2/* 3 4Stars Plugin for DokuWiki 5Use for difficulty, rating, etc 6 7@License: GPL 2 (http://www.gnu.org/licenses/gpl.html) 8@author: Collin "Keeyai" Green - http://keeyai.com 9@url: http://keeyai.com/projects-and-releases/ 10@version: 1.1 11 12 13Original code from the Skill plugin by iDo - http://www.dokuwiki.org/plugin:skill 14 15Modified by Keeyai to include some useful changes mentioned in the comments and other functionality 16 - star limit by anon and kenc 17 - span instead of div by anon 18 - new star image instead of transparency 19 - added classes for styling purposes ( span.starspan, img.starimage, img.halfstarimage, img.emptystarimage) 20 - show half stars (number is floored to the nearest half) 21 - packaged to work with plugin manager 22 23 24Usage: [stars=num] where num is a number, eg: 5, or a ratio, eg: 5/7 25 limits the number of stars to 10 -- ratios over ten, eg: 100/500, will be reduced, eg: 2/10 26 27Examples: 28 show 2 stars: [stars=2] 29 show 3/10 stars: [stars=3/10] 30 show 4.5/5 stars: [stars=4.5/5] 31 32Note: 33 to use custom images, just replace the star.gif, halfstar.gif, and emptystar.cif files 34 35TODO: other image options? control panel? 36 37*/ 38 39 40if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 41if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 42 43if(!defined('DOKU_PATH')) define('DOKU_PATH', dirname($_SERVER['PHP_SELF'])); 44 45require_once(DOKU_PLUGIN.'syntax.php'); 46 47/** 48 * All DokuWiki plugins to extend the parser/rendering mechanism 49 * need to inherit from this class 50 */ 51class syntax_plugin_stars extends DokuWiki_Syntax_Plugin { 52 53 function getInfo() 54 { 55 return array( 56 'author' => 'Keeyai', 57 'email' => 'keeyai@keeyai.com', 58 'date' => '27/09/2008', 59 'name' => 'Stars Plugin', 60 'desc' => 'Show stars for difficulty, ratings, etc', 61 'url' => 'http://www.dokuwiki.org/plugin:stars', 62 ); 63 } // end function getInfo() 64 65 function getType(){ return 'substition'; }// spelling error is intentional to work with dokuwiki} 66 function getSort(){ return 107; } 67 68 function connectTo($mode) 69 { 70 $this->Lexer->addSpecialPattern("\[stars=\d*\.?\d*/?\d*\.?\d*\]",$mode,'plugin_stars'); 71 } // end function connectTo($mode) 72 73 function handle($match, $state, $pos, &$handler) 74 { 75 $match = substr($match,7,-1); // Strip markup 76 $match=split('/',$match); // Strip size 77 78 if (!isset($match[1])) 79 $match[1] = $match[0]; 80 81 if ($match[0]>$match[1]) 82 $match[1]=$match[0]; 83 84 if ($match[1]>10) 85 { 86 $match[0] = 10 * $match[0] / $match[1]; 87 $match[1] = 10; 88 } // end if ($match[1]>10) 89 90 return $match; 91 } // end function handle($match, $state, $pos, &$handler) 92 93 function render($mode, &$renderer, $data) 94 { 95 if($mode == 'xhtml') 96 { 97 $renderer->doc .= $this->_Stars($data); 98 return true; 99 } // end if($mode == 'xhtml') 100 return false; 101 } // end function render($mode, &$renderer, $data) 102 103 function _Stars($d) 104 { 105 $string="<span class='starspan' alt='" . $d[0] . '/' . $d[1] . " stars'>"; 106 107 // render full stars 108 for($i=1; $i<=$d[0]; $i++) 109 $string .= '<img class="starimage" src="' . DOKU_PATH .'/lib/plugins/stars/star.gif" alt="" />'; 110 111 // render half star if necessary 112 if($i-.5 <= $d[0]) 113 { 114 $string .= '<img class="halfstarimage" src="' . DOKU_PATH .'/lib/plugins/stars/halfstar.gif" alt="" />'; 115 $i+= .5; 116 } // end if($i-$d[0] > 0) 117 118 for($i;$i<=$d[1];$i++) 119 $string .= '<img class="emptystarimage" src="' . DOKU_PATH .'/lib/plugins/stars/emptystar.gif" alt="" />'; 120 121 $string .= '</span>'; 122 123 return $string; 124 } // end function _Stars($d) 125 126} // end class syntax_plugin_stars extends DokuWiki_Syntax_Plugin { 127 128//Setup VIM: ex: et ts=4 enc=utf-8 : 129?>