home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Text / Wiki / Parse / Default / Url.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  6.9 KB  |  281 lines

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