[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/bundled-libs/Text/Wiki/Rule/ -> url.php (source)

   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.3 2004/12/02 10:54:32 nohn Exp $
  20  
  21  /**
  22  * 
  23  * This class implements a Text_Wiki_Rule to find source text marked as a
  24  * URL.  Various URL markings are supported: inline (the URL by itself),
  25  * numbered or footnote reference (where the URL is enclosed in square brackets), and
  26  * named reference (where the URL is enclosed in square brackets and has a
  27  * name included inside the brackets).  E.g.:
  28  *
  29  * inline    -- http://example.com
  30  * numbered  -- [http://example.com]
  31  * described -- [http://example.com Example Description]
  32  *
  33  * When rendering a URL token, this will convert URLs pointing to a .gif,
  34  * .jpg, or .png image into an inline <img /> tag (for the 'xhtml'
  35  * format).
  36  *
  37  * @author Paul M. Jones <pmjones@ciaweb.net>
  38  *
  39  * @package Text_Wiki
  40  *
  41  */
  42  
  43  class Text_Wiki_Rule_url extends Text_Wiki_Rule {
  44      
  45      
  46      /**
  47      * 
  48      * When doing numbered references (footnote-style references), we
  49      * need to keep a running count of how many references there are.
  50      * 
  51      * @access public
  52      * 
  53      * @var int
  54      * 
  55      */
  56      
  57      var $footnoteCount = 0;
  58      
  59      
  60      /**
  61      * 
  62      * An array of filename extensions that indicate a file is an image.
  63      * 
  64      * @access public
  65      * 
  66      * @var array
  67      * 
  68      */
  69      
  70      var $img_ext = array('.jpg', '.png', '.gif');
  71      
  72      
  73      function Text_Wiki_Rule_url(&$obj, $name)
  74      {
  75          parent::Text_Wiki_Rule($obj, $name);
  76          
  77          $this->regex = 
  78              "(http:\/\/|https:\/\/|ftp:\/\/|gopher:\/\/|news:\/\/|mailto:)" . // protocols
  79              "(" . 
  80              "[^ \\/\"\'{$this->_wiki->delim}]*\\/" . // no spaces, \, /, ", or single quotes;
  81              ")*" . 
  82              "[^ \\t\\n\\/\"\'{$this->_wiki->delim}]*" .
  83              "[A-Za-z0-9\\/?=&~_]";
  84          
  85      }
  86      
  87      
  88      /**
  89      * 
  90      * A somewhat complex parsing method to find three different kinds
  91      * of URLs in the source text.
  92      *
  93      * @access public
  94      * 
  95      */
  96      
  97      function parse()
  98      {
  99          // -------------------------------------------------------------
 100          // 
 101          // Described-reference (named) URLs.
 102          // 
 103          
 104          // the regular expression for this kind of URL
 105          $tmp_regex = '/\[(' . $this->regex . ') ([^\]]+)\]/';
 106          
 107          // use a custom callback processing method to generate
 108          // the replacement text for matches.
 109          $this->_wiki->_source = preg_replace_callback(
 110              $tmp_regex,
 111              array(&$this, 'processDescr'),
 112              $this->_wiki->_source
 113          );
 114          
 115          
 116          // -------------------------------------------------------------
 117          // 
 118          // Numbered-reference (footnote-style) URLs.
 119          // 
 120          
 121          // the regular expression for this kind of URL
 122          $tmp_regex = '/\[(' . $this->regex . ')\]/U';
 123          
 124          // use a custom callback processing method to generate
 125          // the replacement text for matches.
 126          $this->_wiki->_source = preg_replace_callback(
 127              $tmp_regex,
 128              array(&$this, 'processFootnote'),
 129              $this->_wiki->_source
 130          );
 131          
 132          
 133          // -------------------------------------------------------------
 134          // 
 135          // Normal inline URLs.
 136          // 
 137          
 138          // the regular expression for this kind of URL
 139          
 140          $tmp_regex = '/(^|[^A-Za-z])(' . $this->regex . ')(.*?)/';
 141          
 142          // use the standard callback for inline URLs
 143          $this->_wiki->_source = preg_replace_callback(
 144              $tmp_regex,
 145              array(&$this, 'process'),
 146              $this->_wiki->_source
 147          );
 148      }
 149      
 150      
 151      /**
 152      * 
 153      * Process inline URLs and return replacement text with a delimited
 154      * token.
 155      * 
 156      * Token options are:
 157      *     'type' => ['inline'|'footnote'|'descr'] the type of URL
 158      *     'href' => the URL link href portion
 159      *     'text' => the displayed text of the URL link
 160      * 
 161      * @param array &$matches
 162      * 
 163      * @param array $matches An array of matches from the parse() method
 164      * as generated by preg_replace_callback.  $matches[0] is the full
 165      * matched string, $matches[1] is the first matched pattern,
 166      * $matches[2] is the second matched pattern, and so on.
 167      * 
 168      * @return string The processed text replacement.
 169      * 
 170      */ 
 171      
 172      function process(&$matches)
 173      {
 174          // set options
 175          $options = array(
 176              'type' => 'inline',
 177              'href' => $matches[2],
 178              'text' => $matches[2]
 179          );
 180          
 181          // tokenize
 182          return $matches[1] . $this->addToken($options) . $matches[5];
 183      }
 184      
 185      
 186      /**
 187      * 
 188      * Process numbered (footnote) URLs and return replacement text with
 189      * a delimited token.
 190      * 
 191      * Token options are:
 192      *     'type' => ['inline'|'footnote'|'descr'] the type of URL
 193      *     'href' => the URL link href portion
 194      *     'text' => the displayed text of the URL link
 195      * 
 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->addToken($options);
 221      }
 222      
 223      
 224      /**
 225      * 
 226      * Process described-reference (named-reference) URLs and return
 227      * replacement text with a delimited token.
 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->addToken($options);
 256      }
 257      
 258      
 259      /**
 260      * 
 261      * Renders a token into text matching the requested format.
 262      * 
 263      * @access public
 264      * 
 265      * @param array $options The "options" portion of the token (second
 266      * element).
 267      * 
 268      * @return string The text rendered from the token options.
 269      * 
 270      */
 271      
 272      function renderXhtml($options)
 273      {
 274          // create local variables from the options array (text,
 275          // href, type)
 276          extract($options);
 277          
 278          // find the rightmost dot and determine the filename
 279          // extension.
 280          $pos = strrpos($href, '.');
 281          $ext = strtolower(substr($href, $pos));
 282          
 283          // does the filename extension indicate an image file?
 284          if (in_array($ext, $this->img_ext)) {
 285              
 286              // create alt text for the image
 287              if (! isset($text) || $text == '') {
 288                  $text = basename($href);
 289              }
 290              
 291              // generate an image tag
 292              $output = "<img src=\"$href\" alt=\"$text\" />";
 293              
 294          } else {
 295              
 296              // allow for alternative targets
 297              if (isset($this->_conf['target']) &&
 298                  trim($this->_conf['target']) != '') {
 299                  $target = 'target="' . $this->_conf['target'] . '"';
 300              } else {
 301                  $target = '';
 302              }
 303              
 304              // generate a regular link (not an image)
 305              $output = "<a $target href=\"$href\">$text</a>";
 306              
 307              // make numbered references look like footnotes
 308              if ($type == 'footnote') {
 309                  $output = '<sup>' . $output . '</sup>';
 310              }
 311          }
 312          
 313          return $output;
 314      }
 315  }
 316  ?>


Généré le : Sat Nov 24 09:00:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics