summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/toggle.pm
blob: 3f9c4867256ab0f9aa9f23bd1026f9594ee27a39 (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. my $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. document.getElementById(id).style.display="none";
  22. as[i].onclick = function() {
  23. toggle(this);
  24. return false;
  25. }
  26. }
  27. }
  28. function toggle(s) {
  29. var id = s.href.match(/#(\w.+)/)[1];
  30. style = document.getElementById(id).style;
  31. if (style.display == "none")
  32. style.display = "block";
  33. else
  34. style.display = "none";
  35. }
  36. function getElementsByClass(class) {
  37. var ret = new Array();
  38. var pattern = new RegExp("(^|\\s)"+class+"(\\s|$)");
  39. var els = document.getElementsByTagName('*');
  40. for (i = 0, j = 0; i < els.length; i++) {
  41. if ( pattern.test(els[i].className) ) {
  42. ret[j] = els[i];
  43. j++;
  44. }
  45. }
  46. return ret;
  47. }
  48. //-->
  49. </script>
  50. EOF
  51. sub import { #{{{
  52. hook(type => "preprocess", id => "toggle",
  53. call => \&preprocess_toggle);
  54. hook(type => "preprocess", id => "toggleable",
  55. call => \&preprocess_toggleable);
  56. hook(type => "format", id => "toggle", call => \&format);
  57. } # }}}
  58. sub genid ($$) { #{{{
  59. my $page=shift;
  60. my $id=shift;
  61. $id="$page.$id";
  62. # make it a legal html id attribute
  63. $id=~s/[^-a-zA-Z0-9.]/-/g;
  64. if ($id !~ /^[a-zA-Z]/) {
  65. $id="id$id";
  66. }
  67. return $id;
  68. } #}}}
  69. sub preprocess_toggle (@) { #{{{
  70. my %params=(id => "default", text => "more", @_);
  71. my $id=genid($params{page}, $params{id});
  72. return "<a class=\"toggle\" href=\"#$id\">$params{text}</a>";
  73. } # }}}
  74. sub preprocess_toggleable (@) { #{{{
  75. my %params=(id => "default", text => "", @_);
  76. # Preprocess the text to expand any preprocessor directives
  77. # embedded inside it.
  78. $params{text}=IkiWiki::preprocess($params{page}, $params{destpage},
  79. IkiWiki::filter($params{page}, $params{destpage}, $params{text}));
  80. my $id=genid($params{page}, $params{id});
  81. # Should really be a postprocessor directive, oh well. Work around
  82. # markdown's dislike of markdown inside a <div> with various funky
  83. # whitespace.
  84. my ($indent)=$params{text}=~/( +)$/;
  85. $indent="" unless defined $indent;
  86. return "<div class=\"toggleable\" id=\"$id\"></div>\n\n$params{text}\n$indent<div class=\"toggleableend\"></div>";
  87. } # }}}
  88. sub format (@) { #{{{
  89. my %params=@_;
  90. if ($params{content}=~s!(<div class="toggleable" id="[^"]+">)</div>!$1!g) {
  91. $params{content}=~s/<div class="toggleableend">//g;
  92. $params{content}=~s!^<\/body>!$javascript</body>!m;
  93. }
  94. return $params{content};
  95. } # }}}
  96. 1