summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/map.pm
blob: 826dbbd66ba68dee4401b259c2d3b87ae7e5d1ea (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 3.00;
  12. sub import {
  13. hook(type => "getsetup", id => "map", call => \&getsetup);
  14. hook(type => "preprocess", id => "map", call => \&preprocess);
  15. }
  16. sub getsetup () {
  17. return
  18. plugin => {
  19. safe => 1,
  20. rebuild => undef,
  21. },
  22. }
  23. sub preprocess (@) {
  24. my %params=@_;
  25. $params{pages}="*" unless defined $params{pages};
  26. my $common_prefix;
  27. # Get all the items to map.
  28. my %mapitems;
  29. foreach my $page (pagespec_match_list([keys %pagesources],
  30. $params{pages}, location => $params{page})) {
  31. if (exists $params{show} &&
  32. exists $pagestate{$page} &&
  33. exists $pagestate{$page}{meta}{$params{show}}) {
  34. $mapitems{$page}=$pagestate{$page}{meta}{$params{show}};
  35. }
  36. else {
  37. $mapitems{$page}='';
  38. }
  39. # Check for a common prefix.
  40. if (! defined $common_prefix) {
  41. $common_prefix=$page;
  42. }
  43. elsif (length $common_prefix &&
  44. $page !~ /^\Q$common_prefix\E(\/|$)/) {
  45. my @a=split(/\//, $page);
  46. my @b=split(/\//, $common_prefix);
  47. $common_prefix="";
  48. while (@a && @b && $a[0] eq $b[0]) {
  49. if (length $common_prefix) {
  50. $common_prefix.="/";
  51. }
  52. $common_prefix.=shift(@a);
  53. shift @b;
  54. }
  55. }
  56. }
  57. # Common prefix should not be a page in the map.
  58. while (defined $common_prefix && length $common_prefix &&
  59. exists $mapitems{$common_prefix}) {
  60. $common_prefix=IkiWiki::dirname($common_prefix);
  61. }
  62. # Needs to update whenever a page is added or removed (or in some
  63. # cases, when its content changes, if show=title), so register a
  64. # dependency.
  65. add_depends($params{page}, $params{pages});
  66. # Explicitly add all currently shown pages, to detect when pages
  67. # are removed.
  68. add_depends($params{page}, join(" or ", keys %mapitems));
  69. # Create the map.
  70. my $parent="";
  71. my $indent=0;
  72. my $openli=0;
  73. my $addparent="";
  74. my $map = "<div class='map'>\n";
  75. # Return empty div if %mapitems is empty
  76. if (!scalar(keys %mapitems)) {
  77. $map .= "</div>\n";
  78. return $map;
  79. }
  80. else { # continue populating $map
  81. $map .= "<ul>\n";
  82. }
  83. foreach my $item (sort keys %mapitems) {
  84. my @linktext = (length $mapitems{$item} ? (linktext => $mapitems{$item}) : ());
  85. $item=~s/^\Q$common_prefix\E\///
  86. if defined $common_prefix && length $common_prefix;
  87. my $depth = ($item =~ tr/\//\//) + 1;
  88. my $baseitem=IkiWiki::dirname($item);
  89. while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
  90. $parent=IkiWiki::dirname($parent);
  91. last if length $addparent && $baseitem =~ /^\Q$addparent\E(\/|$)/;
  92. $addparent="";
  93. $indent--;
  94. $map .= "</li>\n";
  95. if ($indent > 0) {
  96. $map .= "</ul>\n";
  97. }
  98. }
  99. while ($depth < $indent) {
  100. $indent--;
  101. $map .= "</li>\n";
  102. if ($indent > 0) {
  103. $map .= "</ul>\n";
  104. }
  105. }
  106. my @bits=split("/", $item);
  107. my $p="";
  108. $p.="/".shift(@bits) for 1..$indent;
  109. while ($depth > $indent) {
  110. $indent++;
  111. if ($indent > 1) {
  112. $map .= "<ul>\n";
  113. }
  114. if ($depth > $indent) {
  115. $p.="/".shift(@bits);
  116. $addparent=$p;
  117. $addparent=~s/^\///;
  118. $map .= "<li>"
  119. .htmllink($params{page}, $params{destpage},
  120. "/".$common_prefix.$p, class => "mapparent",
  121. noimageinline => 1)
  122. ."\n";
  123. $openli=1;
  124. }
  125. else {
  126. $openli=0;
  127. }
  128. }
  129. $map .= "</li>\n" if $openli;
  130. $map .= "<li>"
  131. .htmllink($params{page}, $params{destpage},
  132. "/".$common_prefix."/".$item,
  133. @linktext,
  134. class => "mapitem", noimageinline => 1)
  135. ."\n";
  136. $openli=1;
  137. $parent=$item;
  138. }
  139. while ($indent > 0) {
  140. $indent--;
  141. $map .= "</li>\n</ul>\n";
  142. }
  143. $map .= "</div>\n";
  144. return $map;
  145. }
  146. 1