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

Source for file wikilink.php

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

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