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