wikilink.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.3 2004/01/31 15:52:08 pmjones Exp $
  20. /**
  21. *
  22. * This class implements a Text_Wiki_Rule to find source text marked as a
  23. * wiki page name and automatically create a link to that page.
  24. *
  25. * Wiki page names are typically in StudlyCapsStyle made of
  26. * WordsSmashedTogether.
  27. *
  28. * @author Paul M. Jones <pmjones@ciaweb.net>
  29. *
  30. * @package Text_Wiki
  31. *
  32. */
  33. class Text_Wiki_Rule_wikilink extends Text_Wiki_Rule {
  34. /**
  35. *
  36. * Constructor. We override the Text_Wiki_Rule constructor so we can
  37. * explicitly comment each part of the $regex property.
  38. *
  39. * @access public
  40. *
  41. * @param object &$obj The calling "parent" Text_Wiki object.
  42. *
  43. * @param string $name The token name to use for this rule.
  44. *
  45. */
  46. function Text_Wiki_Rule_wikilink(&$obj, $name)
  47. {
  48. parent::Text_Wiki_Rule($obj, $name);
  49. $this->regex =
  50. "/" . // START regex
  51. "(^|[^A-Za-z])" . //
  52. "(!?" . // START WikiPage pattern
  53. "[A-Z\xc0-\xde]" . // 1 upper
  54. "[A-Za-z\xc0-\xfe]*" . // 0 or more alpha
  55. "[a-z\xdf-\xfe]+" . // 1 or more lower
  56. "[A-Z\xc0-\xde]" . // 1 upper
  57. "[A-Za-z\xc0-\xfe]*" . // 0 or more alpha
  58. '(\\/' . // start subpattern
  59. "[A-Z\xc0-\xde]" . // 1 upper
  60. "[A-Za-z\xc0-\xfe]*" . // 0 or more alpha
  61. ')?' . // end subpattern 0 or 1
  62. ")" . // END WikiPage pattern
  63. "((\#" . // START Anchor pattern
  64. "[A-Za-z]" . // 1 alpha
  65. "(" . // start sub pattern
  66. "[-A-Za-z0-9_:.]*" . // 0+ dash, alpha, digit, underscore
  67. "[-A-Za-z0-9_]" . // 1 dash, alpha, digit, or underscore
  68. ")?" . // end subpattern 0 or 1
  69. ")?)" . // END Anchor pattern
  70. "(\"\")?" . //
  71. "/"; // END regex
  72. }
  73. /**
  74. *
  75. * Generates a replacement for the matched text. Token options are:
  76. *
  77. * 'page' => the wiki page name (e.g., HomePage).
  78. *
  79. * 'anchor' => a named anchor on the target wiki page
  80. *
  81. * @access public
  82. *
  83. * @param array &$matches The array of matches from parse().
  84. *
  85. * @return A delimited token to be used as a placeholder in
  86. * the source text, plus any text priot to the match.
  87. *
  88. */
  89. function process(&$matches)
  90. {
  91. // when prefixed with !, it's explicitly not a wiki link.
  92. // return everything as it was.
  93. if ($matches[2]{0} == '!') {
  94. return $matches[1] . substr($matches[2], 1) .
  95. $matches[3] . $matches[4];
  96. }
  97. // set the options
  98. $options = array(
  99. 'page' => $matches[2],
  100. 'anchor' => substr($matches[4], 1)
  101. );
  102. // create and return the replacement token and preceding text
  103. return $matches[1] . $this->addToken($options);
  104. }
  105. /**
  106. *
  107. * Renders a token into text matching the requested format.
  108. *
  109. * @access public
  110. *
  111. * @param array $options The "options" portion of the token (second
  112. * element).
  113. *
  114. * @return string The text rendered from the token options.
  115. *
  116. */
  117. function renderXhtml($options)
  118. {
  119. // make nice variable names (page, anchor)
  120. extract($options);
  121. // does the page exist?
  122. if (in_array($page, $this->_wiki->pages)) {
  123. // yes, link to the page view
  124. $href = $this->_wiki->view_url . $page . "#" . $anchor;
  125. return "<a href=\"$href\">$page</a>";
  126. } else {
  127. // no, link to a create-page url
  128. $href = $this->_wiki->new_url;
  129. return $page . "<a href=\"$href$page\">{$this->_wiki->new_text}</a>";
  130. }
  131. }
  132. }
  133. ?>