home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Text / Wiki / Parse / Default / Freelink.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  4.3 KB  |  135 lines

  1. <?php
  2.  
  3. /**
  4. * Parses for wiki freelink text.
  5. * @category Text
  6. * @package Text_Wiki
  7. * @author Paul M. Jones <pmjones@php.net>
  8. * @license LGPL
  9. * @version $Id: Freelink.php,v 1.8 2006/12/08 08:23:51 justinpatrin Exp $
  10. */
  11.  
  12. /**
  13. * Parses for freelinked page links.
  14. * This class implements a Text_Wiki_Parse to find source text marked as a
  15. * wiki freelink, and automatically create a link to that page.
  16. * A freelink is any page name not conforming to the standard
  17. * StudlyCapsStyle for a wiki page name.  For example, a page normally
  18. * named MyHomePage can be renamed and referred to as ((My Home Page)) --
  19. * note the spaces in the page name.  You can also make a "nice-looking"
  20. * link without renaming the target page; e.g., ((MyHomePage|My Home
  21. * Page)).  Finally, you can use named anchors on the target page:
  22. * ((MyHomePage|My Home Page#Section1)).
  23. *
  24. * @category Text
  25. * @package Text_Wiki
  26. * @author Paul M. Jones <pmjones@php.net>
  27. */
  28.  
  29. class Text_Wiki_Parse_Freelink extends Text_Wiki_Parse {
  30.     
  31.     var $conf = array (
  32.                        'utf-8' => false
  33.     );
  34.     
  35.     /**
  36.     * 
  37.     * Constructor.  We override the Text_Wiki_Parse constructor so we can
  38.     * explicitly comment each part of the $regex property.
  39.     * 
  40.     * @access public
  41.     * 
  42.     * @param object &$obj The calling "parent" Text_Wiki object.
  43.     * 
  44.     */
  45.     
  46.     function Text_Wiki_Parse_Freelink(&$obj)
  47.     {
  48.         parent::Text_Wiki_Parse($obj);
  49.         if ($this->getConf('utf-8')) {
  50.             $any = '\p{L}';
  51.         } else {
  52.             $any = '';
  53.         }
  54.         $this->regex =
  55.             '/' .                                                   // START regex
  56.             "\\(\\(" .                                               // double open-parens
  57.             "(" .                                                   // START freelink page patter
  58.             "[-A-Za-z0-9 _+\\/.,;:!?'\"\\[\\]\\{\\}&".$any."\xc0-\xff]+" . // 1 or more of just about any character
  59.             ")" .                                                   // END  freelink page pattern
  60.             "(" .                                                   // START display-name
  61.             "\|" .                                                   // a pipe to start the display name
  62.             "[-A-Za-z0-9 _+\\/.,;:!?'\"\\[\\]\\{\\}&".$any."\xc0-\xff]+" . // 1 or more of just about any character
  63.             ")?" .                                                   // END display-name pattern 0 or 1
  64.             "(" .                                                   // START pattern for named anchors
  65.             "\#" .                                                   // a hash mark
  66.             "[A-Za-z]" .                                           // 1 alpha
  67.             "[-A-Za-z0-9_:.]*" .                                   // 0 or more alpha, digit, underscore
  68.             ")?" .                                                   // END named anchors pattern 0 or 1
  69.             "()\\)\\)" .                                           // double close-parens
  70.             '/'.($this->getConf('utf-8') ? 'u' : '');              // END regex
  71.     }
  72.     
  73.     
  74.     /**
  75.     * 
  76.     * Generates a replacement for the matched text.  Token options are:
  77.     * 
  78.     * 'page' => the wiki page name (e.g., HomePage).
  79.     * 
  80.     * 'text' => alternative text to be displayed in place of the wiki
  81.     * page name.
  82.     * 
  83.     * 'anchor' => a named anchor on the target wiki page
  84.     * 
  85.     * @access public
  86.     *
  87.     * @param array &$matches The array of matches from parse().
  88.     *
  89.     * @return A delimited token to be used as a placeholder in
  90.     * the source text, plus any text priot to the match.
  91.     *
  92.     */
  93.     
  94.     function process(&$matches)
  95.     {
  96.         // use nice variable names
  97.         $page = $matches[1];
  98.         $text = $matches[2];
  99.         $anchor = $matches[3];
  100.         
  101.         // is the page given a new text appearance?
  102.         if (trim($text) == '') {
  103.             // no
  104.             $text = $page;
  105.         } else {
  106.             // yes, strip the leading | character
  107.             $text = substr($text, 1);
  108.         }
  109.         
  110.         // set the options
  111.         $options = array(
  112.             'page'   => $page,
  113.             'text'   => $text,
  114.             'anchor' => $anchor
  115.         );
  116.         
  117.         // return a token placeholder
  118.         return $this->wiki->addToken($this->rule, $options);
  119.     }
  120. }
  121. ?>
  122.