summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/toggle.pm
blob: 610d38e3ac0d8e39c43d56e727f098441862b5ce (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 => "getsetup", id => "toggle", call => \&getsetup);
  57. hook(type => "preprocess", id => "toggle",
  58. call => \&preprocess_toggle);
  59. hook(type => "preprocess", id => "toggleable",
  60. call => \&preprocess_toggleable);
  61. hook(type => "format", id => "toggle", call => \&format);
  62. } # }}}
  63. sub getsetup () { #{{{
  64. return
  65. plugin => {
  66. safe => 1,
  67. rebuild => undef,
  68. },
  69. } #}}}
  70. sub genid ($$) { #{{{
  71. my $page=shift;
  72. my $id=shift;
  73. $id="$page.$id";
  74. # make it a legal html id attribute
  75. $id=~s/[^-a-zA-Z0-9.]/-/g;
  76. if ($id !~ /^[a-zA-Z]/) {
  77. $id="id$id";
  78. }
  79. return $id;
  80. } #}}}
  81. sub preprocess_toggle (@) { #{{{
  82. my %params=(id => "default", text => "more", @_);
  83. my $id=genid($params{page}, $params{id});
  84. return "<a class=\"toggle\" href=\"#$id\">$params{text}</a>";
  85. } # }}}
  86. sub preprocess_toggleable (@) { #{{{
  87. my %params=(id => "default", text => "", open => "no", @_);
  88. # Preprocess the text to expand any preprocessor directives
  89. # embedded inside it.
  90. $params{text}=IkiWiki::preprocess($params{page}, $params{destpage},
  91. IkiWiki::filter($params{page}, $params{destpage}, $params{text}));
  92. my $id=genid($params{page}, $params{id});
  93. my $class=(lc($params{open}) ne "yes") ? "toggleable" : "toggleable-open";
  94. # Should really be a postprocessor directive, oh well. Work around
  95. # markdown's dislike of markdown inside a <div> with various funky
  96. # whitespace.
  97. my ($indent)=$params{text}=~/( +)$/;
  98. $indent="" unless defined $indent;
  99. return "<div class=\"$class\" id=\"$id\"></div>\n\n$params{text}\n$indent<div class=\"toggleableend\"></div>";
  100. } # }}}
  101. sub format (@) { #{{{
  102. my %params=@_;
  103. if ($params{content}=~s!(<div class="toggleable(?:-open)?" id="[^"]+">\s*)</div>!$1!g) {
  104. $params{content}=~s/<div class="toggleableend">//g;
  105. if (! ($params{content}=~s!^<body>!<body>$javascript!m)) {
  106. # no </body> tag, probably in preview mode
  107. $params{content}=$javascript.$params{content};
  108. }
  109. }
  110. return $params{content};
  111. } # }}}
  112. 1