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 / Wikilink.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  5.1 KB  |  205 lines

  1. <?php
  2.  
  3. /**
  4. *
  5. * Parse for links to wiki pages.
  6. *
  7. * @category Text
  8. *
  9. * @package Text_Wiki
  10. *
  11. * @author Paul M. Jones <pmjones@php.net>
  12. *
  13. * @license LGPL
  14. *
  15. * @version $Id: Wikilink.php,v 1.9 2006/12/08 08:23:51 justinpatrin Exp $
  16. *
  17. */
  18.  
  19. /**
  20. *
  21. * Parse for links to wiki pages.
  22. *
  23. * Wiki page names are typically in StudlyCapsStyle made of
  24. * WordsSmashedTogether.
  25. *
  26. * You can also create described links to pages in this style:
  27. * [WikiPageName nice text link to use for display]
  28. *
  29. * The token options for this rule are:
  30. *
  31. * 'page' => the wiki page name.
  32. *
  33. * 'text' => the displayed link text.
  34. *
  35. * 'anchor' => a named anchor on the target wiki page.
  36. *
  37. * @category Text
  38. *
  39. * @package Text_Wiki
  40. *
  41. * @author Paul M. Jones <pmjones@php.net>
  42. *
  43. */
  44.  
  45. class Text_Wiki_Parse_Wikilink extends Text_Wiki_Parse {
  46.  
  47.     var $conf = array (
  48.                        'ext_chars' => false,
  49.                        'utf-8' => false
  50.     );
  51.  
  52.     /**
  53.     *
  54.     * Constructor.
  55.     *
  56.     * We override the Text_Wiki_Parse constructor so we can
  57.     * explicitly comment each part of the $regex property.
  58.     *
  59.     * @access public
  60.     *
  61.     * @param object &$obj The calling "parent" Text_Wiki object.
  62.     *
  63.     */
  64.  
  65.     function Text_Wiki_Parse_Wikilink(&$obj)
  66.     {
  67.         parent::Text_Wiki_Parse($obj);
  68.  
  69.         if ($this->getConf('utf-8')) {
  70.             $upper = 'A-Z\p{Lu}';
  71.             $lower = 'a-z0-9\p{Ll}';
  72.             $either = 'A-Za-z0-9\p{L}';
  73.         } else if ($this->getConf('ext_chars')) {
  74.             // use an extended character set; this should
  75.             // allow for umlauts and so on.  taken from the
  76.             // Tavi project defaults.php file.
  77.             $upper = 'A-Z\xc0-\xde';
  78.             $lower = 'a-z0-9\xdf-\xfe';
  79.             $either = 'A-Za-z0-9\xc0-\xfe';
  80.         } else {
  81.             // the default character set, should be fine
  82.             // for most purposes.
  83.             $upper = "A-Z";
  84.             $lower = "a-z0-9";
  85.             $either = "A-Za-z0-9";
  86.         }
  87.  
  88.         // build the regular expression for finding WikiPage names.
  89.         $this->regex =
  90.             "(!?" .            // START WikiPage pattern (1)
  91.             "[$upper]" .       // 1 upper
  92.             "[$either]*" .     // 0+ alpha or digit
  93.             "[$lower]+" .      // 1+ lower or digit
  94.             "[$upper]" .       // 1 upper
  95.             "[$either]*" .     // 0+ or more alpha or digit
  96.             ")" .              // END WikiPage pattern (/1)
  97.             "((\#" .           // START Anchor pattern (2)(3)
  98.             "[$either]" .      // 1 alpha
  99.             "(" .              // start sub pattern (4)
  100.             "[-_$either:.]*" . // 0+ dash, alpha, digit, underscore, colon, dot
  101.             "[-_$either]" .    // 1 dash, alpha, digit, or underscore
  102.             ")?)?)";           // end subpatterns (/4)(/3)(/2)
  103.     }
  104.  
  105.  
  106.     /**
  107.     *
  108.     * First parses for described links, then for standalone links.
  109.     *
  110.     * @access public
  111.     *
  112.     * @return void
  113.     *
  114.     */
  115.  
  116.     function parse()
  117.     {
  118.         // described wiki links
  119.         $tmp_regex = '/\[' . $this->regex . ' (.+?)\]/'.($this->getConf('utf-8') ? 'u' : '');
  120.         $this->wiki->source = preg_replace_callback(
  121.             $tmp_regex,
  122.             array(&$this, 'processDescr'),
  123.             $this->wiki->source
  124.         );
  125.  
  126.         // standalone wiki links
  127.         if ($this->getConf('utf-8')) {
  128.             $either = 'A-Za-z0-9\p{L}';
  129.         } else if ($this->getConf('ext_chars')) {
  130.             $either = "A-Za-z0-9\xc0-\xfe";
  131.         } else {
  132.             $either = "A-Za-z0-9";
  133.         }
  134.  
  135.         $tmp_regex = "/(^|[^{$either}\-_]){$this->regex}/".($this->getConf('utf-8') ? 'u' : '');
  136.         $this->wiki->source = preg_replace_callback(
  137.             $tmp_regex,
  138.             array(&$this, 'process'),
  139.             $this->wiki->source
  140.         );
  141.     }
  142.  
  143.  
  144.     /**
  145.     *
  146.     * Generate a replacement for described links.
  147.     *
  148.     * @access public
  149.     *
  150.     * @param array &$matches The array of matches from parse().
  151.     *
  152.     * @return A delimited token to be used as a placeholder in
  153.     * the source text, plus any text priot to the match.
  154.     *
  155.     */
  156.  
  157.     function processDescr(&$matches)
  158.     {
  159.         // set the options
  160.         $options = array(
  161.             'page'   => $matches[1],
  162.             'text'   => $matches[5],
  163.             'anchor' => $matches[3]
  164.         );
  165.  
  166.         // create and return the replacement token and preceding text
  167.         return $this->wiki->addToken($this->rule, $options); // . $matches[7];
  168.     }
  169.  
  170.  
  171.     /**
  172.     *
  173.     * Generate a replacement for standalone links.
  174.     *
  175.     *
  176.     * @access public
  177.     *
  178.     * @param array &$matches The array of matches from parse().
  179.     *
  180.     * @return A delimited token to be used as a placeholder in
  181.     * the source text, plus any text prior to the match.
  182.     *
  183.     */
  184.  
  185.     function process(&$matches)
  186.     {
  187.         // when prefixed with !, it's explicitly not a wiki link.
  188.         // return everything as it was.
  189.         if ($matches[2]{0} == '!') {
  190.             return $matches[1] . substr($matches[2], 1) . $matches[3];
  191.         }
  192.  
  193.         // set the options
  194.         $options = array(
  195.             'page' => $matches[2],
  196.             'text' => $matches[2] . $matches[3],
  197.             'anchor' => $matches[3]
  198.         );
  199.  
  200.         // create and return the replacement token and preceding text
  201.         return $matches[1] . $this->wiki->addToken($this->rule, $options);
  202.     }
  203. }
  204. ?>
  205.