summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/map.pm
blob: 46f11dc8948ea66b63a72e65b2e6d34910b45fc4 (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= is specified), so
  64. # register a dependency.
  65. add_depends($params{page}, $params{pages}, presence => ! exists $params{show});
  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. # Return empty div if %mapitems is empty
  73. if (!scalar(keys %mapitems)) {
  74. $map .= "</div>\n";
  75. return $map;
  76. }
  77. else { # continue populating $map
  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. while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
  87. $parent=IkiWiki::dirname($parent);
  88. last if length $addparent && $baseitem =~ /^\Q$addparent\E(\/|$)/;
  89. $addparent="";
  90. $indent--;
  91. $map .= "</li>\n";
  92. if ($indent > 0) {
  93. $map .= "</ul>\n";
  94. }
  95. }
  96. while ($depth < $indent) {
  97. $indent--;
  98. $map .= "</li>\n";
  99. if ($indent > 0) {
  100. $map .= "</ul>\n";
  101. }
  102. }
  103. my @bits=split("/", $item);
  104. my $p="";
  105. $p.="/".shift(@bits) for 1..$indent;
  106. while ($depth > $indent) {
  107. $indent++;
  108. if ($indent > 1) {
  109. $map .= "<ul>\n";
  110. }
  111. if ($depth > $indent) {
  112. $p.="/".shift(@bits);
  113. $addparent=$p;
  114. $addparent=~s/^\///;
  115. $map .= "<li>"
  116. .htmllink($params{page}, $params{destpage},
  117. "/".$common_prefix.$p, class => "mapparent",
  118. noimageinline => 1)
  119. ."\n";
  120. $openli=1;
  121. }
  122. else {
  123. $openli=0;
  124. }
  125. }
  126. $map .= "</li>\n" if $openli;
  127. $map .= "<li>"
  128. .htmllink($params{page}, $params{destpage},
  129. "/".$common_prefix."/".$item,
  130. @linktext,
  131. class => "mapitem", noimageinline => 1)
  132. ."\n";
  133. $openli=1;
  134. $parent=$item;
  135. }
  136. while ($indent > 0) {
  137. $indent--;
  138. $map .= "</li>\n</ul>\n";
  139. }
  140. $map .= "</div>\n";
  141. return $map;
  142. }
  143. 1