icon.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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, $flags) = explode('?', $match, 2);
  48. list($pack, $icon) = preg_split('/>/u', $match, 2);
  49. list($flags, $title) = explode('|', $flags);
  50. return array($pack, $icon, explode('&', $flags), $title, $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) = $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. $renderer->doc .= sprintf('<img src="%s" title="%s" class="%s" style="%s" />',
  72. $path, $title,
  73. $this->toClassString($this->getClasses()),
  74. $this->toInlineStyle($this->getStyles()));
  75. return true;
  76. }
  77. $this->classes[] = $this->getFlag('pack');
  78. $this->classes[] = $this->getFlag('pack') . '-' . $icon;
  79. $renderer->doc .= sprintf('<i class="%s" style="%s" title="%s"></i>',
  80. $this->toClassString($this->getClasses()),
  81. $this->toInlineStyle($this->getStyles()),
  82. $title);
  83. return true;
  84. }
  85. return false;
  86. }
  87. protected function toClassString($things) {
  88. return trim(implode(' ', $things), ' ');
  89. }
  90. protected function toInlineStyle($things) {
  91. $result = '';
  92. foreach ($things as $property => $value) {
  93. $result .= "$property:$value;";
  94. }
  95. $result = trim($result, ';');
  96. return $result;
  97. }
  98. protected function getFlag($name) {
  99. return (isset($this->flags[$name]) ? $this->flags[$name] : null);
  100. }
  101. protected function getFlags() {
  102. return $this->flags;
  103. }
  104. protected function parseFlags($pack, $icon, $flags) {
  105. $this->flags = array();
  106. $this->classes = array();
  107. $this->styles = array();
  108. $this->flags['pack'] = $pack;
  109. $this->flags['icon'] = $icon;
  110. if ((int) $flags[0] > 0) {
  111. $flags[] = "size=" . $flags[0];
  112. unset($flags[0]);
  113. }
  114. if ($left = array_search('left', $flags)) {
  115. $flags[] = 'align=left';
  116. unset($flags[$left]);
  117. }
  118. if ($right = array_search('right', $flags)) {
  119. $flags[] = 'align=right';
  120. unset($flags[$right]);
  121. }
  122. if ($center = array_search('center', $flags)) {
  123. $flags[] = 'align=center';
  124. unset($flags[$center]);
  125. }
  126. foreach ($flags as $flag) {
  127. list($flag, $value) = explode('=', $flag);
  128. switch ($flag) {
  129. case 'pack':
  130. $this->flags[$flag] = $value;
  131. break;
  132. case 'size':
  133. $this->flags[$flag] = (int) $value;
  134. $this->styles['font-size'] = "{$value}px";
  135. break;
  136. case 'circle':
  137. $this->flags[$flag] = true;
  138. $this->styles['border-radius'] = '50%';
  139. $this->styles['-moz-border-radius'] = '50%';
  140. $this->styles['-webkit-border-radius'] = '50%';
  141. break;
  142. case 'border':
  143. $this->flags[$flag] = true;
  144. $this->styles['border'] = '0.08em solid #EEE';
  145. break;
  146. case 'borderColor':
  147. $this->flags[$flag] = $value;
  148. $this->styles['border-color'] = $value;
  149. break;
  150. case 'padding':
  151. $this->flags[$flag] = $value;
  152. $this->styles['padding'] = $value;
  153. break;
  154. case 'background':
  155. $this->flags[$flag] = $value;
  156. $this->styles['background-color'] = $value;
  157. break;
  158. case 'color':
  159. $this->flags[$flag] = $value;
  160. $this->styles['color'] = $value;
  161. break;
  162. case 'class':
  163. $this->flags[$flag] = $value;
  164. $this->classes[] = $value;
  165. break;
  166. case 'align':
  167. $this->flags[$flag] = $value;
  168. $this->classes[] = "media$value";
  169. break;
  170. default:
  171. $this->classes[] = $flag;
  172. }
  173. }
  174. if (! isset($this->flags['size'])) {
  175. $this->flags['size'] = $this->getConf('defaultSize');
  176. $this->styles['font-size'] = $this->getConf('defaultSize') . "px";
  177. }
  178. if ($this->flags['pack'] == 'icon') {
  179. $this->flags['pack'] = $this->getConf('defaultPack');
  180. }
  181. }
  182. protected function getStyles() {
  183. return $this->styles;
  184. }
  185. protected function getClasses() {
  186. return $this->classes;
  187. }
  188. }