summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/toggle.pm
blob: 284eb8249df031e77f2e1ff1796d19624bbae1cf (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(class) {
  38. var ret = new Array();
  39. var pattern = new RegExp("(^|\\s)"+class+"(\\s|$)");
  40. var els = document.getElementsByTagName('*');
  41. for (i = 0, j = 0; i < els.length; i++) {
  42. if ( pattern.test(els[i].className) ) {
  43. ret[j] = els[i];
  44. j++;
  45. }
  46. }
  47. return ret;
  48. }
  49. //-->
  50. </script>
  51. EOF
  52. sub import { #{{{
  53. hook(type => "preprocess", id => "toggle",
  54. call => \&preprocess_toggle);
  55. hook(type => "preprocess", id => "toggleable",
  56. call => \&preprocess_toggleable);
  57. hook(type => "format", id => "toggle", call => \&format);
  58. } # }}}
  59. sub genid ($$) { #{{{
  60. my $page=shift;
  61. my $id=shift;
  62. $id="$page.$id";
  63. # make it a legal html id attribute
  64. $id=~s/[^-a-zA-Z0-9.]/-/g;
  65. if ($id !~ /^[a-zA-Z]/) {
  66. $id="id$id";
  67. }
  68. return $id;
  69. } #}}}
  70. sub preprocess_toggle (@) { #{{{
  71. my %params=(id => "default", text => "more", @_);
  72. my $id=genid($params{page}, $params{id});
  73. return "<a class=\"toggle\" href=\"#$id\">$params{text}</a>";
  74. } # }}}
  75. sub preprocess_toggleable (@) { #{{{
  76. my %params=(id => "default", text => "", open => "no", @_);
  77. # Preprocess the text to expand any preprocessor directives
  78. # embedded inside it.
  79. $params{text}=IkiWiki::preprocess($params{page}, $params{destpage},
  80. IkiWiki::filter($params{page}, $params{destpage}, $params{text}));
  81. my $id=genid($params{page}, $params{id});
  82. my $class=(lc($params{open}) ne "yes") ? "toggleable" : "toggleable-open";
  83. # Should really be a postprocessor directive, oh well. Work around
  84. # markdown's dislike of markdown inside a <div> with various funky
  85. # whitespace.
  86. my ($indent)=$params{text}=~/( +)$/;
  87. $indent="" unless defined $indent;
  88. return "<div class=\"$class\" id=\"$id\"></div>\n\n$params{text}\n$indent<div class=\"toggleableend\"></div>";
  89. } # }}}
  90. sub format (@) { #{{{
  91. my %params=@_;
  92. if ($params{content}=~s!(<div class="toggleable(?:-open)?" id="[^"]+">)</div>!$1!g) {
  93. $params{content}=~s/<div class="toggleableend">//g;
  94. if (! ($params{content}=~s!^<body>!<body>$javascript!m)) {
  95. # no </body> tag, probably in preview mode
  96. $params{content}=$javascript.$params{content};
  97. }
  98. }
  99. return $params{content};
  100. } # }}}
  101. 1