trails.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2002-2017 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. Script maintained by Petko YOTOV www.pmwiki.org/petko
  25. */
  26. Markup('<<|','<links','/&lt;&lt;\\|([^|]+|\\[\\[(.+?)\\]\\])\\|&gt;&gt;/',
  27. "MarkupMakeTrail");
  28. Markup('<|','><<|','/&lt;\\|([^|]+|\\[\\[(.+?)\\]\\])\\|&gt;/',
  29. "MarkupMakeTrail");
  30. Markup('^|','<links','/\\^\\|([^|]+|\\[\\[(.+?)\\]\\])\\|\\^/',
  31. "MarkupMakeTrail");
  32. function MarkupMakeTrail($m) {
  33. extract($GLOBALS["MarkupToHTML"]); # get $pagename, $markupid
  34. switch ($markupid) {
  35. case '<<|':
  36. return PRR(MakeTrailStop($pagename,$m[1]));
  37. case '<|':
  38. return PRR(MakeTrailStopB($pagename,$m[1]));
  39. case '^|':
  40. return PRR(MakeTrailPath($pagename,$m[1]));
  41. }
  42. }
  43. SDVA($SaveAttrPatterns, array(
  44. '/<<\\|([^|]+|\\[\\[(.+?)\\]\\])\\|>>/' => '$1',
  45. '/<\\|([^|]+|\\[\\[(.+?)\\]\\])\\|>/' => '$1',
  46. '/\\^\\|([^|]+|\\[\\[(.+?)\\]\\])\\|\\^/' => '$1'));
  47. $Conditions['ontrail'] = 'CondOnTrail($pagename, $condparm)';
  48. function CondOnTrail($pagename, $condparm) {
  49. @list($trailname, $pn) = preg_split('/\\s+/', $condparm, 2);
  50. $trail = ReadTrail($pagename, $trailname);
  51. if (!$trail) return false;
  52. $pn = ($pn > '') ? MakePageName($pagename, $pn) : $pagename;
  53. foreach($trail as $t)
  54. if ($t['pagename'] == $pn) return true;
  55. return false;
  56. }
  57. function ReadTrail($pagename, $trailname) {
  58. global $RASPageName, $SuffixPattern, $GroupPattern, $WikiWordPattern,
  59. $LinkWikiWords;
  60. if (preg_match('/^\\[\\[(.+?)(-&gt;|\\|)(.+?)\\]\\]$/', $trailname, $m))
  61. $trailname = ($m[2] == '|') ? $m[1] : $m[3];
  62. $trailtext = RetrieveAuthSection($pagename, $trailname);
  63. $trailname = $RASPageName;
  64. $trailtext = Qualify($trailname, $trailtext);
  65. $t = array();
  66. $n = 0;
  67. foreach(explode("\n", PHSC(@$trailtext, ENT_NOQUOTES))
  68. as $x) {
  69. $x = preg_replace("/\\[\\[([^\\]]*)-&gt;([^\\]]*)\\]\\]/",'[[$2|$1]]',$x);
  70. if (!preg_match("/^([#*:]+) \\s*
  71. (\\[\\[([^:#!|][^|:]*?)(?:\".*?\")?(\\|.*?)?\\]\\]($SuffixPattern)
  72. | (($GroupPattern([\\/.]))?$WikiWordPattern)) (.*)/x",$x,$match))
  73. continue;
  74. if (@$match[6]) {
  75. if (!$LinkWikiWords) continue;
  76. $tgt = MakePageName($trailname, $match[6]);
  77. } else $tgt = MakePageName($trailname, $match[3]);
  78. $t[$n]['depth'] = $depth = strlen($match[1]);
  79. $t[$n]['pagename'] = $tgt;
  80. $t[$n]['markup'] = $match[2];
  81. $t[$n]['detail'] = $match[9];
  82. for($i=$depth;$i<10;$i++) $d[$i]=$n;
  83. if ($depth>1) $t[$n]['parent']=@$d[$depth-1];
  84. $n++;
  85. }
  86. return $t;
  87. }
  88. function MakeTrailStop($pagename,$trailname) {
  89. $t = ReadTrail($pagename,$trailname);
  90. $prev=''; $next='';
  91. for($i=0;$i<count($t);$i++) {
  92. if ($t[$i]['pagename']==$pagename) {
  93. if ($i>0) $prev = $t[$i-1]['markup'];
  94. if ($i+1<count($t)) $next = $t[$i+1]['markup'];
  95. }
  96. }
  97. return "<span class='wikitrail'>&lt;&lt; $prev | $trailname | $next &gt;&gt;</span>";
  98. }
  99. function MakeTrailStopB($pagename,$trailname) {
  100. $t = ReadTrail($pagename,$trailname);
  101. $prev = ''; $next = '';
  102. for($i=0;$i<count($t);$i++) {
  103. if ($t[$i]['pagename']==$pagename) {
  104. if ($i>0) $prev = '&lt; '.$t[$i-1]['markup'].' | ';
  105. if ($i+1<count($t)) $next = ' | '.$t[$i+1]['markup'].' &gt;';
  106. }
  107. }
  108. return "<span class='wikitrail'>$prev$trailname$next</span>";
  109. }
  110. function MakeTrailPath($pagename,$trailname) {
  111. global $TrailPathSep;
  112. SDV($TrailPathSep,' | ');
  113. $t = ReadTrail($pagename,$trailname);
  114. $crumbs = '';
  115. for($i=0;$i<count($t);$i++) {
  116. if ($t[$i]['pagename']==$pagename) {
  117. while (@$t[$i]['depth']>0) {
  118. $crumbs = $TrailPathSep.$t[$i]['markup'].$crumbs;
  119. $i = @$t[$i]['parent'];
  120. }
  121. return "<span class='wikitrail'>$trailname$crumbs</span>";
  122. }
  123. }
  124. return "<span class='wikitrail'>$trailname</span>";
  125. }