summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/map.pm
blob: 97450eb6dd988fbe2d04cf684528658406ca4ddb (plain)
  1. #!/usr/bin/perl
  2. #
  3. # Produce a hierarchical map of links.
  4. #
  5. # by Alessandro Dotti Contra <alessandro@hyboria.org>
  6. #
  7. # Revision: 0.2
  8. package IkiWiki::Plugin::map;
  9. use warnings;
  10. use strict;
  11. use IkiWiki 2.00;
  12. sub import { #{{{
  13. hook(type => "preprocess", id => "map", call => \&preprocess);
  14. } # }}}
  15. sub preprocess (@) { #{{{
  16. my %params=@_;
  17. $params{pages}="*" unless defined $params{pages};
  18. my $common_prefix;
  19. # Get all the items to map.
  20. my %mapitems;
  21. foreach my $page (keys %pagesources) {
  22. if (pagespec_match($page, $params{pages}, location => $params{page})) {
  23. $mapitems{$page}=1;
  24. # Check for a common prefix.
  25. if (! defined $common_prefix) {
  26. $common_prefix=$page;
  27. }
  28. elsif (length $common_prefix &&
  29. $page !~ /^\Q$common_prefix\E(\/|$)/) {
  30. my @a=split(/\//, $page);
  31. my @b=split(/\//, $common_prefix);
  32. $common_prefix="";
  33. while (@a && @b && $a[0] eq $b[0]) {
  34. $common_prefix.=shift(@a);
  35. shift @b;
  36. }
  37. }
  38. }
  39. }
  40. # Common prefix should not be a page in the map.
  41. while (length $common_prefix && exists $mapitems{$common_prefix}) {
  42. $common_prefix=IkiWiki::dirname($common_prefix);
  43. }
  44. # Needs to update whenever a page is added or removed, so
  45. # register a dependency.
  46. add_depends($params{page}, $params{pages});
  47. # Explicitly add all currently shown pages, to detect when pages
  48. # are removed.
  49. add_depends($params{page}, join(" or ", keys %mapitems));
  50. # Create the map.
  51. my $parent="";
  52. my $indent=0;
  53. my $openli=0;
  54. my $map = "<div class='map'>\n<ul>\n";
  55. foreach my $item (sort keys %mapitems) {
  56. $item=~s/^\Q$common_prefix\E\/// if length $common_prefix;
  57. my $depth = ($item =~ tr/\//\//) + 1;
  58. my $baseitem=IkiWiki::dirname($item);
  59. while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
  60. $parent=IkiWiki::dirname($parent);
  61. $indent--;
  62. $map .= "</li>\n";
  63. if ($indent > 0) {
  64. $map .= "</ul>\n";
  65. }
  66. }
  67. while ($depth < $indent) {
  68. $indent--;
  69. $map .= "</li>\n";
  70. if ($indent > 0) {
  71. $map .= "</ul>\n";
  72. }
  73. }
  74. my @bits=split("/", $item);
  75. my $p="";
  76. $p.="/".shift(@bits) for 1..$indent;
  77. while ($depth > $indent) {
  78. $indent++;
  79. if ($indent > 1) {
  80. $map .= "<ul>\n";
  81. }
  82. if ($depth > $indent) {
  83. $p.="/".shift(@bits);
  84. $map .= "<li>"
  85. .htmllink($params{page}, $params{destpage}, $p, class => "mapparent")
  86. ."</span>\n";
  87. $openli=1;
  88. }
  89. else {
  90. $openli=0;
  91. }
  92. }
  93. $map .= "</li>\n" if $openli;
  94. $map .= "<li>"
  95. .htmllink($params{page}, $params{destpage},
  96. "/".$common_prefix."/".$item, class => "mapitem")
  97. ."</span>\n";
  98. $openli=1;
  99. $parent=$item;
  100. }
  101. while ($indent > 0) {
  102. $indent--;
  103. $map .= "</li>\n</ul>\n";
  104. }
  105. $map .= "</div>\n";
  106. return $map;
  107. } # }}}
  108. 1