summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/map.pm
blob: 5b6af1f36f6ce0c6cc9605365ed491d123d09efd (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},
  92. $p, class => "mapparent",
  93. noimageinline => 1)
  94. ."\n";
  95. $openli=1;
  96. }
  97. else {
  98. $openli=0;
  99. }
  100. }
  101. $map .= "</li>\n" if $openli;
  102. $map .= "<li>"
  103. .htmllink($params{page}, $params{destpage},
  104. "/".$common_prefix."/".$item,
  105. class => "mapitem", noimageinline => 1)
  106. ."\n";
  107. $openli=1;
  108. $parent=$item;
  109. }
  110. while ($indent > 0) {
  111. $indent--;
  112. $map .= "</li>\n</ul>\n";
  113. }
  114. $map .= "</div>\n";
  115. return $map;
  116. } # }}}
  117. 1