summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/map.pm
blob: 120451b5dbde60a26ae165e391cf56232cc30111 (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<ul>\n";
  75. foreach my $item (sort keys %mapitems) {
  76. my @linktext = (length $mapitems{$item} ? (linktext => $mapitems{$item}) : ());
  77. $item=~s/^\Q$common_prefix\E\///
  78. if defined $common_prefix && length $common_prefix;
  79. my $depth = ($item =~ tr/\//\//) + 1;
  80. my $baseitem=IkiWiki::dirname($item);
  81. while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
  82. $parent=IkiWiki::dirname($parent);
  83. last if length $addparent && $baseitem =~ /^\Q$addparent\E(\/|$)/;
  84. $addparent="";
  85. $indent--;
  86. $map .= "</li>\n";
  87. if ($indent > 0) {
  88. $map .= "</ul>\n";
  89. }
  90. }
  91. while ($depth < $indent) {
  92. $indent--;
  93. $map .= "</li>\n";
  94. if ($indent > 0) {
  95. $map .= "</ul>\n";
  96. }
  97. }
  98. my @bits=split("/", $item);
  99. my $p="";
  100. $p.="/".shift(@bits) for 1..$indent;
  101. while ($depth > $indent) {
  102. $indent++;
  103. if ($indent > 1) {
  104. $map .= "<ul>\n";
  105. }
  106. if ($depth > $indent) {
  107. $p.="/".shift(@bits);
  108. $addparent=$p;
  109. $addparent=~s/^\///;
  110. $map .= "<li>"
  111. .htmllink($params{page}, $params{destpage},
  112. "/".$common_prefix.$p, class => "mapparent",
  113. noimageinline => 1)
  114. ."\n";
  115. $openli=1;
  116. }
  117. else {
  118. $openli=0;
  119. }
  120. }
  121. $map .= "</li>\n" if $openli;
  122. $map .= "<li>"
  123. .htmllink($params{page}, $params{destpage},
  124. "/".$common_prefix."/".$item,
  125. @linktext,
  126. class => "mapitem", noimageinline => 1)
  127. ."\n";
  128. $openli=1;
  129. $parent=$item;
  130. }
  131. while ($indent > 0) {
  132. $indent--;
  133. $map .= "</li>\n</ul>\n";
  134. }
  135. $map .= "</div>\n";
  136. return $map;
  137. }
  138. 1