trails.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2002-2004 Patrick R. Michaud (pmichaud@pobox.com)
  3. This file is part of PmWiki; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published
  5. by the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version. See pmwiki.php for full details.
  7. This script enables markup of the form <<|TrailPage|>> to be
  8. used to build "trails" through wiki documents.
  9. This feature is automatically included from stdconfig.php unless
  10. disabled by $EnableWikiTrails = 0; . To explicitly include this feature,
  11. execute
  12. include_once("scripts/trails.php");
  13. from config.php somewhere.
  14. Once enabled, the <<|TrailPage|>> markup is replaced with
  15. << PrevPage | TrailPage | NextPage >> on output. TrailPage should
  16. contain either a bullet or number list defining the sequence of pages
  17. in the "trail".
  18. The ^|TrailPage|^ markup uses the depth of the bullets to display
  19. the ancestry of the TrailPage to the current one. The <|TrailPage|>
  20. markup is like <<|TrailPage|>> except that "< PrevPage |" and
  21. "| NextPage >" are omitted if at the beginning or end of the
  22. trail respectively. Thanks to John Rankin for contributing these
  23. markups and the original suggestion for WikiTrails.
  24. */
  25. $TrailLinkPattern =
  26. "(?:($GroupNamePattern)([\\/.]))?(($WikiWordPattern)|($FreeLinkPattern))";
  27. $DoubleBrackets["/&lt;&lt;\\|($TrailLinkPattern)\\|&gt;&gt;/e"]
  28. = 'MakeTrailStop("$1");';
  29. $DoubleBrackets["/&lt;\\|($TrailLinkPattern)\\|&gt;/e"]
  30. = 'MakeTrailStopB("$1");';
  31. $DoubleBrackets["/\\^\\|($TrailLinkPattern)\\|\\^/e"]
  32. = 'MakeTrailPath("$1");';
  33. SDV($TrailPathSep,' | ');
  34. function MakeTrailStop($link) {
  35. global $pagename;
  36. $t = ReadTrail($link);
  37. $prev = ''; $next = '';
  38. for($i=0;$i<count($t);$i++) {
  39. if ($t[$i]['name']==$pagename) {
  40. if ($i>0) $prev = $t[$i-1]['link'];
  41. if ($i+1<count($t)) $next = $t[$i+1]['link'];
  42. }
  43. }
  44. return
  45. "<span class='wikitrail'>&lt;&lt; $prev | $link | $next &gt;&gt;</span>";
  46. }
  47. function MakeTrailStopB($link) {
  48. global $pagename;
  49. $t = ReadTrail($link);
  50. $prev = ''; $next = '';
  51. for($i=0;$i<count($t);$i++) {
  52. if ($t[$i]['name']==$pagename) {
  53. if ($i>0) $prev = '&lt; '.$t[$i-1]['link'].' | ';
  54. if ($i+1<count($t)) $next = ' | '.$t[$i+1]['link'].' &gt;';
  55. }
  56. }
  57. return "<span class='wikitrail'>$prev$link$next</span>";
  58. }
  59. function MakeTrailPath($link) {
  60. global $pagename,$TrailPathSep;
  61. $t = ReadTrail($link);
  62. for($i=0;$i<count($t);$i++) {
  63. if ($t[$i]['name']==$pagename) {
  64. while ($t[$i]['depth']>0) {
  65. $crumbs = $TrailPathSep.$t[$i]['link'].$crumbs;
  66. $i = $t[$i]['parent'];
  67. }
  68. return "<span class='wikitrail'>$link$crumbs</span>";
  69. }
  70. }
  71. return $link;
  72. }
  73. function ReadTrail($link) {
  74. global $pagename,$TrailLinkPattern;
  75. $trailname = FmtWikiLink('',$link,NULL,'PageName');
  76. $trailpage = ReadPage($trailname);
  77. if ($trailpage) {
  78. $trailgroup = FmtPageName('$Group',$trailname);
  79. $n = 0;
  80. foreach(explode("\n",$trailpage['text']) as $x) {
  81. if (preg_match("/([#*]+)\\s*(\\[\\[)?($TrailLinkPattern)/",$x,$match)) {
  82. $t[$n]['depth'] = $depth = strlen($match[1]);
  83. $link = $match[3];
  84. if (!preg_match('![./]!',$link)) $link="$trailgroup/$link";
  85. $t[$n]['link'] = $link;
  86. $t[$n]['name'] = FmtWikiLink('',$link,NULL,'PageName',$trailname);
  87. if ($match[2]>'' &&
  88. preg_match("/^[#*]+\\s*\\[\\[($TrailLinkPattern)((?:\\s.*?)?\\]\\])/",$x,$dbm))
  89. $t[$n]['link'] = "[[$link".array_pop($dbm);
  90. for($i=$depth;$i<10;$i++) $d[$i] = $n;
  91. if ($depth>1) $t[$n]['parent']=@$d[$depth-1];
  92. $n++;
  93. }
  94. }
  95. }
  96. return $t;
  97. }
  98. ?>