summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/toc.pm
blob: b8537d3eb02ac47e74fa13e8ca81b5df56fad424 (plain)
  1. #!/usr/bin/perl
  2. # Table Of Contents generator
  3. package IkiWiki::Plugin::toc;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 3.00;
  7. use HTML::Parser;
  8. sub import {
  9. hook(type => "getsetup", id => "toc", call => \&getsetup);
  10. hook(type => "preprocess", id => "toc", call => \&preprocess);
  11. hook(type => "format", id => "toc", call => \&format);
  12. }
  13. sub getsetup () {
  14. return
  15. plugin => {
  16. safe => 1,
  17. rebuild => undef,
  18. },
  19. }
  20. my %tocpages;
  21. sub preprocess (@) {
  22. my %params=@_;
  23. if ($params{page} eq $params{destpage}) {
  24. $params{levels}=1 unless exists $params{levels};
  25. # It's too early to generate the toc here, so just record the
  26. # info.
  27. $tocpages{$params{destpage}}=\%params;
  28. return "\n<div class=\"toc\"></div>\n";
  29. }
  30. else {
  31. # Don't generate toc in an inlined page, doesn't work
  32. # right.
  33. return "";
  34. }
  35. }
  36. sub format (@) {
  37. my %params=@_;
  38. my $content=$params{content};
  39. return $content unless exists $tocpages{$params{page}};
  40. %params=%{$tocpages{$params{page}}};
  41. my $p=HTML::Parser->new(api_version => 3);
  42. my $page="";
  43. my $index="";
  44. my %anchors;
  45. my $startlevel=($params{startlevel} ? $params{startlevel} : 0);
  46. my $curlevel=$startlevel-1;
  47. my $liststarted=0;
  48. my $indent=sub { "\t" x $curlevel };
  49. $p->handler(start => sub {
  50. my $tagname=shift;
  51. my $text=shift;
  52. if ($tagname =~ /^h(\d+)$/i) {
  53. my $level=$1;
  54. my $anchor="index".++$anchors{$level}."h$level";
  55. $page.="$text<a name=\"$anchor\"></a>";
  56. # Unless we're given startlevel as a parameter,
  57. # take the first header level seen as the topmost level,
  58. # even if there are higher levels seen later on.
  59. if (! $startlevel) {
  60. $startlevel=$level;
  61. $curlevel=$startlevel-1;
  62. }
  63. elsif (defined $params{startlevel} &&
  64. $level < $params{startlevel}) {
  65. return;
  66. }
  67. elsif ($level < $startlevel) {
  68. $level=$startlevel;
  69. }
  70. return if $level - $startlevel >= $params{levels};
  71. if ($level > $curlevel) {
  72. while ($level > $curlevel + 1) {
  73. $index.=&$indent."<ol>\n";
  74. $curlevel++;
  75. $index.=&$indent."<li class=\"L$curlevel\">\n";
  76. }
  77. $index.=&$indent."<ol>\n";
  78. $curlevel=$level;
  79. $liststarted=1;
  80. }
  81. elsif ($level < $curlevel) {
  82. while ($level < $curlevel) {
  83. $index.=&$indent."</li>\n" if $curlevel;
  84. $curlevel--;
  85. $index.=&$indent."</ol>\n";
  86. }
  87. $liststarted=0;
  88. }
  89. $index.=&$indent."</li>\n" unless $liststarted;
  90. $liststarted=0;
  91. $index.=&$indent."<li class=\"L$curlevel\">".
  92. "<a href=\"#$anchor\">";
  93. $p->handler(text => sub {
  94. $page.=join("", @_);
  95. $index.=join("", @_);
  96. }, "dtext");
  97. $p->handler(end => sub {
  98. my $tagname=shift;
  99. if ($tagname =~ /^h(\d+)$/i) {
  100. $p->handler(text => undef);
  101. $p->handler(end => undef);
  102. $index.="</a>\n";
  103. }
  104. $page.=join("", @_);
  105. }, "tagname, text");
  106. }
  107. else {
  108. $page.=$text;
  109. }
  110. }, "tagname, text");
  111. $p->handler(default => sub { $page.=join("", @_) }, "text");
  112. $p->parse($content);
  113. $p->eof;
  114. while ($startlevel && $curlevel >= $startlevel) {
  115. $index.=&$indent."</li>\n" if $curlevel;
  116. $curlevel--;
  117. $index.=&$indent."</ol>\n";
  118. }
  119. $page=~s/(<div class=\"toc\">)/$1\n$index/;
  120. return $page;
  121. }
  122. 1