Text_Wiki
[ class tree: Text_Wiki ] [ index: Text_Wiki ] [ all elements ]

Source for file freelink.php

Documentation is available at freelink.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: freelink.php,v 1.3 2004/01/31 15:52:08 pmjones Exp $
  20.  
  21.  
  22. /**
  23. * This class implements a Text_Wiki_Rule to find source text marked as a
  24. * wiki freelink, and automatically create a link to that page.
  25. * A freelink is any page name not conforming to the standard
  26. * StudlyCapsStyle for a wiki page name.  For example, a page normally
  27. * named MyHomePage can be renamed and referred to as ((My Home Page)) --
  28. * note the spaces in the page name.  You can also make a "nice-looking"
  29. * link without renaming the target page; e.g., ((MyHomePage|My Home
  30. * Page)).  Finally, you can use named anchors on the target page:
  31. * ((MyHomePage|My Home Page#Section1)).
  32. *
  33. @author Paul M. Jones <pmjones@ciaweb.net>
  34. *
  35. @package Text_Wiki
  36. *
  37. */
  38.  
  39.     
  40.     
  41.     /**
  42.     * 
  43.     * Constructor.  We override the Text_Wiki_Rule constructor so we can
  44.     * explicitly comment each part of the $regex property.
  45.     * 
  46.     * @access public
  47.     * 
  48.     * @param object &$obj The calling "parent" Text_Wiki object.
  49.     * 
  50.     * @param string $name The token name to use for this rule.
  51.     * 
  52.     */
  53.     
  54.     function Text_Wiki_Rule_freelink(&$obj$name)
  55.     {
  56.         parent::Text_Wiki_Rule($obj$name);
  57.         
  58.         $this->regex =
  59.             '/' .                                                  // START regex
  60.             "\\(\\(" .                                             // double open-parens
  61.             "(" .                                                  // START freelink page patter
  62.             "[-A-Za-z0-9 _+\\/.,;:!?'\"\\[\\]\\{\\}&\xc0-\xff]+" // 1 or more of just about any character
  63.             ")" .                                                  // END  freelink page pattern
  64.             "(" .                                                  // START display-name
  65.             "\|" .                                                 // a pipe to start the display name
  66.             "[-A-Za-z0-9 _+\\/.,;:!?'\"\\[\\]\\{\\}&\xc0-\xff]+" // 1 or more of just about any character
  67.             ")?" .                                                 // END display-name pattern 0 or 1
  68.             "(" .                                                  // START pattern for named anchors
  69.             "\#" .                                                 // a hash mark
  70.             "[A-Za-z]" .                                           // 1 alpha
  71.             "[-A-Za-z0-9_:.]*" .                                   // 0 or more alpha, digit, underscore
  72.             ")?" .                                                 // END named anchors pattern 0 or 1
  73.             "()\\)\\)" .                                           // double close-parens
  74.             '/';                                                   // END regex
  75.     }
  76.     
  77.     
  78.     /**
  79.     * 
  80.     * Generates a replacement for the matched text.  Token options are:
  81.     * 
  82.     * 'page' => the wiki page name (e.g., HomePage).
  83.     * 
  84.     * 'text' => alternative text to be displayed in place of the wiki
  85.     * page name.
  86.     * 
  87.     * 'anchor' => a named anchor on the target wiki page
  88.     * 
  89.     * @access public
  90.     *
  91.     * @param array &$matches The array of matches from parse().
  92.     *
  93.     * @return delimited token to be used as a placeholder in
  94.     *  the source text, plus any text priot to the match.
  95.     *
  96.     */
  97.     
  98.     function process(&$matches)
  99.     {
  100.         // use nice variable names
  101.         $page $matches[1];
  102.         $text $matches[2];
  103.         
  104.         // get rid of the leading # from the anchor, if any
  105.         $anchor substr($matches[3]1);
  106.         
  107.         // is the page given a new text appearance?
  108.         if (trim($text== ''{
  109.             // no
  110.             $text $page;
  111.         else {
  112.             // yes, strip the leading | character
  113.             $text substr($text1);
  114.         }
  115.         
  116.         // set the options
  117.         $options = array(
  118.             'page'   => $page,
  119.             'text'   => $text,
  120.             'anchor' => $anchor
  121.         );
  122.         
  123.         // return a token placeholder
  124.         return $this->addToken($options);
  125.     }
  126.     
  127.     
  128.     /**
  129.     * 
  130.     * Renders a token into text matching the requested format.
  131.     * 
  132.     * @access public
  133.     * 
  134.     * @param array $options The "options" portion of the token (second
  135.     *  element).
  136.     * 
  137.     * @return string The text rendered from the token options.
  138.     * 
  139.     */
  140.     
  141.     function renderXhtml($options)
  142.     {
  143.         // get nice variable names (page, text, anchor)
  144.         extract($options);
  145.         
  146.         if (in_array($page$this->_wiki->pages)) {
  147.             // the page exists, show a link to the page
  148.             $href $this->_wiki->view_url . $page "#" $anchor;
  149.             return "<a href=\"$href\">$text</a>";
  150.         else {
  151.             // the page does not exist, show the page name and
  152.             // the "new page" text
  153.             $href $this->_wiki->new_url;
  154.             return $text . "<a href=\"$href$page\">{$this->_wiki->new_text}</a>";
  155.         }
  156.     }
  157. }
  158. ?>

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