vardoc.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 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 only
  12. generated once per browse request, and then only when needed.
  13. */
  14. # These statements set up the default values and add $VariableName
  15. # links into the markup
  16. SDV($VarPagesFmt,array('PmWiki.Variables','PmWiki.BasicVariables',
  17. 'PmWiki.LayoutVariables','PmWiki.LinkVariables','PmWiki.EditVariables',
  18. 'PmWiki.UploadVariables','PmWiki.OtherVariables','PmWiki.MailPosts'));
  19. SDV($VarLinkExistsFmt,"<a class='varlink' href='\$_Url'><code class='varlink'>\$_LinkText</code></a>");
  20. SDV($VarLinkMissingFmt,"\$_LinkText");
  21. $DoubleBrackets["/^:\\$($WikiWordPattern):/"] = ":[[#$1]]\$$1:";
  22. $LinkPatterns[780]["\\$$WikiWordPattern"] = 'FmtVarLink';
  23. $InlineReplacements['/\\[\\[\\$Varindex\\]\\]/e'] = 'Keep(VarIndexList())';
  24. # FmtVarLink(...) is called when the $LinkPattern (above) is seen,
  25. # it returns a string based on whether a variable has been documented
  26. function FmtVarLink($pat,$ref,$txt) {
  27. global $VarIndex,$VarLinkExistsFmt,$VarLinkMissingFmt;
  28. if (!isset($VarIndex)) { VarIndexLoad(); }
  29. $link = substr($ref,1);
  30. $rtxt = (!is_null($txt)) ? $txt : $ref;
  31. if (!$VarIndex[$link]['url'])
  32. return str_replace('$_LinkText',$rtxt,$VarLinkMissingFmt);
  33. return str_replace(array('$_LinkText','$_Url'),
  34. array($rtxt,$VarIndex[$link]['url']),$VarLinkExistsFmt);
  35. }
  36. # VarIndexLoad() loads $VarIndex with the variable definitions from the
  37. # pages given by $VarPagesFmt
  38. function VarIndexLoad() {
  39. global $VarPagesFmt,$VarIndex,$WikiWordPattern;
  40. if (!isset($VarIndex)) $VarIndex=array();
  41. foreach($VarPagesFmt as $v) {
  42. $vname = FmtPageName('$PageName',$v);
  43. $vpage = ReadPage($v);
  44. if (!$vpage) continue;
  45. if (!preg_match_all("/\n:\\$($WikiWordPattern):/",$vpage['text'],$match))
  46. continue;
  47. foreach($match[1] as $n) {
  48. $VarIndex[$n]['page']=$vname;
  49. $VarIndex[$n]['url']=FmtPageName("\$PageUrl#$n",$vname);
  50. }
  51. }
  52. }
  53. # VarIndexList() generates a table of all indexed variables.
  54. function VarIndexList() {
  55. global $VarIndex;
  56. if (!isset($VarIndex)) VarIndexLoad();
  57. ksort($VarIndex);
  58. $out = "<table><tr><th>Variable</th><th>Documented in</th></tr>\n";
  59. foreach($VarIndex as $v=>$a)
  60. $out .= FmtPageName("<tr><td><a class='varlink' href='{$a['url']}'><code>&#036;$v</code></a></td><td><a
  61. href='\$PageUrl'>\$Title</a></td></tr>\n",$a['page']);
  62. $out .= "</table>";
  63. return $out;
  64. }
  65. ?>