includes/wikiengine/Parse/Mediawiki/Emphasis.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  * Mediawiki: Parses for emphazised text.
       
     5  *
       
     6  * Text_Wiki rule parser to find source text emphazised
       
     7  * as defined by text surrounded by repeated single quotes  ''...'' and more
       
     8  * Translated are ''emphasis'' , '''strong''' or '''''both''''' ...
       
     9  *
       
    10  * PHP versions 4 and 5
       
    11  *
       
    12  * @category   Text
       
    13  * @package    Text_Wiki
       
    14  * @author     Bertrand Gugger <bertrand@toggg.com>
       
    15  * @author     Paul M. Jones <pmjones@php.net>
       
    16  * @copyright  2005 bertrand Gugger
       
    17  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
       
    18  * @version    CVS: $Id: Emphasis.php,v 1.4 2006/02/15 12:27:40 toggg Exp $
       
    19  * @link       http://pear.php.net/package/Text_Wiki
       
    20  */
       
    21 
       
    22 /**
       
    23  * Emphazised text rule parser class for Mediawiki. Makes Emphasis, Strong or both
       
    24  * This class implements a Text_Wiki_Parse to find source text marked for
       
    25  * emphasis, stronger and very as defined by text surrounded by 2,3 or 5 single-quotes.
       
    26  * On parsing, the text itself is left in place, but the starting and ending
       
    27  * instances of the single-quotes are replaced with tokens.
       
    28  *
       
    29  * @category   Text
       
    30  * @package    Text_Wiki
       
    31  * @author     Bertrand Gugger <bertrand@toggg.com>
       
    32  * @copyright  2005 bertrand Gugger
       
    33  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
       
    34  * @version    Release: @package_version@
       
    35  * @link       http://pear.php.net/package/Text_Wiki
       
    36  * @see        Text_Wiki_Parse::Text_Wiki_Parse()
       
    37  */
       
    38 class Text_Wiki_Parse_Emphasis extends Text_Wiki_Parse {
       
    39 
       
    40     /**
       
    41      * The regular expression used to parse the source text and find
       
    42      * matches conforming to this rule.  Used by the parse() method.
       
    43      * We match '' , ''' or ''''' embeded texts
       
    44      *
       
    45      * @access public
       
    46      * @var string
       
    47      * @see Text_Wiki_Parse::parse()
       
    48      */
       
    49     var $regex = "/(?<!')'('{1,4})(.*?)\\1'(?!')/";
       
    50 
       
    51     /**
       
    52      * Generates a replacement for the matched text.  Token options are:
       
    53      * - 'type' => ['start'|'end'] The starting or ending point of the emphasized text.
       
    54      * Generated tokens are Emphasis (this rule), Strong or Emphasis / Strong
       
    55      * The text itself is left in the source but may content bested blocks
       
    56      *
       
    57      * @access public
       
    58      * @param array &$matches The array of matches from parse().
       
    59      * @return string Delimited by start/end tokens to be used as
       
    60      * placeholder in the source text surrounding the text to be emphasized.
       
    61      */
       
    62     function process(&$matches)
       
    63     {
       
    64         $embeded = $matches[2];
       
    65         switch (strlen($matches[1])) {
       
    66             case 1:
       
    67                 $start = $this->wiki->addToken($this->rule, array('type' => 'start'));
       
    68                 $end   = $this->wiki->addToken($this->rule, array('type' => 'end'));
       
    69             break;
       
    70             case 3:
       
    71                 $embeded = "'" . $embeded . "'";
       
    72             case 2:
       
    73                 $start = $this->wiki->addToken('Strong',    array('type' => 'start'));
       
    74                 $end   = $this->wiki->addToken('Strong',    array('type' => 'end'));
       
    75             break;
       
    76             case 4:
       
    77                 $start = $this->wiki->addToken($this->rule, array('type' => 'start'))
       
    78                        . $this->wiki->addToken('Strong',    array('type' => 'start'));
       
    79                 $end   = $this->wiki->addToken('Strong',    array('type' => 'end'))
       
    80                        . $this->wiki->addToken($this->rule, array('type' => 'end'));
       
    81             break;
       
    82         }
       
    83         return $start . $embeded . $end;
       
    84     }
       
    85 }
       
    86 ?>