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