icon.php 8.8 KB

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