Text_Wiki
[ class tree: Text_Wiki ] [ index: Text_Wiki ] [ all elements ]

Source for file Url.php

Documentation is available at Url.php

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

Documentation generated on Mon, 11 Mar 2019 13:56:12 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.