summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/toggle.pm
blob: cd9617e259d5433f561faabb4719abc7756c8d75 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::toggle;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. # Here's the javascript that makes this possible. A key feature is the use
  7. # of css to hide toggleables, to avoid any flashing on page load. The css
  8. # is only emitted after the javascript tests that it's going to be able to
  9. # show the toggleables.
  10. our $javascript=<<'EOF';
  11. <script type="text/javascript">
  12. <!--
  13. if (document.getElementById && document.getElementsByTagName && document.createTextNode) {
  14. document.write('<style type="text/css">div.toggleable { display: none; }</style>');
  15. window.onload = inittoggle;
  16. }
  17. function inittoggle() {
  18. var as = getElementsByClass('toggle');
  19. for (var i = 0; i < as.length; i++) {
  20. var id = as[i].href.match(/#(\w.+)/)[1];
  21. if (document.getElementById(id).className == "toggleable")
  22. document.getElementById(id).style.display="none";
  23. as[i].onclick = function() {
  24. toggle(this);
  25. return false;
  26. }
  27. }
  28. }
  29. function toggle(s) {
  30. var id = s.href.match(/#(\w.+)/)[1];
  31. style = document.getElementById(id).style;
  32. if (style.display == "none")
  33. style.display = "block";
  34. else
  35. style.display = "none";
  36. }
  37. function getElementsByClass(cls, node, tag) {
  38. if (document.getElementsByClass)
  39. return document.getElementsByClass(cls, node, tag);
  40. if (! node) node = document;
  41. if (! tag) tag = '*';
  42. var ret = new Array();
  43. var pattern = new RegExp("(^|\\s)"+cls+"(\\s|$)");
  44. var els = node.getElementsByTagName(tag);
  45. for (i = 0; i < els.length; i++) {
  46. if ( pattern.test(els[i].className) ) {
  47. ret.push(els[i]);
  48. }
  49. }
  50. return ret;
  51. }
  52. //-->
  53. </script>
  54. EOF
  55. sub import { #{{{
  56. hook(type => "preprocess", id => "toggle",
  57. call => \&preprocess_toggle);
  58. hook(type => "preprocess", id => "toggleable",
  59. call => \&preprocess_toggleable);
  60. hook(type => "format", id => "toggle", call => \&format);
  61. } # }}}
  62. sub genid ($$) { #{{{
  63. my $page=shift;
  64. my $id=shift;
  65. $id="$page.$id";
  66. # make it a legal html id attribute
  67. $id=~s/[^-a-zA-Z0-9.]/-/g;
  68. if ($id !~ /^[a-zA-Z]/) {
  69. $id="id$id";
  70. }
  71. return $id;
  72. } #}}}
  73. sub preprocess_toggle (@) { #{{{
  74. my %params=(id => "default", text => "more", @_);
  75. my $id=genid($params{page}, $params{id});
  76. return "<a class=\"toggle\" href=\"#$id\">$params{text}</a>";
  77. } # }}}
  78. sub preprocess_toggleable (@) { #{{{
  79. my %params=(id => "default", text => "", open => "no", @_);
  80. # Preprocess the text to expand any preprocessor directives
  81. # embedded inside it.
  82. $params{text}=IkiWiki::preprocess($params{page}, $params{destpage},
  83. IkiWiki::filter($params{page}, $params{destpage}, $params{text}));
  84. my $id=genid($params{page}, $params{id});
  85. my $class=(lc($params{open}) ne "yes") ? "toggleable" : "toggleable-open";
  86. # Should really be a postprocessor directive, oh well. Work around
  87. # markdown's dislike of markdown inside a <div> with various funky
  88. # whitespace.
  89. my ($indent)=$params{text}=~/( +)$/;
  90. $indent="" unless defined $indent;
  91. return "<div class=\"$class\" id=\"$id\"></div>\n\n$params{text}\n$indent<div class=\"toggleableend\"></div>";
  92. } # }}}
  93. sub format (@) { #{{{
  94. my %params=@_;
  95. if ($params{content}=~s!(<div class="toggleable(?:-open)?" id="[^"]+">)</div>!$1!g) {
  96. $params{content}=~s/<div class="toggleableend">//g;
  97. if (! ($params{content}=~s!^<body>!<body>$javascript!m)) {
  98. # no </body> tag, probably in preview mode
  99. $params{content}=$javascript.$params{content};
  100. }
  101. }
  102. return $params{content};
  103. } # }}}
  104. 1