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.  
  3. /**
  4. * Parse for links to wiki pages.
  5. *
  6. * Wiki page names are typically in StudlyCapsStyle made of
  7. * WordsSmashedTogether.
  8. *
  9. * You can also create described links to pages in this style:
  10. * [WikiPageName nice text link to use for display]
  11. *
  12. * The token options for this rule are:
  13. *
  14. * 'page' => the wiki page name.
  15. * 'text' => the displayed link text.
  16. * 'anchor' => a named anchor on the target wiki page.
  17. * $Id: Wikilink.php,v 1.2 2004/09/25 19:05:13 pmjones Exp $
  18. *
  19. @author Paul M. Jones <pmjones@ciaweb.net>
  20. *
  21. @package Text_Wiki
  22. *
  23. */
  24.  
  25.     
  26.     
  27.     /**
  28.     * 
  29.     * Constructor.
  30.     * 
  31.     * We override the Text_Wiki_Parse constructor so we can
  32.     * explicitly comment each part of the $regex property.
  33.     * 
  34.     * @access public
  35.     * 
  36.     * @param object &$obj The calling "parent" Text_Wiki object.
  37.     * 
  38.     */
  39.     
  40.     function Text_Wiki_Parse_Wikilink(&$obj)
  41.     {
  42.         parent::Text_Wiki_Parse($obj);
  43.         
  44.         // allows numbers as "lowercase letters" in the regex
  45.         $this->regex =
  46.             "(!?" .                 // START WikiPage pattern (1)
  47.             "[A-Z]" .             // 1 upper
  48.             "[A-Za-z0-9]*" .     // 0+ alpha or digit
  49.             "[a-z0-9]+" .         // 1+ lower or digit
  50.             "[A-Z]" .             // 1 upper
  51.             "[A-Za-z0-9]*" .     // 0+ or more alpha or digit
  52.             ")" .                 // END WikiPage pattern (/1)
  53.             "((\#" .             // START Anchor pattern (2)(3)
  54.             "[A-Za-z]" .         // 1 alpha
  55.             "(" .                 // start sub pattern (4)
  56.             "[-A-Za-z0-9_:.]*" // 0+ dash, alpha, digit, underscore, colon, dot
  57.             "[-A-Za-z0-9_]" .     // 1 dash, alpha, digit, or underscore
  58.             ")?)?)";             // end subpatterns (/4)(/3)(/2)
  59.         
  60.     }
  61.     
  62.     
  63.     /**
  64.     * 
  65.     * First parses for described links, then for standalone links.
  66.     * 
  67.     * @access public
  68.     * 
  69.     * @return void 
  70.     * 
  71.     */
  72.     
  73.     function parse()
  74.     {
  75.         // described wiki links
  76.         $tmp_regex '/\[' $this->regex . ' (.+?)\]/';
  77.         $this->wiki->source = preg_replace_callback(
  78.             $tmp_regex,
  79.             array(&$this'processDescr'),
  80.             $this->wiki->source
  81.         );
  82.         
  83.         // standalone wiki links
  84.         $tmp_regex '/(^|[^A-Za-z0-9\-_])' $this->regex . '/';
  85.         $this->wiki->source = preg_replace_callback(
  86.             $tmp_regex,
  87.             array(&$this'process'),
  88.             $this->wiki->source
  89.         );
  90.     }
  91.     
  92.     
  93.     /**
  94.     * 
  95.     * Generate a replacement for described links.
  96.     * 
  97.     * @access public
  98.     *
  99.     * @param array &$matches The array of matches from parse().
  100.     *
  101.     * @return delimited token to be used as a placeholder in
  102.     *  the source text, plus any text priot to the match.
  103.     *
  104.     */
  105.     
  106.     function processDescr(&$matches)
  107.     {
  108.         // set the options
  109.         $options = array(
  110.             'page'   => $matches[1],
  111.             'text'   => $matches[5],
  112.             'anchor' => $matches[3]
  113.         );
  114.         
  115.         // create and return the replacement token and preceding text
  116.         return $this->wiki->addToken($this->rule$options)// . $matches[7];
  117.     }
  118.     
  119.     
  120.     /**
  121.     * 
  122.     * Generate a replacement for standalone links.
  123.     * 
  124.     * 
  125.     * @access public
  126.     *
  127.     * @param array &$matches The array of matches from parse().
  128.     *
  129.     * @return delimited token to be used as a placeholder in
  130.     *  the source text, plus any text prior to the match.
  131.     *
  132.     */
  133.     
  134.     function process(&$matches)
  135.     {
  136.         // when prefixed with !, it's explicitly not a wiki link.
  137.         // return everything as it was.
  138.         if ($matches[2]{0== '!'{
  139.             return $matches[1substr($matches[2]1$matches[3];
  140.         }
  141.         
  142.         // set the options
  143.         $options = array(
  144.             'page' => $matches[2],
  145.             'text' => $matches[2$matches[3],
  146.             'anchor' => $matches[3]
  147.         );
  148.         
  149.         // create and return the replacement token and preceding text
  150.         return $matches[1$this->wiki->addToken($this->rule$options);
  151.     }
  152. }
  153. ?>

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