trails.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2002-2006 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. Markup('<<|','<links','/&lt;&lt;\\|([^|]+|\\[\\[(.+?)\\]\\])\\|&gt;&gt;/e',
  26. "MakeTrailStop(\$pagename,'$1')");
  27. Markup('<|','><<|','/&lt;\\|([^|]+|\\[\\[(.+?)\\]\\])\\|&gt;/e',
  28. "MakeTrailStopB(\$pagename,'$1')");
  29. Markup('^|','<links','/\\^\\|([^|]+|\\[\\[(.+?)\\]\\])\\|\\^/e',
  30. "MakeTrailPath(\$pagename,'$1')");
  31. SDVA($SaveAttrPatterns, array(
  32. '/<<\\|([^|]+|\\[\\[(.+?)\\]\\])\\|>>/' => '$1',
  33. '/<\\|([^|]+|\\[\\[(.+?)\\]\\])\\|>/' => '$1',
  34. '/\\^\\|([^|]+|\\[\\[(.+?)\\]\\])\\|\\^/' => '$1'));
  35. function ReadTrail($pagename,$trailname) {
  36. global $SuffixPattern,$GroupPattern,$WikiWordPattern,$LinkWikiWords;
  37. if (preg_match('/^\\[\\[(.+?)(-&gt;|\\|)(.+?)\\]\\]$/', $trailname, $m))
  38. $trailname = ($m[2] == '|') ? $m[1] : $m[3];
  39. $trailname = MakePageName($pagename,$trailname);
  40. $trailpage = ReadPage($trailname, READPAGE_CURRENT);
  41. if (!$trailpage) return false;
  42. $t = array();
  43. $n = 0;
  44. foreach(explode("\n", htmlspecialchars(@$trailpage['text'], ENT_NOQUOTES))
  45. as $x) {
  46. $x = preg_replace("/\\[\\[([^\\]]*)->([^\\]]*)\\]\\]/",'[[$2|$1]]',$x);
  47. if (!preg_match("/^([#*:]+) \\s*
  48. (\\[\\[([^:#!|][^|:]*?)(\\|.*?)?\\]\\]($SuffixPattern)
  49. | (($GroupPattern([\\/.]))?$WikiWordPattern)) (.*)/x",$x,$match))
  50. continue;
  51. if (@$match[6]) {
  52. if (!$LinkWikiWords) continue;
  53. $tgt = MakePageName($trailname, $match[6]);
  54. } else $tgt = MakePageName($trailname,
  55. preg_replace('/[#?].+/', '', $match[3]));
  56. $t[$n]['depth'] = $depth = strlen($match[1]);
  57. $t[$n]['pagename'] = $tgt;
  58. $t[$n]['markup'] = $match[2];
  59. $t[$n]['detail'] = $match[9];
  60. for($i=$depth;$i<10;$i++) $d[$i]=$n;
  61. if ($depth>1) $t[$n]['parent']=@$d[$depth-1];
  62. $n++;
  63. }
  64. return $t;
  65. }
  66. function MakeTrailStop($pagename,$trailname) {
  67. $t = ReadTrail($pagename,$trailname);
  68. $prev=''; $next='';
  69. for($i=0;$i<count($t);$i++) {
  70. if ($t[$i]['pagename']==$pagename) {
  71. if ($i>0) $prev = $t[$i-1]['markup'];
  72. if ($i+1<count($t)) $next = $t[$i+1]['markup'];
  73. }
  74. }
  75. return "<span class='wikitrail'>&lt;&lt; $prev | $trailname | $next &gt;&gt;</span>";
  76. }
  77. function MakeTrailStopB($pagename,$trailname) {
  78. $t = ReadTrail($pagename,$trailname);
  79. $prev = ''; $next = '';
  80. for($i=0;$i<count($t);$i++) {
  81. if ($t[$i]['pagename']==$pagename) {
  82. if ($i>0) $prev = '&lt; '.$t[$i-1]['markup'].' | ';
  83. if ($i+1<count($t)) $next = ' | '.$t[$i+1]['markup'].' &gt;';
  84. }
  85. }
  86. return "<span class='wikitrail'>$prev$trailname$next</span>";
  87. }
  88. function MakeTrailPath($pagename,$trailname) {
  89. global $TrailPathSep;
  90. SDV($TrailPathSep,' | ');
  91. $t = ReadTrail($pagename,$trailname);
  92. $crumbs = '';
  93. for($i=0;$i<count($t);$i++) {
  94. if ($t[$i]['pagename']==$pagename) {
  95. while (@$t[$i]['depth']>0) {
  96. $crumbs = $TrailPathSep.$t[$i]['markup'].$crumbs;
  97. $i = @$t[$i]['parent'];
  98. }
  99. return "<span class='wikitrail'>$trailname$crumbs</span>";
  100. }
  101. }
  102. return $trailname;
  103. }