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

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