freelink.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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: freelink.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 freelink, and automatically create a link to that page.
  24. *
  25. * A freelink is any page name not conforming to the standard
  26. * StudlyCapsStyle for a wiki page name. For example, a page normally
  27. * named MyHomePage can be renamed and referred to as ((My Home Page)) --
  28. * note the spaces in the page name. You can also make a "nice-looking"
  29. * link without renaming the target page; e.g., ((MyHomePage|My Home
  30. * Page)). Finally, you can use named anchors on the target page:
  31. * ((MyHomePage|My Home Page#Section1)).
  32. *
  33. * @author Paul M. Jones <pmjones@ciaweb.net>
  34. *
  35. * @package Text_Wiki
  36. *
  37. */
  38. class Text_Wiki_Rule_freelink extends Text_Wiki_Rule {
  39. /**
  40. *
  41. * Constructor. We override the Text_Wiki_Rule constructor so we can
  42. * explicitly comment each part of the $regex property.
  43. *
  44. * @access public
  45. *
  46. * @param object &$obj The calling "parent" Text_Wiki object.
  47. *
  48. * @param string $name The token name to use for this rule.
  49. *
  50. */
  51. function Text_Wiki_Rule_freelink(&$obj, $name)
  52. {
  53. parent::Text_Wiki_Rule($obj, $name);
  54. $this->regex =
  55. '/' . // START regex
  56. "\\(\\(" . // double open-parens
  57. "(" . // START freelink page patter
  58. "[-A-Za-z0-9 _+\\/.,;:!?'\"\\[\\]\\{\\}&\xc0-\xff]+" . // 1 or more of just about any character
  59. ")" . // END freelink page pattern
  60. "(" . // START display-name
  61. "\|" . // a pipe to start the display name
  62. "[-A-Za-z0-9 _+\\/.,;:!?'\"\\[\\]\\{\\}&\xc0-\xff]+" . // 1 or more of just about any character
  63. ")?" . // END display-name pattern 0 or 1
  64. "(" . // START pattern for named anchors
  65. "\#" . // a hash mark
  66. "[A-Za-z]" . // 1 alpha
  67. "[-A-Za-z0-9_:.]*" . // 0 or more alpha, digit, underscore
  68. ")?" . // END named anchors pattern 0 or 1
  69. "()\\)\\)" . // double close-parens
  70. '/'; // END regex
  71. }
  72. /**
  73. *
  74. * Generates a replacement for the matched text. Token options are:
  75. *
  76. * 'page' => the wiki page name (e.g., HomePage).
  77. *
  78. * 'text' => alternative text to be displayed in place of the wiki
  79. * page name.
  80. *
  81. * 'anchor' => a named anchor on the target wiki page
  82. *
  83. * @access public
  84. *
  85. * @param array &$matches The array of matches from parse().
  86. *
  87. * @return A delimited token to be used as a placeholder in
  88. * the source text, plus any text priot to the match.
  89. *
  90. */
  91. function process(&$matches)
  92. {
  93. // use nice variable names
  94. $page = $matches[1];
  95. $text = $matches[2];
  96. // get rid of the leading # from the anchor, if any
  97. $anchor = substr($matches[3], 1);
  98. // is the page given a new text appearance?
  99. if (trim($text) == '') {
  100. // no
  101. $text = $page;
  102. } else {
  103. // yes, strip the leading | character
  104. $text = substr($text, 1);
  105. }
  106. // set the options
  107. $options = array(
  108. 'page' => $page,
  109. 'text' => $text,
  110. 'anchor' => $anchor
  111. );
  112. // return a token placeholder
  113. return $this->addToken($options);
  114. }
  115. /**
  116. *
  117. * Renders a token into text matching the requested format.
  118. *
  119. * @access public
  120. *
  121. * @param array $options The "options" portion of the token (second
  122. * element).
  123. *
  124. * @return string The text rendered from the token options.
  125. *
  126. */
  127. function renderXhtml($options)
  128. {
  129. // get nice variable names (page, text, anchor)
  130. extract($options);
  131. if (in_array($page, $this->_wiki->pages)) {
  132. // the page exists, show a link to the page
  133. $href = $this->_wiki->view_url . $page . "#" . $anchor;
  134. return "<a href=\"$href\">$text</a>";
  135. } else {
  136. // the page does not exist, show the page name and
  137. // the "new page" text
  138. $href = $this->_wiki->new_url;
  139. return $text . "<a href=\"$href$page\">{$this->_wiki->new_text}</a>";
  140. }
  141. }
  142. }
  143. ?>