includes/wikiengine/Parse/Mediawiki/Newline.php
changeset 1027 98c052fc3337
parent 1026 f0431eb8161e
child 1028 dde4416dea00
equal deleted inserted replaced
1026:f0431eb8161e 1027:98c052fc3337
     1 <?php
       
     2 
       
     3 /**
       
     4 * 
       
     5 * Parses for implied line breaks indicated by newlines.
       
     6 * 
       
     7 * @category Text
       
     8 * 
       
     9 * @package Text_Wiki
       
    10 * 
       
    11 * @author Paul M. Jones <pmjones@php.net>
       
    12 * 
       
    13 * @license LGPL
       
    14 * 
       
    15 * @version $Id: Newline.php,v 1.3 2005/02/23 17:38:29 pmjones Exp $
       
    16 * 
       
    17 */
       
    18 
       
    19 /**
       
    20 * 
       
    21 * Parses for implied line breaks indicated by newlines.
       
    22 * 
       
    23 * This class implements a Text_Wiki_Parse to mark implied line breaks in the
       
    24 * source text, usually a single carriage return in the middle of a paragraph
       
    25 * or block-quoted text.
       
    26 *
       
    27 * @category Text
       
    28 * 
       
    29 * @package Text_Wiki
       
    30 * 
       
    31 * @author Paul M. Jones <pmjones@php.net>
       
    32 * 
       
    33 */
       
    34 
       
    35 class Text_Wiki_Parse_Newline extends Text_Wiki_Parse {
       
    36     
       
    37     
       
    38     /**
       
    39     * 
       
    40     * The regular expression used to parse the source text and find
       
    41     * matches conforming to this rule.  Used by the parse() method.
       
    42     * 
       
    43     * @access public
       
    44     * 
       
    45     * @var string
       
    46     * 
       
    47     * @see parse()
       
    48     * 
       
    49     */
       
    50     
       
    51     var $regex = '/([^\n])\n([^\n])/m';
       
    52     
       
    53     
       
    54     /**
       
    55     * 
       
    56     * Generates a replacement token for the matched text.
       
    57     * 
       
    58     * @access public
       
    59     *
       
    60     * @param array &$matches The array of matches from parse().
       
    61     *
       
    62     * @return string A delimited token to be used as a placeholder in
       
    63     * the source text.
       
    64     *
       
    65     */
       
    66     
       
    67     function process(&$matches)
       
    68     {    
       
    69         return $matches[1] .
       
    70             $this->wiki->addToken($this->rule) .
       
    71             $matches[2];
       
    72     }
       
    73     
       
    74     /**
       
    75     *
       
    76     * Abstrct method to parse source text for matches.
       
    77     *
       
    78     * Applies the rule's regular expression to the source text, passes
       
    79     * every match to the process() method, and replaces the matched text
       
    80     * with the results of the processing.
       
    81     *
       
    82     * @access public
       
    83     *
       
    84     * @see Text_Wiki_Parse::process()
       
    85     *
       
    86     */
       
    87 
       
    88     function parse()
       
    89     {
       
    90         $source =& $this->wiki->source;
       
    91         
       
    92         // This regex attempts to find HTML tags that can be safely compacted together without formatting loss
       
    93         // The idea is to make it easier for the HTML parser to find litewiki elements
       
    94         //$source = preg_replace('/<\/([a-z0-9:-]+?)>([\s]*[\n]+[\s]+|[\s]+[\n]+[\s]*|[\n]+)<([a-z0-9:-]+)(.*?)>/i', '</\\1><\\3\\4>', $source);
       
    95         $source = wikiformat_process_block($source);
       
    96         
       
    97         $rand_key = md5( str_rot13(strval(dechex(time()))) . microtime() . strval(mt_rand()) );
       
    98         preg_match_all('/<(litewiki|pre)([^>]*?)>(.*?)<\/\\1>/is', $this->wiki->source, $matches);
       
    99         
       
   100         $poslist = array();
       
   101         
       
   102         foreach ( $matches[0] as $i => $match )
       
   103         {
       
   104             $source = str_replace($match, "{LITEWIKI:$i:$rand_key}", $source);
       
   105         }
       
   106         
       
   107         $this->wiki->source = preg_replace_callback(
       
   108             $this->regex,
       
   109             array(&$this, 'process'),
       
   110             $this->wiki->source
       
   111         );
       
   112         
       
   113         foreach ( $matches[3] as $i => $match )
       
   114         {
       
   115             $source = str_replace("{LITEWIKI:$i:$rand_key}", $match, $source);
       
   116         }
       
   117         
       
   118         // die('<pre>'.htmlspecialchars($source).'</pre>');
       
   119         
       
   120         unset($matches, $source, $rand_key);
       
   121     }
       
   122 }
       
   123 
       
   124 ?>