icon.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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@gmail.com>
  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 299; }
  30. /**
  31. * @param string $mode
  32. */
  33. public function connectTo($mode) {
  34. $this->Lexer->addSpecialPattern($this->pattern, $mode, 'plugin_icons_'.$this->getPluginComponent());
  35. $this->Lexer->addSpecialPattern('\[\[[^\}]+\|'.$this->pattern.'\]\]', $mode, 'plugin_icons_'.$this->getPluginComponent());
  36. }
  37. /**
  38. * Handler to prepare matched data for the rendering process
  39. *
  40. * @param string $match The text matched by the patterns
  41. * @param int $state The lexer state for the match
  42. * @param int $pos The character position of the matched text
  43. * @param Doku_Handler $handler The Doku_Handler object
  44. * @return bool|array Return an array with all data you want to use in render, false don't add an instruction
  45. */
  46. public function handle($match, $state, $pos, Doku_Handler $handler) {
  47. $url = null;
  48. $flags = array();
  49. $title = null;
  50. $pack = null;
  51. $icon = null;
  52. $match = substr($match, 2, -2); // strip markup
  53. @list($match, $title, $title2) = explode('|', $match);
  54. if (isset($title2)) $title .= '}}';
  55. if (isset($title) && preg_match('/'.$this->pattern.'/', $title)) {
  56. $url = $match;
  57. $match = $title;
  58. $match = substr($match, 2, -2); // strip markup
  59. list($match, $title) = explode('|', $match);
  60. if (isset($title2)) $title = rtrim($title2, '}');
  61. }
  62. list($match, $flags) = explode('?', $match, 2);
  63. list($pack, $icon) = explode('>', $match, 2);
  64. return array($pack, $icon, explode('&', $flags), $title, $url, $match, $state, $pos);
  65. }
  66. /**
  67. * Handles the actual output creation.
  68. *
  69. * @param string $mode output format being rendered
  70. * @param Doku_Renderer $renderer the current renderer object
  71. * @param array $data data created by handler()
  72. * @return boolean rendered correctly? (however, returned value is not used at the moment)
  73. */
  74. public function render($mode, Doku_Renderer $renderer, $data) {
  75. if ($mode == 'xhtml') {
  76. /** @var Doku_Renderer_xhtml $renderer */
  77. list($pack, $icon, $flags, $title, $url) = $data;
  78. $this->parseFlags($pack, $icon, $flags);
  79. $class_icon = 'syntax_plugin_icons_'.$this->getFlag('pack');
  80. if (constant("$class_icon::IS_ICON")) {
  81. unset($this->styles['font-size']);
  82. $size = $this->getFlag('size');
  83. $base_path = rtrim($this->getConf("{$pack}URL"), '/');
  84. $path = call_user_func_array(array($class_icon, 'makePath'), array($icon, $size, $base_path));
  85. $icon_markup = sprintf('<img src="%s" title="%s" class="%s" style="%s" />',
  86. $path, $title,
  87. $this->toClassString($this->getClasses()),
  88. $this->toInlineStyle($this->getStyles()));
  89. } else {
  90. $this->classes[] = $this->getFlag('pack');
  91. $this->classes[] = $this->getFlag('pack') . '-' . $icon;
  92. $icon_markup = sprintf('<i class="%s" style="%s" title="%s"></i>',
  93. $this->toClassString($this->getClasses()),
  94. $this->toInlineStyle($this->getStyles()),
  95. $title);
  96. }
  97. if (isset($url)) {
  98. global $conf;
  99. global $ID;
  100. resolve_pageid(getNS($ID), $url, $exists, $this->date_at, true);
  101. $link['target'] = $conf['target']['wiki'];
  102. $link['style'] = '';
  103. $link['pre'] = '';
  104. $link['suf'] = '';
  105. $link['more'] = '';
  106. $link['class'] = '';
  107. $link['url'] = wl($url);
  108. $link['name'] = $icon_markup;
  109. if ($exists) {
  110. $link['class'] = 'wikilink1';
  111. } else {
  112. $link['class'] = 'wikilink2';
  113. $link['rel'] = 'nofollow';
  114. }
  115. $renderer->doc .= $renderer->_formatLink($link);
  116. } else {
  117. $renderer->doc .= $icon_markup;
  118. }
  119. return true;
  120. }
  121. return false;
  122. }
  123. protected function toClassString($things) {
  124. return trim(implode(' ', $things), ' ');
  125. }
  126. protected function toInlineStyle($things) {
  127. $result = '';
  128. foreach ($things as $property => $value) {
  129. $result .= "$property:$value;";
  130. }
  131. $result = trim($result, ';');
  132. return $result;
  133. }
  134. protected function getFlag($name) {
  135. return (isset($this->flags[$name]) ? $this->flags[$name] : null);
  136. }
  137. protected function getFlags() {
  138. return $this->flags;
  139. }
  140. protected function parseFlags($pack, $icon, $flags) {
  141. $this->flags = array();
  142. $this->classes = array();
  143. $this->styles = array();
  144. $this->flags['pack'] = $pack;
  145. $this->flags['icon'] = $icon;
  146. if ((int) $flags[0] > 0) {
  147. $flags[] = "size=" . $flags[0];
  148. unset($flags[0]);
  149. }
  150. if ($left = array_search('left', $flags)) {
  151. $flags[] = 'align=left';
  152. unset($flags[$left]);
  153. }
  154. if ($right = array_search('right', $flags)) {
  155. $flags[] = 'align=right';
  156. unset($flags[$right]);
  157. }
  158. if ($center = array_search('center', $flags)) {
  159. $flags[] = 'align=center';
  160. unset($flags[$center]);
  161. }
  162. foreach ($flags as $flag) {
  163. list($flag, $value) = explode('=', $flag);
  164. switch ($flag) {
  165. case 'pack':
  166. $this->flags[$flag] = $value;
  167. break;
  168. case 'size':
  169. $this->flags[$flag] = (int) $value;
  170. $this->styles['font-size'] = "{$value}px";
  171. break;
  172. case 'circle':
  173. $this->flags[$flag] = true;
  174. $this->styles['border-radius'] = '50%';
  175. $this->styles['-moz-border-radius'] = '50%';
  176. $this->styles['-webkit-border-radius'] = '50%';
  177. break;
  178. case 'border':
  179. $this->flags[$flag] = true;
  180. $this->styles['border'] = '0.08em solid #EEE';
  181. break;
  182. case 'borderColor':
  183. $this->flags[$flag] = $value;
  184. $this->styles['border-color'] = $value;
  185. break;
  186. case 'padding':
  187. $this->flags[$flag] = $value;
  188. $this->styles['padding'] = $value;
  189. break;
  190. case 'background':
  191. $this->flags[$flag] = $value;
  192. $this->styles['background-color'] = $value;
  193. break;
  194. case 'color':
  195. $this->flags[$flag] = $value;
  196. $this->styles['color'] = $value;
  197. break;
  198. case 'class':
  199. $this->flags[$flag] = $value;
  200. $this->classes[] = $value;
  201. break;
  202. case 'align':
  203. $this->flags[$flag] = $value;
  204. $class_icon = 'syntax_plugin_icons_'.$this->getFlag('pack');
  205. if (constant("$class_icon::IS_ICON")) {
  206. $this->classes[] = "media$value";
  207. } else {
  208. if ($value == 'center') {
  209. $this->styles['text-align'] = 'center';
  210. } else {
  211. $this->styles['padding-'.(($value == 'left') ? 'right' : 'left')] = '.2em';
  212. $this->styles['float'] = $value;
  213. }
  214. }
  215. break;
  216. default:
  217. $this->classes[] = $flag;
  218. }
  219. }
  220. if (! isset($this->flags['size'])) {
  221. $this->flags['size'] = $this->getConf('defaultSize');
  222. $this->styles['font-size'] = $this->getConf('defaultSize') . "px";
  223. }
  224. if ($this->flags['pack'] == 'icon') {
  225. $this->flags['pack'] = $this->getConf('defaultPack');
  226. }
  227. }
  228. protected function getStyles() {
  229. return $this->styles;
  230. }
  231. protected function getClasses() {
  232. return $this->classes;
  233. }
  234. }