includes/wikiengine/Parse/Mediawiki/Smiley.php
changeset 1027 98c052fc3337
parent 1026 f0431eb8161e
child 1028 dde4416dea00
equal deleted inserted replaced
1026:f0431eb8161e 1027:98c052fc3337
     1 <?php
       
     2 // vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
       
     3 /**
       
     4  * Default: Parses for smileys / emoticons tags
       
     5  *
       
     6  * This class implements a Text_Wiki_Rule to find source text marked as
       
     7  * smileys defined by symbols as ':)' , ':-)' or ':smile:'
       
     8  * The symbol is replaced with a token.
       
     9  *
       
    10  * PHP versions 4 and 5
       
    11  *
       
    12  * @category   Text
       
    13  * @package    Text_Wiki
       
    14  * @author     Bertrand Gugger <bertrand@toggg.com>
       
    15  * @copyright  2005 bertrand Gugger
       
    16  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
       
    17  * @version    CVS: $Id: Smiley.php,v 1.6 2005/10/04 08:17:51 toggg Exp $
       
    18  * @link       http://pear.php.net/package/Text_Wiki
       
    19  */
       
    20 
       
    21 /**
       
    22  * Smiley rule parser class for Default.
       
    23  *
       
    24  * @category   Text
       
    25  * @package    Text_Wiki
       
    26  * @author     Bertrand Gugger <bertrand@toggg.com>
       
    27  * @copyright  2005 bertrand Gugger
       
    28  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
       
    29  * @version    Release: @package_version@
       
    30  * @link       http://pear.php.net/package/Text_Wiki
       
    31  * @see        Text_Wiki_Parse::Text_Wiki_Parse()
       
    32  */
       
    33 class Text_Wiki_Parse_Smiley extends Text_Wiki_Parse {
       
    34 
       
    35     /**
       
    36      * Configuration keys for this rule
       
    37      * 'smileys' => array Smileys recognized by this rule, symbols key definitions:
       
    38      *              'symbol' => array ( 'name', 'description' [, 'variante', ...] ) as
       
    39      *                  ':)'  => array('smile', 'Smile'),
       
    40      *                  ':D'  => array('biggrin', 'Very Happy',':grin:'),
       
    41      *              the eventual elements after symbol and description are variantes
       
    42      *
       
    43      * 'auto_nose' => boolean enabling the auto nose feature:
       
    44      *                auto build a variante for 2 chars symbols by inserting a '-' as ':)' <=> ':-)'
       
    45      *
       
    46      * @access public
       
    47      * @var array 'config-key' => mixed config-value
       
    48      */
       
    49     var $conf = array(
       
    50         'smileys' => array(
       
    51             ':D'        => array('biggrin', 'Very Happy', ':grin:'),
       
    52             ':)'        => array('smile', 'Smile', '(:'),
       
    53             ':('        => array('sad', 'Sad', '):'),
       
    54             ':o'        => array('surprised', 'Surprised', ':eek:', 'o:'),
       
    55             ':shock:'   => array('eek', 'Shocked'),
       
    56             ':?'        => array('confused', 'Confused', ':???:'),
       
    57             '8)'        => array('cool', 'Cool', '(8'),
       
    58             ':lol:'     => array('lol', 'Laughing'),
       
    59             ':x'        => array('mad', 'Mad'),
       
    60             ':P'        => array('razz', 'Razz'),
       
    61             ':oops:'    => array('redface', 'Embarassed'),
       
    62             ':cry:'     => array('cry', 'Crying or Very sad'),
       
    63             ':evil:'    => array('evil', 'Evil or Very Mad'),
       
    64             ':twisted:' => array('twisted', 'Twisted Evil'),
       
    65             ':roll:'    => array('rolleyes', 'Rolling Eyes'),
       
    66             ';)'        => array('wink', 'Wink', '(;'),
       
    67             ':!:'       => array('exclaim', 'Exclamation'),
       
    68             ':?:'       => array('question', 'Question'),
       
    69             ':idea:'    => array('idea', 'Idea'),
       
    70             ':arrow:'   => array('arrow', 'Arrow'),
       
    71             ':|'        => array('neutral', 'Neutral', '|:'),
       
    72             ':mrgreen:' => array('mrgreen', 'Mr. Green'),
       
    73         ),
       
    74         'auto_nose' => true
       
    75     );
       
    76 
       
    77     /**
       
    78      * Definition array of smileys, variantes references their model
       
    79      * 'symbol' => array ( 'name', 'description')
       
    80      *
       
    81      * @access private
       
    82      * @var array 'config-key' => mixed config-value
       
    83      */
       
    84     var $_smileys = array();
       
    85 
       
    86      /**
       
    87      * Constructor.
       
    88      * We override the constructor to build up the regex from config
       
    89      *
       
    90      * @param object &$obj the base conversion handler
       
    91      * @return The parser object
       
    92      * @access public
       
    93      */
       
    94     function Text_Wiki_Parse_Smiley(&$obj)
       
    95     {
       
    96         $default = $this->conf;
       
    97         parent::Text_Wiki_Parse($obj);
       
    98 
       
    99         // read the list of smileys to sort out variantes and :xxx: while building the regexp
       
   100         $this->_smileys = $this->getConf('smileys', $default['smileys']);
       
   101         $autoNose = $this->getConf('auto_nose', $default['auto_nose']);
       
   102         $reg1 = $reg2 = '';
       
   103         $sep1 = ':(?:';
       
   104         $sep2 = '';
       
   105         foreach ($this->_smileys as $smiley => $def) {
       
   106             for ($i = 1; $i < count($def); $i++) {
       
   107                 if ($i > 1) {
       
   108                     $cur = $def[$i];
       
   109                     $this->_smileys[$cur] = &$this->_smileys[$smiley];
       
   110                 } else {
       
   111                     $cur = $smiley;
       
   112                 }
       
   113                 $len = strlen($cur);
       
   114                 if (($cur{0} == ':') && ($len > 2) && ($cur{$len - 1} == ':')) {
       
   115                     $reg1 .= $sep1 . preg_quote(substr($cur, 1, -1), '#');
       
   116                     $sep1 = '|';
       
   117                     continue;
       
   118                 }
       
   119                 if ($autoNose && ($len === 2)) {
       
   120                     $variante = $cur{0} . '-' . $cur{1};
       
   121                     $this->_smileys[$variante] = &$this->_smileys[$smiley];
       
   122                     $cur = preg_quote($cur{0}, '#') . '-?' . preg_quote($cur{1}, '#');
       
   123                 } else {
       
   124                     $cur = preg_quote($cur, '#');
       
   125                 }
       
   126                 $reg2 .= $sep2 . $cur;
       
   127                 $sep2 = '|';
       
   128             }
       
   129         }
       
   130         $delim = '[\n\r\s' . $this->wiki->delim . '$^]';
       
   131         $this->regex = '#(?<=' . $delim .
       
   132              ')(' . ($reg1 ? $reg1 . '):' . ($reg2 ? '|' : '') : '') . $reg2 .
       
   133              ')(?=' . $delim . ')#i';
       
   134     }
       
   135 
       
   136     /**
       
   137      * Generates a replacement token for the matched text.  Token options are:
       
   138      *     'symbol' => the original marker
       
   139      *     'name' => the name of the smiley
       
   140      *     'desc' => the description of the smiley
       
   141      *
       
   142      * @param array &$matches The array of matches from parse().
       
   143      * @return string Delimited token representing the smiley
       
   144      * @access public
       
   145      */
       
   146     function process(&$matches)
       
   147     {
       
   148         // tokenize
       
   149         return $this->wiki->addToken($this->rule,
       
   150             array(
       
   151                 'symbol' => $matches[1],
       
   152                 'name'   => $this->_smileys[$matches[1]][0],
       
   153                 'desc'   => $this->_smileys[$matches[1]][1]
       
   154             ));
       
   155     }
       
   156 }
       
   157 ?>