includes/wikiengine/Parse/Mediawiki/Paragraph.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 paragraph blocks.
       
     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: Paragraph.php,v 1.3 2005/02/23 17:38:29 pmjones Exp $
       
    16 * 
       
    17 */
       
    18 
       
    19 /**
       
    20 * 
       
    21 * Parses for paragraph blocks.
       
    22 * 
       
    23 * This class implements a Text_Wiki rule to find sections of the source
       
    24 * text that are paragraphs.  A para is any line not starting with a token
       
    25 * delimiter, followed by two newlines.
       
    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_Paragraph extends Text_Wiki_Parse {
       
    36     
       
    37     /**
       
    38     * 
       
    39     * The regular expression used to find source text matching this
       
    40     * rule.
       
    41     * 
       
    42     * @access public
       
    43     * 
       
    44     * @var string
       
    45     * 
       
    46     */
       
    47     
       
    48     var $regex = "/^.*?\n\n/m";
       
    49     
       
    50     var $conf = array(
       
    51         'skip' => array(
       
    52             'blockquote', // are we sure about this one?
       
    53             'code',
       
    54             'heading',
       
    55             'horiz',
       
    56             'deflist',
       
    57             'table',
       
    58             'list',
       
    59             'toc',
       
    60             'pre'
       
    61         )
       
    62     );
       
    63     
       
    64     
       
    65     /**
       
    66     * 
       
    67     * Generates a token entry for the matched text.  Token options are:
       
    68     * 
       
    69     * 'start' => The starting point of the paragraph.
       
    70     * 
       
    71     * 'end' => The ending point of the paragraph.
       
    72     * 
       
    73     * @access public
       
    74     *
       
    75     * @param array &$matches The array of matches from parse().
       
    76     *
       
    77     * @return A delimited token number to be used as a placeholder in
       
    78     * the source text.
       
    79     *
       
    80     */
       
    81     
       
    82     function process(&$matches)
       
    83     {
       
    84         $delim = $this->wiki->delim;
       
    85         
       
    86         // was anything there?
       
    87         if (trim($matches[0]) == '') {
       
    88             return '';
       
    89         }
       
    90         
       
    91         // does the match start with a delimiter?
       
    92         if (substr($matches[0], 0, 1) != $delim) { 
       
    93             // no.
       
    94             
       
    95             $start = $this->wiki->addToken(
       
    96                 $this->rule, array('type' => 'start')
       
    97             );
       
    98             
       
    99             $end = $this->wiki->addToken(
       
   100                 $this->rule, array('type' => 'end')
       
   101             );
       
   102             
       
   103             return $start . trim($matches[0]) . $end;
       
   104         }
       
   105         
       
   106         // the line starts with a delimiter.  read in the delimited
       
   107         // token number, check the token, and see if we should
       
   108         // skip it.
       
   109         
       
   110         // loop starting at the second character (we already know
       
   111         // the first is a delimiter) until we find another
       
   112         // delimiter; the text between them is a token key number.
       
   113         $key = '';
       
   114         $len = strlen($matches[0]);
       
   115         for ($i = 1; $i < $len; $i++) {
       
   116             $char = $matches[0]{$i};
       
   117             if ($char == $delim) {
       
   118                 break;
       
   119             } else {
       
   120                 $key .= $char;
       
   121             }
       
   122         }
       
   123         
       
   124         // look at the token and see if it's skippable (if we skip,
       
   125         // it will not be marked as a paragraph)
       
   126         $token_type = strtolower($this->wiki->tokens[$key][0]);
       
   127         $skip = $this->getConf('skip', array());
       
   128         
       
   129         if (in_array($token_type, $skip)) {
       
   130             // this type of token should not have paragraphs applied to it.
       
   131             // return the entire matched text.
       
   132             return $matches[0];
       
   133         } else {
       
   134             
       
   135             $start = $this->wiki->addToken(
       
   136                 $this->rule, array('type' => 'start')
       
   137             );
       
   138             
       
   139             $end = $this->wiki->addToken(
       
   140                 $this->rule, array('type' => 'end')
       
   141             );
       
   142             
       
   143             return $start . trim($matches[0]) . $end;
       
   144         }
       
   145     }
       
   146     
       
   147     /**
       
   148     *
       
   149     * Abstrct method to parse source text for matches.
       
   150     *
       
   151     * Applies the rule's regular expression to the source text, passes
       
   152     * every match to the process() method, and replaces the matched text
       
   153     * with the results of the processing.
       
   154     *
       
   155     * @access public
       
   156     *
       
   157     * @see Text_Wiki_Parse::process()
       
   158     *
       
   159     */
       
   160 
       
   161     function parse()
       
   162     {
       
   163         $source =& $this->wiki->source;
       
   164         $source = wikiformat_process_block($source);
       
   165         
       
   166         $source = preg_replace('/<litewiki>(.*?)<\/litewiki>([\s]+?|$)/is', '<litewiki>\\1\\2</litewiki>', $source);
       
   167         
       
   168         // die('<pre>' . htmlspecialchars($source) . '</pre>');
       
   169         
       
   170         $this->wiki->source = preg_replace_callback(
       
   171             $this->regex,
       
   172             array(&$this, 'process'),
       
   173             $this->wiki->source
       
   174         );
       
   175         
       
   176         $source = preg_replace('/<litewiki>(.*?)<\/litewiki>/is', '\\1', $source);
       
   177     }
       
   178 }
       
   179 ?>