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.2 2004/06/19 16:02:12 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.     * 
  69.     * Constructor.
  70.     * 
  71.     * We override the constructor so we can comment the regex nicely.
  72.     * 
  73.     * @access public
  74.     * 
  75.     */
  76.     
  77.     function Text_Wiki_Parse_Url(&$obj)
  78.     {
  79.         parent::Text_Wiki_Parse($obj);
  80.         
  81.         // convert the list of recognized schemes to a regex-safe string,
  82.         // where the pattern delim is a slash
  83.         $tmp = array();
  84.         $list $this->getConf('schemes'array());
  85.         foreach ($list as $val{
  86.             $tmp[preg_quote($val'/');
  87.         }
  88.         $schemes implode('|'$tmp);
  89.         
  90.         // build the regex
  91.         $this->regex =
  92.             "($schemes)" . // allowed schemes
  93.             "(" // start pattern
  94.             "[^ \\/\"\'{$this->wiki->delim}]*\\/" . // no spaces, backslashes, slashes, double-quotes, single quotes, or delimiters;
  95.             ")*" // end pattern
  96.             "[^ \\t\\n\\/\"\'{$this->wiki->delim}]*" .
  97.             "[A-Za-z0-9\\/?=&~_]";
  98.     }
  99.     
  100.     
  101.     /**
  102.     * 
  103.     * Find three different kinds of URLs in the source text.
  104.     *
  105.     * @access public
  106.     * 
  107.     */
  108.     
  109.     function parse()
  110.     {
  111.         // -------------------------------------------------------------
  112.         // 
  113.         // Described-reference (named) URLs.
  114.         // 
  115.         
  116.         // the regular expression for this kind of URL
  117.         $tmp_regex '/\[(' $this->regex . ') ([^\]]+)\]/';
  118.         
  119.         // use a custom callback processing method to generate
  120.         // the replacement text for matches.
  121.         $this->wiki->source = preg_replace_callback(
  122.             $tmp_regex,
  123.             array(&$this'processDescr'),
  124.             $this->wiki->source
  125.         );
  126.         
  127.         
  128.         // -------------------------------------------------------------
  129.         // 
  130.         // Numbered-reference (footnote-style) URLs.
  131.         // 
  132.         
  133.         // the regular expression for this kind of URL
  134.         $tmp_regex '/\[(' $this->regex . ')\]/U';
  135.         
  136.         // use a custom callback processing method to generate
  137.         // the replacement text for matches.
  138.         $this->wiki->source = preg_replace_callback(
  139.             $tmp_regex,
  140.             array(&$this'processFootnote'),
  141.             $this->wiki->source
  142.         );
  143.         
  144.         
  145.         // -------------------------------------------------------------
  146.         // 
  147.         // Normal inline URLs.
  148.         // 
  149.         
  150.         // the regular expression for this kind of URL
  151.         
  152.         $tmp_regex '/(^|[^A-Za-z])(' $this->regex . ')(.*?)/';
  153.         
  154.         // use the standard callback for inline URLs
  155.         $this->wiki->source = preg_replace_callback(
  156.             $tmp_regex,
  157.             array(&$this'process'),
  158.             $this->wiki->source
  159.         );
  160.     }
  161.     
  162.     
  163.     /**
  164.     * 
  165.     * Process inline URLs.
  166.     * 
  167.     * @param array &$matches 
  168.     * 
  169.     * @param array $matches An array of matches from the parse() method
  170.     *  as generated by preg_replace_callback.  $matches[0] is the full
  171.     *  matched string, $matches[1] is the first matched pattern,
  172.     *  $matches[2] is the second matched pattern, and so on.
  173.     * 
  174.     * @return string The processed text replacement.
  175.     * 
  176.     */ 
  177.     
  178.     function process(&$matches)
  179.     {
  180.         // set options
  181.         $options = array(
  182.             'type' => 'inline',
  183.             'href' => $matches[2],
  184.             'text' => $matches[2]
  185.         );
  186.         
  187.         // tokenize
  188.         return $matches[1$this->wiki->addToken($this->rule$options$matches[5];
  189.     }
  190.     
  191.     
  192.     /**
  193.     * 
  194.     * Process numbered (footnote) URLs.
  195.     * 
  196.     * Token options are:
  197.     * @param array &$matches 
  198.     * 
  199.     * @param array $matches An array of matches from the parse() method
  200.     *  as generated by preg_replace_callback.  $matches[0] is the full
  201.     *  matched string, $matches[1] is the first matched pattern,
  202.     *  $matches[2] is the second matched pattern, and so on.
  203.     * 
  204.     * @return string The processed text replacement.
  205.     * 
  206.     */ 
  207.     
  208.     function processFootnote(&$matches)
  209.     {
  210.         // keep a running count for footnotes 
  211.         $this->footnoteCount++;
  212.         
  213.         // set options
  214.         $options = array(
  215.             'type' => 'footnote',
  216.             'href' => $matches[1],
  217.             'text' => $this->footnoteCount
  218.         );
  219.         
  220.         // tokenize
  221.         return $this->wiki->addToken($this->rule$options);
  222.     }
  223.     
  224.     
  225.     /**
  226.     * 
  227.     * Process described-reference (named-reference) URLs.
  228.     * 
  229.     * Token options are:
  230.     *     'type' => ['inline'|'footnote'|'descr'] the type of URL
  231.     *     'href' => the URL link href portion
  232.     *     'text' => the displayed text of the URL link
  233.     * 
  234.     * @param array &$matches 
  235.     * 
  236.     * @param array $matches An array of matches from the parse() method
  237.     *  as generated by preg_replace_callback.  $matches[0] is the full
  238.     *  matched string, $matches[1] is the first matched pattern,
  239.     *  $matches[2] is the second matched pattern, and so on.
  240.     * 
  241.     * @return string The processed text replacement.
  242.     * 
  243.     */ 
  244.     
  245.     function processDescr(&$matches)
  246.     {
  247.         // set options
  248.         $options = array(
  249.             'type' => 'descr',
  250.             'href' => $matches[1],
  251.             'text' => $matches[4]
  252.         );
  253.         
  254.         // tokenize
  255.         return $this->wiki->addToken($this->rule$options);
  256.     }
  257. }
  258. ?>

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