[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/bundled-libs/Text/Wiki/Rule/ -> wikilink.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: wikilink.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 a
  25  * wiki page name and automatically create a link to that page.
  26  *
  27  * Wiki page names are typically in StudlyCapsStyle made of
  28  * WordsSmashedTogether.
  29  *
  30  * You can also create described links to pages in this style:
  31  * [WikiPageName nice text link to use for display]
  32  *
  33  * @author Paul M. Jones <pmjones@ciaweb.net>
  34  *
  35  * @package Text_Wiki
  36  *
  37  */
  38  
  39  class Text_Wiki_Rule_wikilink extends Text_Wiki_Rule {
  40      
  41      
  42      /**
  43      * 
  44      * Constructor.  We override the Text_Wiki_Rule constructor so we can
  45      * explicitly comment each part of the $regex property.
  46      * 
  47      * @access public
  48      * 
  49      * @param object &$obj The calling "parent" Text_Wiki object.
  50      * 
  51      * @param string $name The token name to use for this rule.
  52      * 
  53      */
  54      
  55      function Text_Wiki_Rule_wikilink(&$obj, $name)
  56      {
  57          parent::Text_Wiki_Rule($obj, $name);
  58          
  59          $this->regex =
  60              "(!?" .              // START WikiPage pattern (1)
  61              "[A-Z]" .            // 1 upper
  62              "[A-Za-z]*" .        // 0+ alpha
  63              "[a-z]+" .           // 1+ lower
  64              "[A-Z]" .            // 1 upper
  65              "[A-Za-z]*" .        // 0+ or more alpha
  66              ")" .                // END WikiPage pattern (/1)
  67              "((\#" .             // START Anchor pattern (2)(3)
  68              "[A-Za-z]" .         // 1 alpha
  69              "(" .                // start sub pattern (4)
  70              "[-A-Za-z0-9_:.]*" . // 0+ dash, alpha, digit, underscore, colon, dot
  71              "[-A-Za-z0-9_]" .    // 1 dash, alpha, digit, or underscore
  72              ")?)?)";             // end subpatterns (/4)(/3)(/2)
  73      }
  74      
  75      
  76      /**
  77      * 
  78      * First parses for described links, then for standalone links.
  79      * 
  80      * @access public
  81      * 
  82      * @return void
  83      * 
  84      */
  85      
  86      function parse()
  87      {
  88          // described wiki links
  89          $tmp_regex = '/\[' . $this->regex . ' (.+?)\]/';
  90          $this->_wiki->_source = preg_replace_callback(
  91              $tmp_regex,
  92              array(&$this, 'processDescr'),
  93              $this->_wiki->_source
  94          );
  95          
  96          // standalone wiki links
  97          $tmp_regex = '/(^|[^A-Za-z0-9\-_])' . $this->regex . '/';
  98          $this->_wiki->_source = preg_replace_callback(
  99              $tmp_regex,
 100              array(&$this, 'process'),
 101              $this->_wiki->_source
 102          );
 103      }
 104      
 105      
 106      /**
 107      * 
 108      * Generates a replacement for described links.  Token options are:
 109      * 
 110      * 'page' => the wiki page name.
 111      * 
 112      * 'text' => the displayed link text.
 113      * 
 114      * 'anchor' => a named anchor on the target wiki page.
 115      * 
 116      * @access public
 117      *
 118      * @param array &$matches The array of matches from parse().
 119      *
 120      * @return A delimited token to be used as a placeholder in
 121      * the source text, plus any text priot to the match.
 122      *
 123      */
 124      
 125      function processDescr(&$matches)
 126      {
 127          // set the options
 128          $options = array(
 129              'page' => $matches[1],
 130              'text' => $matches[5],
 131              'anchor' => $matches[3]
 132          );
 133          
 134          // create and return the replacement token and preceding text
 135          return $this->addToken($options); // . $matches[7];
 136      }
 137      
 138      
 139      /**
 140      * 
 141      * Generates a replacement for standalone links.  Token options are:
 142      * 
 143      * 'page' => the wiki page name.
 144      * 
 145      * 'text' => the displayed link text.
 146      * 
 147      * 'anchor' => a named anchor on the target wiki page.
 148      * 
 149      * @access public
 150      *
 151      * @param array &$matches The array of matches from parse().
 152      *
 153      * @return A delimited token to be used as a placeholder in
 154      * the source text, plus any text prior to the match.
 155      *
 156      */
 157      
 158      function process(&$matches)
 159      {
 160          // when prefixed with !, it's explicitly not a wiki link.
 161          // return everything as it was.
 162          if ($matches[2]{0} == '!') {
 163              return $matches[1] . substr($matches[2], 1) . $matches[3];
 164          }
 165          
 166          // set the options
 167          $options = array(
 168              'page' => $matches[2],
 169              'text' => $matches[2] . $matches[3],
 170              'anchor' => $matches[3]
 171          );
 172          
 173          // create and return the replacement token and preceding text
 174          return $matches[1] . $this->addToken($options);
 175      }
 176      
 177      
 178      /**
 179      * 
 180      * Renders a token into text matching the requested format.
 181      * 
 182      * @access public
 183      * 
 184      * @param array $options The "options" portion of the token (second
 185      * element).
 186      * 
 187      * @return string The text rendered from the token options.
 188      * 
 189      */
 190      
 191      function renderXhtml($options)
 192      {
 193          // make nice variable names (page, anchor, text)
 194          extract($options);
 195          
 196          // does the page exist?
 197          if (in_array($page, $this->_conf['pages'])) {
 198          
 199              // yes, link to the page view, but we have to build
 200              // the HREF.  we support both the old form where
 201              // the page always comes at the end, and the new
 202              // form that uses %s for sprintf()
 203              $href = $this->_conf['view_url'];
 204              
 205              if (strpos($href, '%s') === false) {
 206                  // use the old form
 207                  $href = $href . $page . $anchor;
 208              } else {
 209                  // use the new form
 210                  $href = sprintf($href, $page . $anchor);
 211              }
 212              
 213              return "<a href=\"$href\">$text</a>";
 214              
 215          }
 216          
 217          // no, link to a create-page url, but only if new_url is set
 218          if (! isset($this->_conf['new_url']) ||
 219              trim($this->_conf['new_url']) == '') {
 220              return $text;
 221          } else {
 222          
 223              // yes, link to the page view, but we have to build
 224              // the HREF.  we support both the old form where
 225              // the page always comes at the end, and the new
 226              // form that uses sprintf()
 227              $href = $this->_conf['new_url'];
 228              
 229              if (strpos($href, '%s') === false) {
 230                  // use the old form
 231                  $href = $href . $page;
 232              } else {
 233                  // use the new form
 234                  $href = sprintf($href, $page);
 235              }
 236              
 237              return $text . "<a href=\"$href\">{$this->_conf['new_text']}</a>";
 238          }
 239      }
 240  }
 241  ?>


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