icon.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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-2016, 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. const IS_ICON = null;
  13. const IS_FONT_ICON = null;
  14. protected $pattern = '{{icon>.+?}}';
  15. protected $linkPattern = '\[\[[^\]\r\n]*\|%s\]\]';
  16. protected $flags = array();
  17. protected $classes = array();
  18. protected $styles = array();
  19. /**
  20. * Syntax Type
  21. *
  22. * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
  23. *
  24. * @return string
  25. */
  26. public function getType() { return 'substition'; }
  27. /**
  28. * Sort for applying this mode
  29. *
  30. * @return int
  31. */
  32. public function getSort() { return 299; }
  33. /**
  34. * @param string $mode
  35. */
  36. public function connectTo($mode) {
  37. $this->Lexer->addSpecialPattern($this->pattern, $mode, 'plugin_icons_'.$this->getPluginComponent());
  38. $this->Lexer->addSpecialPattern(sprintf($this->linkPattern, $this->pattern), $mode, 'plugin_icons_'.$this->getPluginComponent());
  39. }
  40. /**
  41. * Handler to prepare matched data for the rendering process
  42. *
  43. * @param string $match The text matched by the patterns
  44. * @param int $state The lexer state for the match
  45. * @param int $pos The character position of the matched text
  46. * @param Doku_Handler $handler The Doku_Handler object
  47. * @return bool|array Return an array with all data you want to use in render, false don't add an instruction
  48. */
  49. public function handle($match, $state, $pos, Doku_Handler $handler) {
  50. $url = null;
  51. $flags = array();
  52. $title = null;
  53. $pack = null;
  54. $icon = null;
  55. $match = substr($match, 2, -2); // strip markup
  56. @list($match, $title, $title2) = explode('|', $match);
  57. if (isset($title2)) $title .= '}}';
  58. if (isset($title) && preg_match('/'.$this->pattern.'/', $title)) {
  59. $url = $match;
  60. $match = $title;
  61. $match = substr($match, 2, -2); // strip markup
  62. list($match, $title) = explode('|', $match);
  63. if (isset($title2)) $title = rtrim($title2, '}');
  64. }
  65. list($match, $flags) = explode('?', $match, 2);
  66. list($pack, $icon) = explode('>', $match, 2);
  67. return array($pack, $icon, explode('&', $flags), $title, $url, $match, $state, $pos);
  68. }
  69. /**
  70. * Handles the actual output creation.
  71. *
  72. * @param string $mode output format being rendered
  73. * @param Doku_Renderer $renderer the current renderer object
  74. * @param array $data data created by handler()
  75. * @return boolean rendered correctly? (however, returned value is not used at the moment)
  76. */
  77. public function render($mode, Doku_Renderer $renderer, $data) {
  78. if ($mode !== 'xhtml') return false;
  79. /** @var Doku_Renderer_xhtml $renderer */
  80. list($pack, $icon, $flags, $title, $url) = $data;
  81. $this->parseFlags($pack, $icon, $flags);
  82. if ($this->isIcon()) {
  83. $icon_size = $this->getFlag('size');
  84. $icon_pack = $this->getFlag('pack');
  85. $base_path = rtrim($this->getConf(sprintf('%sURL', $icon_pack)), '/');
  86. $icon_path = $this->makePath($icon, $icon_size, $base_path);
  87. $icon_markup = sprintf('<img src="%s" title="%s" class="%s" style="%s" />',
  88. $icon_path, $title,
  89. $this->toClassString($this->getClasses()),
  90. $this->toInlineStyle($this->getStyles()));
  91. } else {
  92. $this->classes[] = $this->getFlag('pack');
  93. $this->classes[] = sprintf('%s-%s', $this->getFlag('pack'), $icon);
  94. $icon_markup = sprintf('<i class="%s" style="%s" title="%s"></i>',
  95. $this->toClassString($this->getClasses()),
  96. $this->toInlineStyle($this->getStyles()),
  97. $title);
  98. }
  99. if (isset($url)) {
  100. global $conf;
  101. global $ID;
  102. $is_external = false;
  103. $exists = false;
  104. $link = array();
  105. if (preg_match('/^(http?|ftp?|www?)/', $url)) {
  106. $is_external = true;
  107. } else {
  108. resolve_pageid(getNS($ID), $url, $exists);
  109. $url = wl($url);
  110. }
  111. $link['target'] = ($is_external) ? $conf['target']['extern'] : $conf['target']['wiki'];
  112. $link['style'] = '';
  113. $link['pre'] = '';
  114. $link['suf'] = '';
  115. $link['more'] = '';
  116. $link['class'] = '';
  117. $link['url'] = $url;
  118. $link['name'] = $icon_markup;
  119. if ($exists) {
  120. $link['class'] = 'wikilink1';
  121. } else {
  122. $link['rel'] = 'nofollow';
  123. if (! $is_external) {
  124. $link['class'] = 'wikilink2';
  125. }
  126. }
  127. $renderer->doc .= $renderer->_formatLink($link);
  128. return true;
  129. }
  130. $renderer->doc .= $icon_markup;
  131. return true;
  132. }
  133. protected function isIcon() {
  134. $class_icon = sprintf('syntax_plugin_icons_%s', $this->getFlag('pack'));
  135. return constant("$class_icon::IS_ICON");
  136. }
  137. protected function isFontIcon() {
  138. $class_icon = sprintf('syntax_plugin_icons_%s', $this->getFlag('pack'));
  139. return constant("$class_icon::IS_FONT_ICON");
  140. }
  141. protected function toClassString($things) {
  142. return trim(implode(' ', $things), ' ');
  143. }
  144. protected function toInlineStyle($things) {
  145. $result = '';
  146. foreach ($things as $property => $value) {
  147. $result .= "$property:$value;";
  148. }
  149. $result = trim($result, ';');
  150. return $result;
  151. }
  152. protected function getFlag($name) {
  153. return (isset($this->flags[$name]) ? $this->flags[$name] : null);
  154. }
  155. protected function getFlags() {
  156. return $this->flags;
  157. }
  158. protected function parseFlags($pack, $icon, $flags) {
  159. $this->flags = array();
  160. $this->classes = array();
  161. $this->styles = array();
  162. $this->flags['pack'] = $pack;
  163. $this->flags['icon'] = $icon;
  164. if ((int) $flags[0] > 0 && ! in_array($flags[0], array('2x', '3x', '4x', '5x'))) {
  165. $flags[] = "size=" . $flags[0];
  166. unset($flags[0]);
  167. }
  168. if ($left = array_search('left', $flags)) {
  169. $flags[] = 'align=left';
  170. unset($flags[$left]);
  171. }
  172. if ($right = array_search('right', $flags)) {
  173. $flags[] = 'align=right';
  174. unset($flags[$right]);
  175. }
  176. if ($center = array_search('center', $flags)) {
  177. $flags[] = 'align=center';
  178. unset($flags[$center]);
  179. }
  180. foreach ($flags as $flag) {
  181. @list($flag, $value) = explode('=', $flag);
  182. if (! $flag) continue;
  183. $this->flags[$flag] = $value;
  184. switch ($flag) {
  185. case 'size':
  186. $this->flags[$flag] = (int) $value;
  187. if ($this->isFontIcon()) {
  188. $this->styles['font-size'] = "{$value}px";
  189. }
  190. break;
  191. case 'circle':
  192. $this->flags[$flag] = true;
  193. $this->styles['border-radius'] = '50%';
  194. $this->styles['-moz-border-radius'] = '50%';
  195. $this->styles['-webkit-border-radius'] = '50%';
  196. break;
  197. case 'border':
  198. $this->flags[$flag] = true;
  199. if ($this->flags['pack'] == 'fa') {
  200. $this->classes[] = 'fa-border';
  201. } else {
  202. $this->styles['border'] = '0.08em solid #EEE';
  203. }
  204. break;
  205. case 'borderColor':
  206. $this->styles['border-color'] = $value;
  207. break;
  208. case 'padding':
  209. $this->styles['padding'] = $value;
  210. break;
  211. case 'background':
  212. $this->styles['background-color'] = $value;
  213. break;
  214. case 'color':
  215. $this->styles['color'] = $value;
  216. break;
  217. case 'class':
  218. $this->classes[] = $value;
  219. break;
  220. case 'align':
  221. if ($this->isIcon()) {
  222. $this->classes[] = "media$value";
  223. } else {
  224. if ($value == 'center') {
  225. $this->styles['text-align'] = 'center';
  226. } else {
  227. $this->styles['padding-'.(($value == 'left') ? 'right' : 'left')] = '.2em';
  228. $this->styles['float'] = $value;
  229. }
  230. }
  231. break;
  232. case 'rotate':
  233. if (in_array($value, array(90, 180, 270))) {
  234. $this->classes[] = "fa-rotate-$value";
  235. }
  236. break;
  237. case 'flip':
  238. if (in_array($value, array('horizontal', 'vertical'))) {
  239. $this->classes[] = "fa-flip-$value";
  240. }
  241. break;
  242. case 'pull-left':
  243. case 'pullLeft':
  244. case 'pull-right':
  245. case 'pullRight':
  246. case 'spin':
  247. case 'pulse':
  248. $this->classes[] = "fa-$flag";
  249. break;
  250. case 'fw':
  251. case 'lg':
  252. case '2x':
  253. case '3x':
  254. case '4x':
  255. case '5x':
  256. $this->classes[] = "fa-$flag";
  257. $this->flags['size'] = true;
  258. unset($this->styles['font-size']);
  259. break;
  260. default:
  261. $this->classes[] = $flag;
  262. }
  263. }
  264. if (! isset($this->flags['size'])) {
  265. $this->flags['size'] = (int) $this->getConf('defaultSize');
  266. if ($this->isFontIcon()) {
  267. $this->styles['font-size'] = $this->getConf('defaultSize') . "px";
  268. }
  269. }
  270. if ($this->flags['pack'] == 'icon') {
  271. $this->flags['pack'] = $this->getConf('defaultPack');
  272. }
  273. }
  274. protected function getStyles() {
  275. return $this->styles;
  276. }
  277. protected function getClasses() {
  278. return $this->classes;
  279. }
  280. public static function makePath($icon, $size, $base_url) {
  281. return true;
  282. }
  283. }