[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class PhabricatorIconRemarkupRule extends PhutilRemarkupRule { 4 5 public function getPriority() { 6 return 200.0; 7 } 8 9 public function apply($text) { 10 return preg_replace_callback( 11 '@{icon\b((?:[^}\\\\]+|\\\\.)*)}@m', 12 array($this, 'markupIcon'), 13 $text); 14 } 15 16 public function markupIcon($matches) { 17 $engine = $this->getEngine(); 18 $text_mode = $engine->isTextMode(); 19 $mail_mode = $engine->isHTMLMailMode(); 20 21 if (!$this->isFlatText($matches[0]) || $text_mode || $mail_mode) { 22 return $matches[0]; 23 } 24 25 $extra = idx($matches, 1); 26 27 // We allow various forms, like these: 28 // 29 // {icon} 30 // {icon camera} 31 // {icon,camera} 32 // {icon camera color=red} 33 // {icon, camera, color=red} 34 35 $extra = ltrim($extra, ", \n"); 36 $extra = preg_split('/[\s,]+/', $extra, 2); 37 38 // Choose some arbitrary default icon so that previews render in a mostly 39 // reasonable way as you're typing the syntax. 40 $icon = idx($extra, 0, 'paw'); 41 42 $defaults = array( 43 'color' => null, 44 ); 45 46 $options = idx($extra, 1, ''); 47 $parser = new PhutilSimpleOptions(); 48 $options = $parser->parse($options) + $defaults; 49 50 // NOTE: We're validating icon and color names to prevent users from 51 // adding arbitrary CSS classes to the document. Although this probably 52 // isn't dangerous, it's safer to validate. 53 54 static $icon_names; 55 if (!$icon_names) { 56 $icon_names = array_fuse(PHUIIconView::getFontIcons()); 57 } 58 59 static $color_names; 60 if (!$color_names) { 61 $color_names = array_fuse(PHUIIconView::getFontIconColors()); 62 } 63 64 if (empty($icon_names['fa-'.$icon])) { 65 $icon = 'paw'; 66 } 67 68 $color = $options['color']; 69 if (empty($color_names[$color])) { 70 $color = null; 71 } 72 73 $icon_view = id(new PHUIIconView()) 74 ->setIconFont('fa-'.$icon, $color); 75 76 77 return $this->getEngine()->storeText($icon_view); 78 } 79 80 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |