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.5 2004/05/25 01:17:27 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.         if (isset($this->_conf['numbers']&&
  58.             $this->_conf['numbers']{
  59.         
  60.             // allow numbers as "lowercase letters" in the regex
  61.             $this->regex =
  62.                 "(!?" .              // START WikiPage pattern (1)
  63.                 "[A-Z]" .            // 1 upper
  64.                 "[A-Za-z0-9]*" .     // 0+ alpha or digit
  65.                 "[a-z0-9]+" .        // 1+ lower or digit
  66.                 "[A-Z]" .            // 1 upper
  67.                 "[A-Za-z0-9]*" .     // 0+ or more alpha or digit
  68.                 ")" .                // END WikiPage pattern (/1)
  69.                 "((\#" .             // START Anchor pattern (2)(3)
  70.                 "[A-Za-z]" .         // 1 alpha
  71.                 "(" .                // start sub pattern (4)
  72.                 "[-A-Za-z0-9_:.]*" // 0+ dash, alpha, digit, underscore, colon, dot
  73.                 "[-A-Za-z0-9_]" .    // 1 dash, alpha, digit, or underscore
  74.                 ")?)?)";             // end subpatterns (/4)(/3)(/2)
  75.         
  76.         else {
  77.             
  78.             // no numbers allowed in the regex
  79.             $this->regex =
  80.                 "(!?" .              // START WikiPage pattern (1)
  81.                 "[A-Z]" .            // 1 upper
  82.                 "[A-Za-z]*" .        // 0+ alpha
  83.                 "[a-z]+" .           // 1+ lower
  84.                 "[A-Z]" .            // 1 upper
  85.                 "[A-Za-z]*" .        // 0+ or more alpha
  86.                 ")" .                // END WikiPage pattern (/1)
  87.                 "((\#" .             // START Anchor pattern (2)(3)
  88.                 "[A-Za-z]" .         // 1 alpha
  89.                 "(" .                // start sub pattern (4)
  90.                 "[-A-Za-z0-9_:.]*" // 0+ dash, alpha, digit, underscore, colon, dot
  91.                 "[-A-Za-z0-9_]" .    // 1 dash, alpha, digit, or underscore
  92.                 ")?)?)";             // end subpatterns (/4)(/3)(/2)
  93.         }
  94.     }
  95.     
  96.     
  97.     /**
  98.     * 
  99.     * First parses for described links, then for standalone links.
  100.     * 
  101.     * @access public
  102.     * 
  103.     * @return void 
  104.     * 
  105.     */
  106.     
  107.     function parse()
  108.     {
  109.         // described wiki links
  110.         $tmp_regex '/\[' $this->regex . ' (.+?)\]/';
  111.         $this->_wiki->_source = preg_replace_callback(
  112.             $tmp_regex,
  113.             array(&$this'processDescr'),
  114.             $this->_wiki->_source
  115.         );
  116.         
  117.         // standalone wiki links
  118.         $tmp_regex '/(^|[^A-Za-z0-9\-_])' $this->regex . '/';
  119.         $this->_wiki->_source = preg_replace_callback(
  120.             $tmp_regex,
  121.             array(&$this'process'),
  122.             $this->_wiki->_source
  123.         );
  124.     }
  125.     
  126.     
  127.     /**
  128.     * 
  129.     * Generates a replacement for described links.  Token options are:
  130.     * 
  131.     * 'page' => the wiki page name.
  132.     * 
  133.     * 'text' => the displayed link text.
  134.     * 
  135.     * 'anchor' => a named anchor on the target wiki page.
  136.     * 
  137.     * @access public
  138.     *
  139.     * @param array &$matches The array of matches from parse().
  140.     *
  141.     * @return delimited token to be used as a placeholder in
  142.     *  the source text, plus any text priot to the match.
  143.     *
  144.     */
  145.     
  146.     function processDescr(&$matches)
  147.     {
  148.         // set the options
  149.         $options = array(
  150.             'page' => $matches[1],
  151.             'text' => $matches[5],
  152.             'anchor' => $matches[3]
  153.         );
  154.         
  155.         // create and return the replacement token and preceding text
  156.         return $this->addToken($options)// . $matches[7];
  157.     }
  158.     
  159.     
  160.     /**
  161.     * 
  162.     * Generates a replacement for standalone links.  Token options are:
  163.     * 
  164.     * 'page' => the wiki page name.
  165.     * 
  166.     * 'text' => the displayed link text.
  167.     * 
  168.     * 'anchor' => a named anchor on the target wiki page.
  169.     * 
  170.     * @access public
  171.     *
  172.     * @param array &$matches The array of matches from parse().
  173.     *
  174.     * @return delimited token to be used as a placeholder in
  175.     *  the source text, plus any text prior to the match.
  176.     *
  177.     */
  178.     
  179.     function process(&$matches)
  180.     {
  181.         // when prefixed with !, it's explicitly not a wiki link.
  182.         // return everything as it was.
  183.         if ($matches[2]{0== '!'{
  184.             return $matches[1substr($matches[2]1$matches[3];
  185.         }
  186.         
  187.         // set the options
  188.         $options = array(
  189.             'page' => $matches[2],
  190.             'text' => $matches[2$matches[3],
  191.             'anchor' => $matches[3]
  192.         );
  193.         
  194.         // create and return the replacement token and preceding text
  195.         return $matches[1$this->addToken($options);
  196.     }
  197.     
  198.     
  199.     /**
  200.     * 
  201.     * Renders a token into XHTML.
  202.     * 
  203.     * @access public
  204.     * 
  205.     * @param array $options The "options" portion of the token (second
  206.     *  element).
  207.     * 
  208.     * @return string The text rendered from the token options.
  209.     * 
  210.     */
  211.     
  212.     function renderXhtml($options)
  213.     {
  214.         // make nice variable names (page, anchor, text)
  215.         extract($options);
  216.         
  217.         // does the page exist?
  218.         if (in_array($page$this->_conf['pages'])) {
  219.         
  220.             // yes, link to the page view, but we have to build
  221.             // the HREF.  we support both the old form where
  222.             // the page always comes at the end, and the new
  223.             // form that uses %s for sprintf()
  224.             $href $this->_conf['view_url'];
  225.             
  226.             if (strpos($href'%s'=== false{
  227.                 // use the old form
  228.                 $href $href $page $anchor;
  229.             else {
  230.                 // use the new form
  231.                 $href sprintf($href$page $anchor);
  232.             }
  233.             
  234.             return "<a href=\"$href\">$text</a>";
  235.             
  236.         }
  237.         
  238.         // no, link to a create-page url, but only if new_url is set
  239.         if (isset($this->_conf['new_url']||
  240.             trim($this->_conf['new_url']== ''{
  241.             return $text;
  242.         else {
  243.         
  244.             // yes, link to the page view, but we have to build
  245.             // the HREF.  we support both the old form where
  246.             // the page always comes at the end, and the new
  247.             // form that uses sprintf()
  248.             $href $this->_conf['new_url'];
  249.             
  250.             if (strpos($href'%s'=== false{
  251.                 // use the old form
  252.                 $href $href $page;
  253.             else {
  254.                 // use the new form
  255.                 $href sprintf($href$page);
  256.             }
  257.             
  258.             return $text . "<a href=\"$href\">{$this->_conf['new_text']}</a>";
  259.         }
  260.     }
  261. }
  262. ?>

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