summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/map.pm
blob: af08005b8e4138e7a1791b165347e5bc97b8ce5c (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. if (exists $params{show} &&
  24. exists $pagestate{$page}{meta}{$params{show}}) {
  25. $mapitems{$page}=$pagestate{$page}{meta}{$params{show}};
  26. }
  27. else {
  28. $mapitems{$page}=$page;
  29. }
  30. # Check for a common prefix.
  31. if (! defined $common_prefix) {
  32. $common_prefix=$page;
  33. }
  34. elsif (length $common_prefix &&
  35. $page !~ /^\Q$common_prefix\E(\/|$)/) {
  36. my @a=split(/\//, $page);
  37. my @b=split(/\//, $common_prefix);
  38. $common_prefix="";
  39. while (@a && @b && $a[0] eq $b[0]) {
  40. if (length $common_prefix) {
  41. $common_prefix.="/";
  42. }
  43. $common_prefix.=shift(@a);
  44. shift @b;
  45. }
  46. }
  47. }
  48. }
  49. # Common prefix should not be a page in the map.
  50. while (defined $common_prefix && length $common_prefix &&
  51. exists $mapitems{$common_prefix}) {
  52. $common_prefix=IkiWiki::dirname($common_prefix);
  53. }
  54. # Needs to update whenever a page is added or removed, so
  55. # register a dependency.
  56. add_depends($params{page}, $params{pages});
  57. # Explicitly add all currently shown pages, to detect when pages
  58. # are removed.
  59. add_depends($params{page}, join(" or ", keys %mapitems));
  60. # Create the map.
  61. my $parent="";
  62. my $indent=0;
  63. my $openli=0;
  64. my $dummy=0;
  65. my $map = "<div class='map'>\n<ul>\n";
  66. foreach my $item (sort { $mapitems{$a} cmp $mapitems{$b} } keys %mapitems) {
  67. $item=~s/^\Q$common_prefix\E\///
  68. if defined $common_prefix && length $common_prefix;
  69. my $depth = ($item =~ tr/\//\//) + 1;
  70. my $baseitem=IkiWiki::dirname($item);
  71. while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
  72. $parent=IkiWiki::dirname($parent);
  73. last if !$dummy && length $parent && $baseitem =~ /^\Q$parent\E(\/|$)/;
  74. $indent--;
  75. $map .= "</li>\n";
  76. if ($indent > 0) {
  77. $map .= "</ul>\n";
  78. }
  79. }
  80. $dummy=0;
  81. while ($depth < $indent) {
  82. $indent--;
  83. $map .= "</li>\n";
  84. if ($indent > 0) {
  85. $map .= "</ul>\n";
  86. }
  87. }
  88. my @bits=split("/", $item);
  89. my $p="";
  90. $p.="/".shift(@bits) for 1..$indent;
  91. while ($depth > $indent) {
  92. $indent++;
  93. if ($indent > 1) {
  94. $map .= "<ul>\n";
  95. }
  96. if ($depth > $indent) {
  97. $dummy=1;
  98. $p.="/".shift(@bits);
  99. $map .= "<li>"
  100. .htmllink($params{page}, $params{destpage},
  101. $p, class => "mapparent",
  102. noimageinline => 1)
  103. ."\n";
  104. $openli=1;
  105. }
  106. else {
  107. $openli=0;
  108. }
  109. }
  110. $map .= "</li>\n" if $openli;
  111. $map .= "<li>"
  112. .htmllink($params{page}, $params{destpage},
  113. "/".$common_prefix."/".$item,
  114. linktext => $mapitems{$item},
  115. class => "mapitem", noimageinline => 1)
  116. ."\n";
  117. $openli=1;
  118. $parent=$item;
  119. }
  120. while ($indent > 0) {
  121. $indent--;
  122. $map .= "</li>\n</ul>\n";
  123. }
  124. $map .= "</div>\n";
  125. return $map;
  126. } # }}}
  127. 1