[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/bundled-libs/Text/Wiki/Rule/ -> interwiki.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: interwiki.php,v 1.3 2004/12/02 10:54:32 nohn Exp $
  20  
  21  
  22  /**
  23  * 
  24  * This class implements a Text_Wiki_Rule to find source text marked as
  25  * an Interwiki link.  See the regex for a detailed explanation of the
  26  * text matching procedure; e.g., "InterWikiName:PageName".
  27  *
  28  * @author Paul M. Jones <pmjones@ciaweb.net>
  29  *
  30  * @package Text_Wiki
  31  *
  32  */
  33  
  34  class Text_Wiki_Rule_interwiki extends Text_Wiki_Rule {
  35      
  36      
  37      var $regex = '([A-Za-z0-9_]+):([\/=&~#A-Za-z0-9_]+)';
  38      
  39      
  40      /**
  41      * 
  42      * Parser.  We override the standard parser so we can
  43      * find both described interwiki links and standalone links.
  44      * 
  45      * @access public
  46      * 
  47      * @return void
  48      * 
  49      */
  50      
  51      function parse()
  52      {
  53          // described interwiki links
  54          $tmp_regex = '/\[' . $this->regex . ' (.+?)\]/';
  55          $this->_wiki->_source = preg_replace_callback(
  56              $tmp_regex,
  57              array(&$this, 'processDescr'),
  58              $this->_wiki->_source
  59          );
  60          
  61          // standalone interwiki links
  62          $tmp_regex = '/' . $this->regex . '/';
  63          $this->_wiki->_source = preg_replace_callback(
  64              $tmp_regex,
  65              array(&$this, 'process'),
  66              $this->_wiki->_source
  67          );
  68         
  69      }
  70      
  71      
  72      /**
  73      * 
  74      * Generates a replacement for the matched standalone interwiki text.
  75      * Token options are:
  76      * 
  77      * 'site' => The key name for the Text_Wiki interwiki array map,
  78      * usually the name of the interwiki site.
  79      * 
  80      * 'page' => The page on the target interwiki to link to.
  81      * 
  82      * 'text' => The text to display as the link.
  83      * 
  84      * @access public
  85      *
  86      * @param array &$matches The array of matches from parse().
  87      *
  88      * @return A delimited token to be used as a placeholder in
  89      * the source text, plus any text priot to the match.
  90      *
  91      */
  92      
  93      function process(&$matches)
  94      {
  95          $options = array(
  96              'site' => $matches[1],
  97              'page' => $matches[2],
  98              'text' => $matches[0]
  99          );
 100          
 101          // if not in the interwiki map, don't make it an interwiki link
 102          if (isset($this->_conf['sites'][$options['site']])) {
 103              return $this->addToken($options);
 104          } else {
 105              return $matches[0];
 106          }
 107      }
 108      
 109      
 110      /**
 111      * 
 112      * Generates a replacement for described interwiki links. Token
 113      * options are:
 114      * 
 115      * 'site' => The key name for the Text_Wiki interwiki array map,
 116      * usually the name of the interwiki site.
 117      * 
 118      * 'page' => The page on the target interwiki to link to.
 119      * 
 120      * 'text' => The text to display as the link.
 121      * 
 122      * @access public
 123      *
 124      * @param array &$matches The array of matches from parse().
 125      *
 126      * @return A delimited token to be used as a placeholder in
 127      * the source text, plus any text priot to the match.
 128      *
 129      */
 130      
 131      function processDescr(&$matches)
 132      {
 133          $options = array(
 134              'site' => $matches[1],
 135              'page' => $matches[2],
 136              'text' => $matches[3]
 137          );
 138          
 139          // if not in the interwiki map, don't make it an interwiki link
 140          if (isset($this->_conf['sites'][$options['site']])) {
 141              return $this->addToken($options);
 142          } else {
 143              return $matches[0];
 144          }
 145      }
 146      
 147      
 148      /**
 149      * 
 150      * Renders a token into text matching the requested format.
 151      * 
 152      * @access public
 153      * 
 154      * @param array $options The "options" portion of the token (second
 155      * element).
 156      * 
 157      * @return string The text rendered from the token options.
 158      * 
 159      */
 160      
 161      function renderXhtml($options)
 162      {
 163          $site = $options['site'];
 164          $page = $options['page'];
 165          $text = $options['text'];
 166          
 167          if (isset($this->_conf['sites'][$site])) {
 168              $href = $this->_conf['sites'][$site];
 169          } else {
 170              return $text;
 171          }
 172          
 173          // old form where page is at end,
 174          // or new form with %s placeholder for sprintf()?
 175          if (strpos($href, '%s') === false) {
 176              // use the old form
 177              $href = $href . $page;
 178          } else {
 179              // use the new form
 180              $href = sprintf($href, $page);
 181          }
 182          
 183          // allow for alternative targets
 184          if (isset($this->_conf['target']) &&
 185              trim($this->_conf['target']) != '') {
 186              $target = 'target="' . $this->_conf['target'] . '"';
 187          } else {
 188              $target = '';
 189          }
 190          
 191          
 192          return "<a $target href=\"$href\">$text</a>";
 193      }
 194  }
 195  ?>


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