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. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Paul M. Jones <pmjones@ciaweb.net>                          |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: url.php,v 1.4 2004/03/11 03:18:33 pmjones Exp $
  20.  
  21. /**
  22. * This class implements a Text_Wiki_Rule to find source text marked as a
  23. * URL.  Various URL markings are supported: inline (the URL by itself),
  24. * numbered or footnote reference (where the URL is enclosed in square brackets), and
  25. * named reference (where the URL is enclosed in square brackets and has a
  26. * 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. @author Paul M. Jones <pmjones@ciaweb.net>
  37. *
  38. @package Text_Wiki
  39. *
  40. */
  41.  
  42.     
  43.     
  44.     /**
  45.     * 
  46.     * When doing numbered references (footnote-style references), we
  47.     * need to keep a running count of how many references there are.
  48.     * 
  49.     * @access public
  50.     * 
  51.     * @var int 
  52.     * 
  53.     */
  54.     
  55.     var $footnoteCount = 0;
  56.     
  57.     
  58.     /**
  59.     * 
  60.     * An array of filename extensions that indicate a file is an image.
  61.     * 
  62.     * @access public
  63.     * 
  64.     * @var array 
  65.     * 
  66.     */
  67.     
  68.     var $img_ext = array('.jpg''.png''.gif');
  69.     
  70.     
  71.     function Text_Wiki_Rule_url(&$obj$name)
  72.     {
  73.         parent::Text_Wiki_Rule($obj$name);
  74.         
  75.         $this->regex = 
  76.             "(http:\/\/|https:\/\/|ftp:\/\/|gopher:\/\/|news:\/\/|mailto:)" // protocols
  77.             "(" 
  78.             "[^ \\/\"\'{$this->_wiki->delim}]*\\/" . // no spaces, \, /, ", or single quotes;
  79.             ")*" 
  80.             "[^ \\t\\n\\/\"\'{$this->_wiki->delim}]*" .
  81.             "[A-Za-z0-9\\/?=&~_]";
  82.         
  83.     }
  84.     
  85.     
  86.     /**
  87.     * 
  88.     * A somewhat complex parsing method to find three different kinds
  89.     * of URLs in the source text.
  90.     *
  91.     * @access public
  92.     * 
  93.     */
  94.     
  95.     function parse()
  96.     {
  97.         // -------------------------------------------------------------
  98.         // 
  99.         // Described-reference (named) URLs.
  100.         // 
  101.         
  102.         // the regular expression for this kind of URL
  103.         $tmp_regex '/\[(' $this->regex . ') ([^\]]+)\]/';
  104.         
  105.         // use a custom callback processing method to generate
  106.         // the replacement text for matches.
  107.         $this->_wiki->_source = preg_replace_callback(
  108.             $tmp_regex,
  109.             array(&$this'processDescr'),
  110.             $this->_wiki->_source
  111.         );
  112.         
  113.         
  114.         // -------------------------------------------------------------
  115.         // 
  116.         // Numbered-reference (footnote-style) URLs.
  117.         // 
  118.         
  119.         // the regular expression for this kind of URL
  120.         $tmp_regex '/\[(' $this->regex . ')\]/U';
  121.         
  122.         // use a custom callback processing method to generate
  123.         // the replacement text for matches.
  124.         $this->_wiki->_source = preg_replace_callback(
  125.             $tmp_regex,
  126.             array(&$this'processFootnote'),
  127.             $this->_wiki->_source
  128.         );
  129.         
  130.         
  131.         // -------------------------------------------------------------
  132.         // 
  133.         // Normal inline URLs.
  134.         // 
  135.         
  136.         // the regular expression for this kind of URL
  137.         
  138.         $tmp_regex '/(^|[^A-Za-z])(' $this->regex . ')(.*?)/';
  139.         
  140.         // use the standard callback for inline URLs
  141.         $this->_wiki->_source = preg_replace_callback(
  142.             $tmp_regex,
  143.             array(&$this'process'),
  144.             $this->_wiki->_source
  145.         );
  146.     }
  147.     
  148.     
  149.     /**
  150.     * 
  151.     * Process inline URLs and return replacement text with a delimited
  152.     * token.
  153.     * 
  154.     * Token options are:
  155.     *     'type' => ['inline'|'footnote'|'descr'] the type of URL
  156.     *     'href' => the URL link href portion
  157.     *     'text' => the displayed text of the URL link
  158.     * 
  159.     * @param array &$matches 
  160.     * 
  161.     * @param array $matches An array of matches from the parse() method
  162.     *  as generated by preg_replace_callback.  $matches[0] is the full
  163.     *  matched string, $matches[1] is the first matched pattern,
  164.     *  $matches[2] is the second matched pattern, and so on.
  165.     * 
  166.     * @return string The processed text replacement.
  167.     * 
  168.     */ 
  169.     
  170.     function process(&$matches)
  171.     {
  172.         // set options
  173.         $options = array(
  174.             'type' => 'inline',
  175.             'href' => $matches[2],
  176.             'text' => $matches[2]
  177.         );
  178.         
  179.         // tokenize
  180.         return $matches[1$this->addToken($options$matches[5];
  181.     }
  182.     
  183.     
  184.     /**
  185.     * 
  186.     * Process numbered (footnote) URLs and return replacement text with
  187.     * a delimited token.
  188.     * 
  189.     * Token options are:
  190.     *     'type' => ['inline'|'footnote'|'descr'] the type of URL
  191.     *     'href' => the URL link href portion
  192.     *     'text' => the displayed text of the URL link
  193.     * 
  194.     * @param array &$matches 
  195.     * 
  196.     * @param array $matches An array of matches from the parse() method
  197.     *  as generated by preg_replace_callback.  $matches[0] is the full
  198.     *  matched string, $matches[1] is the first matched pattern,
  199.     *  $matches[2] is the second matched pattern, and so on.
  200.     * 
  201.     * @return string The processed text replacement.
  202.     * 
  203.     */ 
  204.     
  205.     function processFootnote(&$matches)
  206.     {
  207.         // keep a running count for footnotes 
  208.         $this->footnoteCount++;
  209.         
  210.         // set options
  211.         $options = array(
  212.             'type' => 'footnote',
  213.             'href' => $matches[1],
  214.             'text' => $this->footnoteCount
  215.         );
  216.         
  217.         // tokenize
  218.         return $this->addToken($options);
  219.     }
  220.     
  221.     
  222.     /**
  223.     * 
  224.     * Process described-reference (named-reference) URLs and return
  225.     * replacement text with a delimited token.
  226.     * 
  227.     * Token options are:
  228.     *     'type' => ['inline'|'footnote'|'descr'] the type of URL
  229.     *     'href' => the URL link href portion
  230.     *     'text' => the displayed text of the URL link
  231.     * 
  232.     * @param array &$matches 
  233.     * 
  234.     * @param array $matches An array of matches from the parse() method
  235.     *  as generated by preg_replace_callback.  $matches[0] is the full
  236.     *  matched string, $matches[1] is the first matched pattern,
  237.     *  $matches[2] is the second matched pattern, and so on.
  238.     * 
  239.     * @return string The processed text replacement.
  240.     * 
  241.     */ 
  242.     
  243.     function processDescr(&$matches)
  244.     {
  245.         // set options
  246.         $options = array(
  247.             'type' => 'descr',
  248.             'href' => $matches[1],
  249.             'text' => $matches[4]
  250.         );
  251.         
  252.         // tokenize
  253.         return $this->addToken($options);
  254.     }
  255.     
  256.     
  257.     /**
  258.     * 
  259.     * Renders a token into text matching the requested format.
  260.     * 
  261.     * @access public
  262.     * 
  263.     * @param array $options The "options" portion of the token (second
  264.     *  element).
  265.     * 
  266.     * @return string The text rendered from the token options.
  267.     * 
  268.     */
  269.     
  270.     function renderXhtml($options)
  271.     {
  272.         // create local variables from the options array (text,
  273.         // href, type)
  274.         extract($options);
  275.         
  276.         // find the rightmost dot and determine the filename
  277.         // extension.
  278.         $pos strrpos($href'.');
  279.         $ext strtolower(substr($href$pos));
  280.         
  281.         // does the filename extension indicate an image file?
  282.         if (in_array($ext$this->img_ext)) {
  283.             
  284.             // create alt text for the image
  285.             if (isset($text|| $text == ''{
  286.                 $text basename($href);
  287.             }
  288.             
  289.             // generate an image tag
  290.             $output = "<img src=\"$href\" alt=\"$text\" />";
  291.             
  292.         else {
  293.         
  294.             // generate a regular link (not an image)
  295.             $output = "<a href=\"$href\">$text</a>";
  296.             
  297.             // make numbered references look like footnotes
  298.             if ($type == 'footnote'{
  299.                 $output '<sup>' $output '</sup>';
  300.             }
  301.         }
  302.         
  303.         return $output;
  304.     }
  305. }
  306. ?>

Documentation generated on Mon, 11 Mar 2019 10:14:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.