vardoc.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2002-2005 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 provides special handling for WikiWords that are
  8. preceded by a $, treating them as PmWiki variables to be looked up
  9. in the variable documentation pages if such documentation exists.
  10. The $VarPagesFmt variable contains a list of pages to be searched
  11. to build an index of the variable documentation. This index is
  12. generated only once per browse request, and then only when needed.
  13. */
  14. SDV($VarPagesFmt,array('$[PmWiki.Variables]'));
  15. Markup('varlink','<wikilink',"/\\$($WikiWordPattern)\\b/e",
  16. "Keep(VarLink(\$pagename,'$1','$$1'))");
  17. Markup('vardef','<links',"/^:\\$($WikiWordPattern):/",
  18. ':[[#$1]]$$1:');
  19. Markup('varindex', 'directives',
  20. '/\\(:varindex:\\)/ei',
  21. "Keep(VarIndexList(\$pagename))");
  22. $HTMLStylesFmt['vardoc'] = "a.varlink { text-decoration:none; }\n";
  23. function VarLink($pagename,$tgt,$txt) {
  24. global $VarIndex,$FmtV,$VarLinkMissingFmt,$VarLinkExistsFmt;
  25. SDV($VarLinkMissingFmt,'$LinkText');
  26. SDV($VarLinkExistsFmt,"<a class='varlink' href='\$LinkUrl'><code class='varlink'>\$LinkText</code></a>");
  27. VarIndexLoad($pagename);
  28. $FmtV['$LinkText'] = str_replace('$', '&#36;', $txt);
  29. $FmtV['$LinkUrl'] = @$VarIndex[$tgt]['url'];
  30. if (@!$VarIndex[$tgt]['url'])
  31. return FmtPageName($VarLinkMissingFmt,$pagename);
  32. return FmtPageName($VarLinkExistsFmt,$pagename);
  33. }
  34. function VarIndexLoad($pagename) {
  35. global $VarPagesFmt,$VarIndex,$WikiWordPattern;
  36. static $loaded;
  37. $VarIndex = (array)@$VarIndex;
  38. if ($loaded) return;
  39. foreach($VarPagesFmt as $vf) {
  40. $v = FmtPageName($vf, $pagename);
  41. if (@$loaded[$v]) continue;
  42. $vlist = array($v);
  43. $t = ReadTrail($pagename,$v);
  44. if ($t)
  45. for($i=0;$i<count($t);$i++)
  46. if (@!$loaded[$t[$i]['pagename']]) $vlist[]=$t[$i]['pagename'];
  47. foreach($vlist as $vname) {
  48. $vpage = ReadPage($vname); @$loaded[$vname]++;
  49. if (!$vpage) continue;
  50. if (!preg_match_all("/\n:\\$([[:upper:]]\\w+):/",@$vpage['text'],$match))
  51. continue;
  52. foreach($match[1] as $n) {
  53. $VarIndex[$n]['pagename'] = $vname;
  54. $VarIndex[$n]['url'] = FmtPageName("{\$PageUrl}#$n",$vname);
  55. }
  56. }
  57. }
  58. }
  59. # VarIndexList() generates a table of all indexed variables.
  60. function VarIndexList($pagename) {
  61. global $VarIndex;
  62. if (!isset($VarIndex)) VarIndexLoad($pagename);
  63. ksort($VarIndex);
  64. $out = "<table><tr><th>Variable</th><th>Documented in</th></tr>\n";
  65. foreach($VarIndex as $v=>$a)
  66. $out .= FmtPageName("<tr><td><a class='varlink'
  67. href='{$a['url']}'><code>&#036;$v</code></a></td><td><a
  68. href='{\$PageUrl}'>{\$Name}</a></td></tr>\n",$a['pagename']);
  69. $out .= "</table>";
  70. return $out;
  71. }