summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/toggle.pm
blob: 54c9a0d9a29223e98aecfc6e49f707c7263adfae (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. if (! $params{preview}) {
  74. return "<a class=\"toggle\" href=\"#$id\">$params{text}</a>";
  75. }
  76. else {
  77. return "$params{text} ".
  78. gettext("(not toggleable in preview mode)");
  79. }
  80. } # }}}
  81. sub preprocess_toggleable (@) { #{{{
  82. my %params=(id => "default", text => "", @_);
  83. # Preprocess the text to expand any preprocessor directives
  84. # embedded inside it.
  85. $params{text}=IkiWiki::preprocess($params{page}, $params{destpage},
  86. IkiWiki::filter($params{page}, $params{destpage}, $params{text}));
  87. my $id=genid($params{page}, $params{id});
  88. # Should really be a postprocessor directive, oh well. Work around
  89. # markdown's dislike of markdown inside a <div> with various funky
  90. # whitespace.
  91. my ($indent)=$params{text}=~/( +)$/;
  92. $indent="" unless defined $indent;
  93. return "<div class=\"toggleable\" id=\"$id\"></div>\n\n$params{text}\n$indent<div class=\"toggleableend\"></div>";
  94. } # }}}
  95. sub format (@) { #{{{
  96. my %params=@_;
  97. if ($params{content}=~s!(<div class="toggleable" id="[^"]+">)</div>!$1!g) {
  98. $params{content}=~s/<div class="toggleableend">//g;
  99. if (! ($params{content}=~s!^<\/body>!$javascript</body>!m)) {
  100. # no </body> tag, probably in preview mode
  101. $params{content}.=$javascript;
  102. }
  103. }
  104. return $params{content};
  105. } # }}}
  106. 1