icon.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * Plugin Icons for DokuWiki
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Giuseppe Di Terlizzi <giuseppe.diterlizzi>
  7. * @copyright (C) 2015, Giuseppe Di Terlizzi
  8. */
  9. // must be run within Dokuwiki
  10. if(!defined('DOKU_INC')) die();
  11. class syntax_plugin_icons_icon extends DokuWiki_Syntax_Plugin {
  12. protected $pattern = '{{icon>.+?}}';
  13. protected $flags = array();
  14. protected $classes = array();
  15. protected $styles = array();
  16. /**
  17. * Syntax Type
  18. *
  19. * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
  20. *
  21. * @return string
  22. */
  23. public function getType() { return 'substition'; }
  24. /**
  25. * Sort for applying this mode
  26. *
  27. * @return int
  28. */
  29. public function getSort() { return 32; }
  30. /**
  31. * @param string $mode
  32. */
  33. public function connectTo($mode) {
  34. $this->Lexer->addSpecialPattern($this->pattern, $mode, 'plugin_icons_'.$this->getPluginComponent());
  35. }
  36. /**
  37. * Handler to prepare matched data for the rendering process
  38. *
  39. * @param string $match The text matched by the patterns
  40. * @param int $state The lexer state for the match
  41. * @param int $pos The character position of the matched text
  42. * @param Doku_Handler $handler The Doku_Handler object
  43. * @return bool|array Return an array with all data you want to use in render, false don't add an instruction
  44. */
  45. public function handle($match, $state, $pos, Doku_Handler $handler) {
  46. $match = substr($match, 2, -2); // strip markup
  47. list($match, $title, $url) = explode('|', $match);
  48. list($match, $flags) = explode('?', $match, 2);
  49. list($pack, $icon) = explode('>', $match, 2);
  50. return array($pack, $icon, explode('&', $flags), $title, $url, $align, $match, $state, $pos);
  51. }
  52. /**
  53. * Handles the actual output creation.
  54. *
  55. * @param string $mode output format being rendered
  56. * @param Doku_Renderer $renderer the current renderer object
  57. * @param array $data data created by handler()
  58. * @return boolean rendered correctly? (however, returned value is not used at the moment)
  59. */
  60. public function render($mode, Doku_Renderer $renderer, $data) {
  61. if ($mode == 'xhtml') {
  62. /** @var Doku_Renderer_xhtml $renderer */
  63. list($pack, $icon, $flags, $title, $url) = $data;
  64. $this->parseFlags($pack, $icon, $flags);
  65. $class_icon = 'syntax_plugin_icons_'.$this->getFlag('pack');
  66. if (constant("$class_icon::IS_ICON")) {
  67. unset($this->styles['font-size']);
  68. $size = $this->getFlag('size');
  69. $base_path = rtrim($this->getConf("{$pack}URL"), '/');
  70. $path = call_user_func_array(array($class_icon, 'makePath'), array($icon, $size, $base_path));
  71. if (isset($url)) {
  72. $renderer->doc .= sprintf('<a href="%s" rel="nofollow">', $url);
  73. }
  74. $renderer->doc .= sprintf('<img src="%s" title="%s" class="%s" style="%s" />',
  75. $path, $title,
  76. $this->toClassString($this->getClasses()),
  77. $this->toInlineStyle($this->getStyles()));
  78. if (isset($url)) {
  79. $renderer->doc .= sprintf('</a>');
  80. }
  81. return true;
  82. }
  83. $this->classes[] = $this->getFlag('pack');
  84. $this->classes[] = $this->getFlag('pack') . '-' . $icon;
  85. if (isset($url)) {
  86. $renderer->doc .= sprintf('<a href="%s" rel="nofollow">', $url);
  87. }
  88. $renderer->doc .= sprintf('<i class="%s" style="%s" title="%s"></i>',
  89. $this->toClassString($this->getClasses()),
  90. $this->toInlineStyle($this->getStyles()),
  91. $title);
  92. if (isset($url)) {
  93. $renderer->doc .= sprintf('</a>');
  94. }
  95. return true;
  96. }
  97. return false;
  98. }
  99. protected function toClassString($things) {
  100. return trim(implode(' ', $things), ' ');
  101. }
  102. protected function toInlineStyle($things) {
  103. $result = '';
  104. foreach ($things as $property => $value) {
  105. $result .= "$property:$value;";
  106. }
  107. $result = trim($result, ';');
  108. return $result;
  109. }
  110. protected function getFlag($name) {
  111. return (isset($this->flags[$name]) ? $this->flags[$name] : null);
  112. }
  113. protected function getFlags() {
  114. return $this->flags;
  115. }
  116. protected function parseFlags($pack, $icon, $flags) {
  117. $this->flags = array();
  118. $this->classes = array();
  119. $this->styles = array();
  120. $this->flags['pack'] = $pack;
  121. $this->flags['icon'] = $icon;
  122. if ((int) $flags[0] > 0) {
  123. $flags[] = "size=" . $flags[0];
  124. unset($flags[0]);
  125. }
  126. if ($left = array_search('left', $flags)) {
  127. $flags[] = 'align=left';
  128. unset($flags[$left]);
  129. }
  130. if ($right = array_search('right', $flags)) {
  131. $flags[] = 'align=right';
  132. unset($flags[$right]);
  133. }
  134. if ($center = array_search('center', $flags)) {
  135. $flags[] = 'align=center';
  136. unset($flags[$center]);
  137. }
  138. foreach ($flags as $flag) {
  139. list($flag, $value) = explode('=', $flag);
  140. switch ($flag) {
  141. case 'pack':
  142. $this->flags[$flag] = $value;
  143. break;
  144. case 'size':
  145. $this->flags[$flag] = (int) $value;
  146. $this->styles['font-size'] = "{$value}px";
  147. break;
  148. case 'circle':
  149. $this->flags[$flag] = true;
  150. $this->styles['border-radius'] = '50%';
  151. $this->styles['-moz-border-radius'] = '50%';
  152. $this->styles['-webkit-border-radius'] = '50%';
  153. break;
  154. case 'border':
  155. $this->flags[$flag] = true;
  156. $this->styles['border'] = '0.08em solid #EEE';
  157. break;
  158. case 'borderColor':
  159. $this->flags[$flag] = $value;
  160. $this->styles['border-color'] = $value;
  161. break;
  162. case 'padding':
  163. $this->flags[$flag] = $value;
  164. $this->styles['padding'] = $value;
  165. break;
  166. case 'background':
  167. $this->flags[$flag] = $value;
  168. $this->styles['background-color'] = $value;
  169. break;
  170. case 'color':
  171. $this->flags[$flag] = $value;
  172. $this->styles['color'] = $value;
  173. break;
  174. case 'class':
  175. $this->flags[$flag] = $value;
  176. $this->classes[] = $value;
  177. break;
  178. case 'align':
  179. $this->flags[$flag] = $value;
  180. $class_icon = 'syntax_plugin_icons_'.$this->getFlag('pack');
  181. if (constant("$class_icon::IS_ICON")) {
  182. $this->classes[] = "media$value";
  183. } else {
  184. if ($value == 'center') {
  185. $this->styles['text-align'] = 'center';
  186. } else {
  187. $this->styles['padding-'.(($value == 'left') ? 'right' : 'left')] = '.2em';
  188. $this->styles['float'] = $value;
  189. }
  190. }
  191. break;
  192. default:
  193. $this->classes[] = $flag;
  194. }
  195. }
  196. if (! isset($this->flags['size'])) {
  197. $this->flags['size'] = $this->getConf('defaultSize');
  198. $this->styles['font-size'] = $this->getConf('defaultSize') . "px";
  199. }
  200. if ($this->flags['pack'] == 'icon') {
  201. $this->flags['pack'] = $this->getConf('defaultPack');
  202. }
  203. }
  204. protected function getStyles() {
  205. return $this->styles;
  206. }
  207. protected function getClasses() {
  208. return $this->classes;
  209. }
  210. }