includes/wikiengine/Parse/Default/Url.php
changeset 1 fe660c52c48f
equal deleted inserted replaced
0:902822492a68 1:fe660c52c48f
       
     1 <?php
       
     2 
       
     3 /**
       
     4 * 
       
     5 * Parse for URLS in the source text.
       
     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: Url.php,v 1.3 2005/02/23 17:38:29 pmjones Exp $
       
    16 * 
       
    17 */
       
    18 
       
    19 /**
       
    20 * 
       
    21 * Parse for URLS in the source text.
       
    22 * 
       
    23 * Various URL markings are supported: inline (the URL by itself),
       
    24 * numbered or footnote reference (where the URL is enclosed in square
       
    25 * brackets), and named reference (where the URL is enclosed in square
       
    26 * brackets and has a name included inside the brackets).  E.g.:
       
    27 *
       
    28 * inline    -- http://example.com
       
    29 * numbered  -- [http://example.com]
       
    30 * described -- [http://example.com Example Description]
       
    31 *
       
    32 * When rendering a URL token, this will convert URLs pointing to a .gif,
       
    33 * .jpg, or .png image into an inline <img /> tag (for the 'xhtml'
       
    34 * format).
       
    35 *
       
    36 * Token options are:
       
    37 * 
       
    38 * 'type' => ['inline'|'footnote'|'descr'] the type of URL
       
    39 * 
       
    40 * 'href' => the URL link href portion
       
    41 * 
       
    42 * 'text' => the displayed text of the URL link
       
    43 * 
       
    44 * @category Text
       
    45 * 
       
    46 * @package Text_Wiki
       
    47 * 
       
    48 * @author Paul M. Jones <pmjones@php.net>
       
    49 * 
       
    50 */
       
    51 
       
    52 class Text_Wiki_Parse_Url extends Text_Wiki_Parse {
       
    53     
       
    54     
       
    55     /**
       
    56     * 
       
    57     * Keeps a running count of numbered-reference URLs.
       
    58     * 
       
    59     * @access public
       
    60     * 
       
    61     * @var int
       
    62     * 
       
    63     */
       
    64     
       
    65     var $footnoteCount = 0;
       
    66     
       
    67     
       
    68     /**
       
    69     * 
       
    70     * URL schemes recognized by this rule.
       
    71     * 
       
    72     * @access public
       
    73     * 
       
    74     * @var array
       
    75     * 
       
    76     */
       
    77     
       
    78     var $conf = array(
       
    79         'schemes' => array(
       
    80             'http://',
       
    81             'https://',
       
    82             'ftp://',
       
    83             'gopher://',
       
    84             'news://',
       
    85             'mailto:'
       
    86         )
       
    87     );
       
    88     
       
    89     
       
    90     /**
       
    91     * 
       
    92     * Constructor.
       
    93     * 
       
    94     * We override the constructor so we can comment the regex nicely.
       
    95     * 
       
    96     * @access public
       
    97     * 
       
    98     */
       
    99     
       
   100     function Text_Wiki_Parse_Url(&$obj)
       
   101     {
       
   102         parent::Text_Wiki_Parse($obj);
       
   103         
       
   104         // convert the list of recognized schemes to a regex-safe string,
       
   105         // where the pattern delim is a slash
       
   106         $tmp = array();
       
   107         $list = $this->getConf('schemes', array());
       
   108         foreach ($list as $val) {
       
   109             $tmp[] = preg_quote($val, '/');
       
   110         }
       
   111         $schemes = implode('|', $tmp);
       
   112         
       
   113         // build the regex
       
   114         $this->regex =
       
   115             "($schemes)" . // allowed schemes
       
   116             "(" . // start pattern
       
   117             "[^ \\/".'"'."\'{$this->wiki->delim}]*\\/" . // no spaces, backslashes, slashes, double-quotes, single quotes, or delimiters;
       
   118             ")*" . // end pattern
       
   119             "[^ \\t\\n\\/".'"'."\'{$this->wiki->delim}]*" .
       
   120             "[A-Za-z0-9\\/?=&~_]";
       
   121     }
       
   122     
       
   123     
       
   124     /**
       
   125     * 
       
   126     * Find three different kinds of URLs in the source text.
       
   127     *
       
   128     * @access public
       
   129     * 
       
   130     */
       
   131     
       
   132     function parse()
       
   133     {
       
   134         // -------------------------------------------------------------
       
   135         // 
       
   136         // Described-reference (named) URLs.
       
   137         // 
       
   138         
       
   139         // the regular expression for this kind of URL
       
   140         $tmp_regex = '/\[(' . $this->regex . ') ([^\]]+)\]/';
       
   141         
       
   142         // use a custom callback processing method to generate
       
   143         // the replacement text for matches.
       
   144         $this->wiki->source = preg_replace_callback(
       
   145             $tmp_regex,
       
   146             array(&$this, 'processDescr'),
       
   147             $this->wiki->source
       
   148         );
       
   149         
       
   150         
       
   151         // -------------------------------------------------------------
       
   152         // 
       
   153         // Numbered-reference (footnote-style) URLs.
       
   154         // 
       
   155         
       
   156         // the regular expression for this kind of URL
       
   157         $tmp_regex = '/\[(' . $this->regex . ')\]/U';
       
   158         
       
   159         // use a custom callback processing method to generate
       
   160         // the replacement text for matches.
       
   161         $this->wiki->source = preg_replace_callback(
       
   162             $tmp_regex,
       
   163             array(&$this, 'processFootnote'),
       
   164             $this->wiki->source
       
   165         );
       
   166         
       
   167         
       
   168         // -------------------------------------------------------------
       
   169         // 
       
   170         // Normal inline URLs.
       
   171         // 
       
   172         
       
   173         // the regular expression for this kind of URL
       
   174         
       
   175         $tmp_regex = '/(^|[^A-Za-z])(' . $this->regex . ')(.*?)/';
       
   176         
       
   177         // use the standard callback for inline URLs
       
   178         $this->wiki->source = preg_replace_callback(
       
   179             $tmp_regex,
       
   180             array(&$this, 'process'),
       
   181             $this->wiki->source
       
   182         );
       
   183     }
       
   184     
       
   185     
       
   186     /**
       
   187     * 
       
   188     * Process inline URLs.
       
   189     * 
       
   190     * @param array &$matches
       
   191     * 
       
   192     * @param array $matches An array of matches from the parse() method
       
   193     * as generated by preg_replace_callback.  $matches[0] is the full
       
   194     * matched string, $matches[1] is the first matched pattern,
       
   195     * $matches[2] is the second matched pattern, and so on.
       
   196     * 
       
   197     * @return string The processed text replacement.
       
   198     * 
       
   199     */ 
       
   200     
       
   201     function process(&$matches)
       
   202     {
       
   203         // set options
       
   204         $options = array(
       
   205             'type' => 'inline',
       
   206             'href' => $matches[2],
       
   207             'text' => $matches[2]
       
   208         );
       
   209         
       
   210         // tokenize
       
   211         return $matches[1] . $this->wiki->addToken($this->rule, $options) . $matches[5];
       
   212     }
       
   213     
       
   214     
       
   215     /**
       
   216     * 
       
   217     * Process numbered (footnote) URLs.
       
   218     * 
       
   219     * Token options are:
       
   220     * @param array &$matches
       
   221     * 
       
   222     * @param array $matches An array of matches from the parse() method
       
   223     * as generated by preg_replace_callback.  $matches[0] is the full
       
   224     * matched string, $matches[1] is the first matched pattern,
       
   225     * $matches[2] is the second matched pattern, and so on.
       
   226     * 
       
   227     * @return string The processed text replacement.
       
   228     * 
       
   229     */ 
       
   230     
       
   231     function processFootnote(&$matches)
       
   232     {
       
   233         // keep a running count for footnotes 
       
   234         $this->footnoteCount++;
       
   235         
       
   236         // set options
       
   237         $options = array(
       
   238             'type' => 'footnote',
       
   239             'href' => $matches[1],
       
   240             'text' => $this->footnoteCount
       
   241         );
       
   242         
       
   243         // tokenize
       
   244         return $this->wiki->addToken($this->rule, $options);
       
   245     }
       
   246     
       
   247     
       
   248     /**
       
   249     * 
       
   250     * Process described-reference (named-reference) URLs.
       
   251     * 
       
   252     * Token options are:
       
   253     *     'type' => ['inline'|'footnote'|'descr'] the type of URL
       
   254     *     'href' => the URL link href portion
       
   255     *     'text' => the displayed text of the URL link
       
   256     * 
       
   257     * @param array &$matches
       
   258     * 
       
   259     * @param array $matches An array of matches from the parse() method
       
   260     * as generated by preg_replace_callback.  $matches[0] is the full
       
   261     * matched string, $matches[1] is the first matched pattern,
       
   262     * $matches[2] is the second matched pattern, and so on.
       
   263     * 
       
   264     * @return string The processed text replacement.
       
   265     * 
       
   266     */ 
       
   267     
       
   268     function processDescr(&$matches)
       
   269     {
       
   270         // set options
       
   271         $options = array(
       
   272             'type' => 'descr',
       
   273             'href' => $matches[1],
       
   274             'text' => $matches[4]
       
   275         );
       
   276         
       
   277         // tokenize
       
   278         return $this->wiki->addToken($this->rule, $options);
       
   279     }
       
   280 }
       
   281 ?>