icon.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. switch ($this->getFlag('pack')) {
  66. case 'fugue':
  67. case 'oxygen':
  68. $path = $this->makeIconPath($icon);
  69. $renderer->doc .= sprintf('<img src="%s" title="%s" class="%s" style="%s" />',
  70. $path, $title,
  71. trim(implode(' ', $this->getClasses()), ' '),
  72. trim(implode(';', $this->getStyles()), ';'));
  73. return true;
  74. }
  75. $this->classes[] = $this->getFlag('pack');
  76. $this->classes[] = $this->getFlag('pack') . '-' . $icon;
  77. $renderer->doc .= sprintf('<i class="%s" style="%s" title="%s"></i>',
  78. trim(implode(' ', $this->getClasses()), ' '),
  79. trim(implode(';', $this->getStyles()), ';'),
  80. $title);
  81. return true;
  82. }
  83. return false;
  84. }
  85. protected function getFlag($name) {
  86. return (isset($this->flags[$name]) ? $this->flags[$name] : null);
  87. }
  88. protected function getFlags() {
  89. return $this->flags;
  90. }
  91. protected function parseFlags($pack, $icon, $flags) {
  92. $this->flags = array();
  93. $this->classes = array();
  94. $this->styles = array();
  95. if ((int) $flags[0] > 0) {
  96. $flags[] = "size=" . $flags[0];
  97. unset($flags[0]);
  98. }
  99. $this->flags['pack'] = $pack;
  100. $this->flags['icon'] = $icon;
  101. foreach ($flags as $flag) {
  102. list($flag, $value) = explode('=', $flag);
  103. switch ($flag) {
  104. case 'pack':
  105. $this->flags['pack'] = $value;
  106. break;
  107. case 'size':
  108. $this->flags['size'] = (int) $value;
  109. $this->styles[] = "font-size:{$value}px";
  110. break;
  111. case 'circle':
  112. $this->flags['circle'] = true;
  113. $this->styles[] = 'border-radius:50%; -moz-border-radius:50%; -webkit-border-radius:50%';
  114. break;
  115. case 'padding':
  116. $this->flags['padding'] = $value;
  117. $this->styles[] = "padding:$value";
  118. break;
  119. case 'background':
  120. $this->flags['background'] = $value;
  121. $this->styles[] = "background-color:$value";
  122. break;
  123. case 'color':
  124. $this->flags['color'] = $value;
  125. $this->styles[] = "color: $value";
  126. break;
  127. case 'class':
  128. $this->flags['class'] = $value;
  129. $this->classes[] = $value;
  130. break;
  131. case 'align':
  132. $this->flags['align'] = $value;
  133. if ($value !== 'center') {
  134. $margin = ($value == 'left') ? 'right' : 'left';
  135. $this->styles[] = "float:$value; margin-$margin: .1em";
  136. } else {
  137. $this->styles[] = "display:block; text-align:center; margin:0 auto";
  138. }
  139. break;
  140. default:
  141. $this->classes[] = $flag;
  142. }
  143. }
  144. if (! isset($this->flags['size'])) {
  145. $this->styles[] = "font-size:" . $this->getConf('defaultSize') . "px";
  146. }
  147. if ($this->flags['pack'] == 'icon') {
  148. $this->flags['pack'] = $this->getConf('defaultPack');
  149. }
  150. }
  151. protected function getStyles() {
  152. return $this->styles;
  153. }
  154. protected function getClasses() {
  155. return $this->classes;
  156. }
  157. protected function makeIconPath($icon) {
  158. switch ($this->getFlag('pack')) {
  159. case 'fugue' : return $this->makeFuguePath($icon);
  160. case 'oxygen' : return $this->makeOxygenPath($icon);
  161. }
  162. }
  163. protected function makeFuguePath($icon) {
  164. $repo = 'https://raw.githubusercontent.com/yusukekamiyamane/fugue-icons/master';
  165. $sizes = array(16, 24, 32);
  166. $size = (($this->getFlag('size') > max($sizes)) ? max($sizes) : $this->getFlag('size'));
  167. switch ($size) {
  168. case 0:
  169. case 16:
  170. $size = 'icons'; break;
  171. case 24:
  172. $size = 'bonus/icons-24'; break;
  173. case 32:
  174. $size = 'bonus/icons-32'; break;
  175. default:
  176. $size = 'icons';
  177. }
  178. return "$repo/$size/$icon.png";
  179. }
  180. protected function makeOxygenPath($icon) {
  181. $repo = 'https://raw.githubusercontent.com/pasnox/oxygen-icons-png/master/oxygen';
  182. $size = $this->getFlag('size').'x'.$this->getFlag('size');
  183. return "$repo/$size/$icon.png";
  184. }
  185. }