summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/map.pm
blob: 19872e51cafe24973ee98217c06050b463715b41 (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},
  66. deptype(exists $params{show} ? "content" : "presence");
  67. # Create the map.
  68. my $parent="";
  69. my $indent=0;
  70. my $openli=0;
  71. my $addparent="";
  72. my $map = "<div class='map'>\n";
  73. # Return empty div if %mapitems is empty
  74. if (!scalar(keys %mapitems)) {
  75. $map .= "</div>\n";
  76. return $map;
  77. }
  78. else { # continue populating $map
  79. $map .= "<ul>\n";
  80. }
  81. foreach my $item (sort keys %mapitems) {
  82. my @linktext = (length $mapitems{$item} ? (linktext => $mapitems{$item}) : ());
  83. $item=~s/^\Q$common_prefix\E\///
  84. if defined $common_prefix && length $common_prefix;
  85. my $depth = ($item =~ tr/\//\//) + 1;
  86. my $baseitem=IkiWiki::dirname($item);
  87. while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
  88. $parent=IkiWiki::dirname($parent);
  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. $p.="/".shift(@bits) for 1..$indent;
  107. while ($depth > $indent) {
  108. $indent++;
  109. if ($indent > 1) {
  110. $map .= "<ul>\n";
  111. }
  112. if ($depth > $indent) {
  113. $p.="/".shift(@bits);
  114. $addparent=$p;
  115. $addparent=~s/^\///;
  116. $map .= "<li>"
  117. .htmllink($params{page}, $params{destpage},
  118. "/".$common_prefix.$p, class => "mapparent",
  119. noimageinline => 1)
  120. ."\n";
  121. $openli=1;
  122. }
  123. else {
  124. $openli=0;
  125. }
  126. }
  127. $map .= "</li>\n" if $openli;
  128. $map .= "<li>"
  129. .htmllink($params{page}, $params{destpage},
  130. "/".$common_prefix."/".$item,
  131. @linktext,
  132. class => "mapitem", noimageinline => 1)
  133. ."\n";
  134. $openli=1;
  135. $parent=$item;
  136. }
  137. while ($indent > 0) {
  138. $indent--;
  139. $map .= "</li>\n</ul>\n";
  140. }
  141. $map .= "</div>\n";
  142. return $map;
  143. }
  144. 1