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

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