/plugin/navi.inc.php

https://bitbucket.org/hironow/pukiwiki-skin-bootpuki · PHP · 186 lines · 125 code · 15 blank · 46 comment · 32 complexity · 99a641e90a6c140694b6204cf37bdf88 MD5 · raw file

  1. <?php
  2. // PukiWiki - Yet another WikiWikiWeb clone.
  3. // $Id: navi.inc.php,v 1.22 2005/04/02 06:33:39 henoheno Exp $
  4. //
  5. // Navi plugin: Show DocBook-like navigation bar and contents
  6. /*
  7. * Usage:
  8. * #navi(contents-page-name) <for ALL child pages>
  9. * #navi([contents-page-name][,reverse]) <for contents page>
  10. *
  11. * Parameter:
  12. * contents-page-name - Page name of home of the navigation (default:itself)
  13. * reverse - Show contents revese
  14. *
  15. * Behaviour at contents page:
  16. * Always show child-page list like 'ls' plugin
  17. *
  18. * Behaviour at child pages:
  19. *
  20. * The first plugin call - Show a navigation bar like a DocBook header
  21. *
  22. * Prev <contents-page-name> Next
  23. * --------------------------------
  24. *
  25. * The second call - Show a navigation bar like a DocBook footer
  26. *
  27. * --------------------------------
  28. * Prev Home Next
  29. * <pagename> Up <pagename>
  30. *
  31. * Page-construction example:
  32. * foobar - Contents page, includes '#navi' or '#navi(foobar)'
  33. * foobar/1 - One of child pages, includes one or two '#navi(foobar)'
  34. * foobar/2 - One of child pages, includes one or two '#navi(foobar)'
  35. */
  36. // Exclusive regex pattern of child pages
  37. define('PLUGIN_NAVI_EXCLUSIVE_REGEX', '');
  38. //define('PLUGIN_NAVI_EXCLUSIVE_REGEX', '#/_#'); // Ignore 'foobar/_memo' etc.
  39. // Insert <link rel=... /> tags into XHTML <head></head>
  40. define('PLUGIN_NAVI_LINK_TAGS', FALSE); // FALSE, TRUE
  41. // ----
  42. function plugin_navi_convert()
  43. {
  44. global $vars, $script, $head_tags;
  45. global $_navi_prev, $_navi_next, $_navi_up, $_navi_home;
  46. static $navi = array();
  47. $current = $vars['page'];
  48. $reverse = FALSE;
  49. if (func_num_args()) {
  50. list($home, $reverse) = array_pad(func_get_args(), 2, '');
  51. // strip_bracket() is not necessary but compatible
  52. $home = get_fullname(strip_bracket($home), $current);
  53. $is_home = ($home == $current);
  54. if (! is_page($home)) {
  55. return '#navi(contents-page-name): No such page: ' .
  56. htmlspecialchars($home) . '<br />';
  57. } else if (! $is_home &&
  58. ! preg_match('/^' . preg_quote($home, '/') . '/', $current)) {
  59. return '#navi(' . htmlspecialchars($home) .
  60. '): Not a child page like: ' .
  61. htmlspecialchars($home . '/' . basename($current)) .
  62. '<br />';
  63. }
  64. $reverse = (strtolower($reverse) == 'reverse');
  65. } else {
  66. $home = $vars['page'];
  67. $is_home = TRUE; // $home == $current
  68. }
  69. $pages = array();
  70. $footer = isset($navi[$home]); // The first time: FALSE, the second: TRUE
  71. if (! $footer) {
  72. $navi[$home] = array(
  73. 'up' =>'',
  74. 'prev' =>'',
  75. 'prev1'=>'',
  76. 'next' =>'',
  77. 'next1'=>'',
  78. 'home' =>'',
  79. 'home1'=>'',
  80. );
  81. $pages = preg_grep('/^' . preg_quote($home, '/') .
  82. '($|\/)/', get_existpages());
  83. if (PLUGIN_NAVI_EXCLUSIVE_REGEX != '') {
  84. // If old PHP could use preg_grep(,,PREG_GREP_INVERT)...
  85. $pages = array_diff($pages,
  86. preg_grep(PLUGIN_NAVI_EXCLUSIVE_REGEX, $pages));
  87. }
  88. $pages[] = $current; // Sentinel :)
  89. $pages = array_unique($pages);
  90. natcasesort($pages);
  91. if ($reverse) $pages = array_reverse($pages);
  92. $prev = $home;
  93. foreach ($pages as $page) {
  94. if ($page == $current) break;
  95. $prev = $page;
  96. }
  97. $next = current($pages);
  98. $pos = strrpos($current, '/');
  99. $up = '';
  100. if ($pos > 0) {
  101. $up = substr($current, 0, $pos);
  102. $navi[$home]['up'] = make_pagelink($up, $_navi_up);
  103. }
  104. if (! $is_home) {
  105. $navi[$home]['prev'] = make_pagelink($prev);
  106. $navi[$home]['prev1'] = make_pagelink($prev, $_navi_prev);
  107. }
  108. if ($next != '') {
  109. $navi[$home]['next'] = make_pagelink($next);
  110. $navi[$home]['next1'] = make_pagelink($next, $_navi_next);
  111. }
  112. $navi[$home]['home'] = make_pagelink($home);
  113. $navi[$home]['home1'] = make_pagelink($home, $_navi_home);
  114. // Generate <link> tag: start next prev(previous) parent(up)
  115. // Not implemented: contents(toc) search first(begin) last(end)
  116. if (PLUGIN_NAVI_LINK_TAGS) {
  117. foreach (array('start'=>$home, 'next'=>$next,
  118. 'prev'=>$prev, 'up'=>$up) as $rel=>$_page) {
  119. if ($_page != '') {
  120. $s_page = htmlspecialchars($_page);
  121. $r_page = rawurlencode($_page);
  122. $head_tags[] = ' <link rel="' .
  123. $rel . '" href="' . $script .
  124. '?' . $r_page . '" title="' .
  125. $s_page . '" />';
  126. }
  127. }
  128. }
  129. }
  130. $ret = '';
  131. if ($is_home) {
  132. // Show contents
  133. $count = count($pages);
  134. if ($count == 0) {
  135. return '#navi(contents-page-name): You already view the result<br />';
  136. } else if ($count == 1) {
  137. // Sentinel only: Show usage and warning
  138. $home = htmlspecialchars($home);
  139. $ret .= '#navi(' . $home . '): No child page like: ' .
  140. $home . '/Foo';
  141. } else {
  142. $ret .= '<ul>';
  143. foreach ($pages as $page)
  144. if ($page != $home)
  145. $ret .= ' <li>' . make_pagelink($page) . '</li>';
  146. $ret .= '</ul>';
  147. }
  148. } else if (! $footer) {
  149. // Header
  150. $ret = <<<EOD
  151. <ul class="navi">
  152. <li class="navi_left">{$navi[$home]['prev1']}</li>
  153. <li class="navi_right">{$navi[$home]['next1']}</li>
  154. <li class="navi_none">{$navi[$home]['home']}</li>
  155. </ul>
  156. <hr class="full_hr" />
  157. EOD;
  158. } else {
  159. // Footer
  160. $ret = <<<EOD
  161. <hr class="full_hr" />
  162. <ul class="navi">
  163. <li class="navi_left">{$navi[$home]['prev1']}<br /><small>{$navi[$home]['prev']}</small></li>
  164. <li class="navi_right">{$navi[$home]['next1']}<br /><small>{$navi[$home]['next']}</small></li>
  165. <li class="navi_none">{$navi[$home]['home1']}<br /><small>{$navi[$home]['up']}</small></li>
  166. </ul>
  167. EOD;
  168. }
  169. return $ret;
  170. }
  171. ?>