icon.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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-2018, 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. $align_left = false;
  66. $align_right = false;
  67. $align_center = false;
  68. $align_flag = '';
  69. if (substr($match, 0, 1) == ' ') {
  70. $align_right = true;
  71. $align_flag = "align=right";
  72. }
  73. if (substr($match, -1, 1) == ' ') {
  74. $align_left = true;
  75. $align_flag = "align=left";
  76. }
  77. if ($align_left && $align_right) {
  78. $align_center = true;
  79. $align_flag = "align=center";
  80. }
  81. list($match, $flags) = explode('?', trim($match), 2);
  82. list($pack, $icon) = explode('>', trim($match), 2);
  83. $flags .= "&$align_flag";
  84. return array($pack, $icon, explode('&', rtrim($flags, '&')), $title, $url, $match, $state, $pos);
  85. }
  86. /**
  87. * Handles the actual output creation.
  88. *
  89. * @param string $mode output format being rendered
  90. * @param Doku_Renderer $renderer the current renderer object
  91. * @param array $data data created by handler()
  92. * @return boolean rendered correctly? (however, returned value is not used at the moment)
  93. */
  94. public function render($mode, Doku_Renderer $renderer, $data) {
  95. if ($mode !== 'xhtml') return false;
  96. /** @var Doku_Renderer_xhtml $renderer */
  97. list($pack, $icon, $flags, $title, $url) = $data;
  98. $this->parseFlags($pack, $icon, $flags);
  99. if ($this->isIcon()) {
  100. $icon_size = $this->getFlag('size');
  101. $icon_pack = $this->getFlag('pack');
  102. $icon_base_url = rtrim($this->getConf(sprintf('%sURL', $icon_pack)), '/');
  103. $icon_url = $this->makePath($icon, $icon_size, $icon_base_url);
  104. $cached_icon_url = ml($icon_url, array('cache' => 'recache', 'w' => $icon_size, 'h' => $icon_size));
  105. $icon_markup = sprintf('<img src="%s" title="%s" class="%s" style="%s" />',
  106. $cached_icon_url, $title,
  107. $this->toClassString($this->getClasses()),
  108. $this->toInlineStyle($this->getStyles()));
  109. } else {
  110. if ($this->getFlag('pack') == 'material') {
  111. $this->classes[] = 'material-icons';
  112. # Material Icons use ligatures feature supported in most modern browsers
  113. # on both desktop and mobile devices.
  114. $icon_markup = sprintf('<i class="%s" style="%s" title="%s">%s</i>',
  115. $this->toClassString($this->getClasses()),
  116. $this->toInlineStyle($this->getStyles()),
  117. $title, $icon);
  118. } else {
  119. $this->classes[] = $this->getFlag('pack');
  120. $this->classes[] = sprintf('%s-%s', $this->getFlag('pack'), $icon);
  121. $icon_markup = sprintf('<i class="%s" style="%s" title="%s"></i>',
  122. $this->toClassString($this->getClasses()),
  123. $this->toInlineStyle($this->getStyles()),
  124. $title);
  125. }
  126. }
  127. if (isset($url)) {
  128. global $conf;
  129. global $ID;
  130. $is_external = false;
  131. $exists = false;
  132. $link = array();
  133. if (preg_match('/^(http?|ftp?|www?)/', $url)) {
  134. $is_external = true;
  135. } else {
  136. resolve_pageid(getNS($ID), $url, $exists);
  137. $url = wl($url);
  138. }
  139. $link['target'] = ($is_external) ? $conf['target']['extern'] : $conf['target']['wiki'];
  140. $link['style'] = '';
  141. $link['pre'] = '';
  142. $link['suf'] = '';
  143. $link['more'] = '';
  144. $link['class'] = '';
  145. $link['url'] = $url;
  146. $link['name'] = $icon_markup;
  147. if ($exists) {
  148. $link['class'] = 'wikilink1';
  149. } else {
  150. $link['rel'] = 'nofollow';
  151. if (! $is_external) {
  152. $link['class'] = 'wikilink2';
  153. }
  154. }
  155. $renderer->doc .= $renderer->_formatLink($link);
  156. return true;
  157. }
  158. $renderer->doc .= $icon_markup;
  159. return true;
  160. }
  161. protected function isIcon() {
  162. $class_icon = sprintf('syntax_plugin_icons_%s', $this->getFlag('pack'));
  163. return constant("$class_icon::IS_ICON");
  164. }
  165. protected function isFontIcon() {
  166. $class_icon = sprintf('syntax_plugin_icons_%s', $this->getFlag('pack'));
  167. return constant("$class_icon::IS_FONT_ICON");
  168. }
  169. protected function toClassString($things) {
  170. return trim(implode(' ', $things), ' ');
  171. }
  172. protected function toInlineStyle($things) {
  173. $result = '';
  174. foreach ($things as $property => $value) {
  175. $result .= "$property:$value;";
  176. }
  177. $result = trim($result, ';');
  178. return $result;
  179. }
  180. protected function getFlag($name) {
  181. return (isset($this->flags[$name]) ? $this->flags[$name] : null);
  182. }
  183. protected function getFlags() {
  184. return $this->flags;
  185. }
  186. protected function parseFlags($pack, $icon, $flags) {
  187. $this->flags = array();
  188. $this->classes = array();
  189. $this->styles = array();
  190. $this->flags['pack'] = $pack;
  191. $this->flags['icon'] = $icon;
  192. if ((int) $flags[0] > 0 && ! in_array($flags[0], array('2x', '3x', '4x', '5x'))) {
  193. $flags[] = "size=" . $flags[0];
  194. unset($flags[0]);
  195. }
  196. if ($left = array_search('left', $flags)) {
  197. $flags[] = 'align=left';
  198. unset($flags[$left]);
  199. }
  200. if ($right = array_search('right', $flags)) {
  201. $flags[] = 'align=right';
  202. unset($flags[$right]);
  203. }
  204. if ($center = array_search('center', $flags)) {
  205. $flags[] = 'align=center';
  206. unset($flags[$center]);
  207. }
  208. foreach ($flags as $flag) {
  209. @list($flag, $value) = explode('=', $flag);
  210. if (! $flag) continue;
  211. $this->flags[$flag] = $value;
  212. switch ($flag) {
  213. case 'size':
  214. $this->flags[$flag] = (int) $value;
  215. if ($this->isFontIcon()) {
  216. $this->styles['font-size'] = "{$value}px";
  217. }
  218. break;
  219. case 'circle':
  220. $this->flags[$flag] = true;
  221. $this->styles['border-radius'] = '50%';
  222. $this->styles['-moz-border-radius'] = '50%';
  223. $this->styles['-webkit-border-radius'] = '50%';
  224. break;
  225. case 'border':
  226. $this->flags[$flag] = true;
  227. if ($this->flags['pack'] == 'fa') {
  228. $this->classes[] = 'fa-border';
  229. } else {
  230. $this->styles['border'] = '0.08em solid #EEE';
  231. }
  232. break;
  233. case 'borderColor':
  234. $this->styles['border-color'] = $value;
  235. break;
  236. case 'padding':
  237. $this->styles['padding'] = $value;
  238. break;
  239. case 'background':
  240. $this->styles['background-color'] = $value;
  241. break;
  242. case 'color':
  243. $this->styles['color'] = $value;
  244. break;
  245. case 'class':
  246. $this->classes[] = $value;
  247. break;
  248. case 'align':
  249. if ($this->isIcon()) {
  250. $this->classes[] = "media$value";
  251. } else {
  252. if ($value == 'center') {
  253. $this->styles['text-align'] = 'center';
  254. } else {
  255. $this->styles['padding-'.(($value == 'left') ? 'right' : 'left')] = '.2em';
  256. $this->styles['float'] = $value;
  257. }
  258. }
  259. break;
  260. case 'rotate':
  261. if (in_array($value, array(90, 180, 270))) {
  262. $this->classes[] = "fa-rotate-$value";
  263. }
  264. break;
  265. case 'flip':
  266. if (in_array($value, array('horizontal', 'vertical'))) {
  267. $this->classes[] = "fa-flip-$value";
  268. }
  269. break;
  270. case 'pull-left':
  271. case 'pullLeft':
  272. case 'pull-right':
  273. case 'pullRight':
  274. case 'spin':
  275. case 'pulse':
  276. $this->classes[] = "fa-$flag";
  277. break;
  278. case 'fw':
  279. case 'lg':
  280. case '2x':
  281. case '3x':
  282. case '4x':
  283. case '5x':
  284. $this->classes[] = "fa-$flag";
  285. $this->flags['size'] = true;
  286. unset($this->styles['font-size']);
  287. break;
  288. default:
  289. $this->classes[] = $flag;
  290. }
  291. }
  292. if (! isset($this->flags['size'])) {
  293. $this->flags['size'] = (int) $this->getConf('defaultSize');
  294. if ($this->isFontIcon()) {
  295. $this->styles['font-size'] = $this->getConf('defaultSize') . "px";
  296. }
  297. }
  298. if ($this->flags['pack'] == 'icon') {
  299. $this->flags['pack'] = $this->getConf('defaultPack');
  300. }
  301. }
  302. protected function getStyles() {
  303. return $this->styles;
  304. }
  305. protected function getClasses() {
  306. return $this->classes;
  307. }
  308. public static function makePath($icon, $size, $base_url) {
  309. return true;
  310. }
  311. }