summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/toc.pm
blob: 460837b1d44587928702198a2fa38110ad6b76cd (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 => "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 $curlevel;
  46. my $startlevel=0;
  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. # Take the first header level seen as the topmost level,
  57. # even if there are higher levels seen later on.
  58. if (! $startlevel) {
  59. $startlevel=$level;
  60. $curlevel=$startlevel-1;
  61. }
  62. elsif ($level < $startlevel) {
  63. $level=$startlevel;
  64. }
  65. return if $level - $startlevel >= $params{levels};
  66. if ($level > $curlevel) {
  67. while ($level > $curlevel + 1) {
  68. $index.=&$indent."<ol>\n";
  69. $curlevel++;
  70. $index.=&$indent."<li class=\"L$curlevel\">\n";
  71. }
  72. $index.=&$indent."<ol>\n";
  73. $curlevel=$level;
  74. $liststarted=1;
  75. }
  76. elsif ($level < $curlevel) {
  77. while ($level < $curlevel) {
  78. $index.=&$indent."</li>\n" if $curlevel;
  79. $curlevel--;
  80. $index.=&$indent."</ol>\n";
  81. }
  82. $liststarted=0;
  83. }
  84. $index.=&$indent."</li>\n" unless $liststarted;
  85. $liststarted=0;
  86. $index.=&$indent."<li class=\"L$curlevel\">".
  87. "<a href=\"#$anchor\">";
  88. $p->handler(text => sub {
  89. $page.=join("", @_);
  90. $index.=join("", @_);
  91. }, "dtext");
  92. $p->handler(end => sub {
  93. my $tagname=shift;
  94. if ($tagname =~ /^h(\d+)$/i) {
  95. $p->handler(text => undef);
  96. $p->handler(end => undef);
  97. $index.="</a>\n";
  98. }
  99. $page.=join("", @_);
  100. }, "tagname, text");
  101. }
  102. else {
  103. $page.=$text;
  104. }
  105. }, "tagname, text");
  106. $p->handler(default => sub { $page.=join("", @_) }, "text");
  107. $p->parse($content);
  108. $p->eof;
  109. while ($startlevel && $curlevel >= $startlevel) {
  110. $index.=&$indent."</li>\n" if $curlevel;
  111. $curlevel--;
  112. $index.=&$indent."</ol>\n";
  113. }
  114. $page=~s/(<div class=\"toc\">)/$1\n$index/;
  115. return $page;
  116. }
  117. 1