summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/map.pm
blob: b08d78d76dc6752ee37012bb3df707eba7faefc4 (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 (defined $common_prefix && length $common_prefix &&
  42. exists $mapitems{$common_prefix}) {
  43. $common_prefix=IkiWiki::dirname($common_prefix);
  44. }
  45. # Needs to update whenever a page is added or removed, so
  46. # register a dependency.
  47. add_depends($params{page}, $params{pages});
  48. # Explicitly add all currently shown pages, to detect when pages
  49. # are removed.
  50. add_depends($params{page}, join(" or ", keys %mapitems));
  51. # Create the map.
  52. my $parent="";
  53. my $indent=0;
  54. my $openli=0;
  55. my $dummy=0;
  56. my $map = "<div class='map'>\n<ul>\n";
  57. foreach my $item (sort keys %mapitems) {
  58. $item=~s/^\Q$common_prefix\E\///
  59. if defined $common_prefix && length $common_prefix;
  60. my $depth = ($item =~ tr/\//\//) + 1;
  61. my $baseitem=IkiWiki::dirname($item);
  62. while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
  63. $parent=IkiWiki::dirname($parent);
  64. last if !$dummy && length $parent && $baseitem =~ /^\Q$parent\E(\/|$)/;
  65. $indent--;
  66. $map .= "</li>\n";
  67. if ($indent > 0) {
  68. $map .= "</ul>\n";
  69. }
  70. }
  71. $dummy=0;
  72. while ($depth < $indent) {
  73. $indent--;
  74. $map .= "</li>\n";
  75. if ($indent > 0) {
  76. $map .= "</ul>\n";
  77. }
  78. }
  79. my @bits=split("/", $item);
  80. my $p="";
  81. $p.="/".shift(@bits) for 1..$indent;
  82. while ($depth > $indent) {
  83. $indent++;
  84. if ($indent > 1) {
  85. $map .= "<ul>\n";
  86. }
  87. if ($depth > $indent) {
  88. $dummy=1;
  89. $p.="/".shift(@bits);
  90. $map .= "<li>"
  91. .htmllink($params{page}, $params{destpage}, $p, class => "mapparent")
  92. ."\n";
  93. $openli=1;
  94. }
  95. else {
  96. $openli=0;
  97. }
  98. }
  99. $map .= "</li>\n" if $openli;
  100. $map .= "<li>"
  101. .htmllink($params{page}, $params{destpage},
  102. "/".$common_prefix."/".$item, class => "mapitem")
  103. ."\n";
  104. $openli=1;
  105. $parent=$item;
  106. }
  107. while ($indent > 0) {
  108. $indent--;
  109. $map .= "</li>\n</ul>\n";
  110. }
  111. $map .= "</div>\n";
  112. return $map;
  113. } # }}}
  114. 1