summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/map.pm
blob: 4c2a15b019dab6fdab77ac9a3a7c5144ae5fe3d3 (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 2.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 (keys %pagesources) {
  30. if (pagespec_match($page, $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. }
  58. # Common prefix should not be a page in the map.
  59. while (defined $common_prefix && length $common_prefix &&
  60. exists $mapitems{$common_prefix}) {
  61. $common_prefix=IkiWiki::dirname($common_prefix);
  62. }
  63. # Needs to update whenever a page is added or removed (or in some
  64. # cases, when its content changes, if show=title), so register a
  65. # dependency.
  66. add_depends($params{page}, $params{pages});
  67. # Explicitly add all currently shown pages, to detect when pages
  68. # are removed.
  69. add_depends($params{page}, join(" or ", keys %mapitems));
  70. # Create the map.
  71. my $parent="";
  72. my $indent=0;
  73. my $openli=0;
  74. my $dummy=0;
  75. my $map = "<div class='map'>\n<ul>\n";
  76. foreach my $item (sort keys %mapitems) {
  77. my @linktext = (length $mapitems{$item} ? (linktext => $mapitems{$item}) : ());
  78. $item=~s/^\Q$common_prefix\E\///
  79. if defined $common_prefix && length $common_prefix;
  80. my $depth = ($item =~ tr/\//\//) + 1;
  81. my $baseitem=IkiWiki::dirname($item);
  82. print STDERR "!! parent: $parent baseitem: $baseitem\n";
  83. while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
  84. $parent=IkiWiki::dirname($parent);
  85. last if !$dummy && length $parent && $baseitem =~ /^\Q$parent\E(\/|$)/;
  86. $indent--;
  87. $map .= "</li>\n";
  88. if ($indent > 0) {
  89. $map .= "</ul>\n";
  90. }
  91. }
  92. $dummy=0;
  93. while ($depth < $indent) {
  94. $indent--;
  95. $map .= "</li>\n";
  96. if ($indent > 0) {
  97. $map .= "</ul>\n";
  98. }
  99. }
  100. my @bits=split("/", $item);
  101. my $p="";
  102. $p.="/".shift(@bits) for 1..$indent;
  103. while ($depth > $indent) {
  104. $indent++;
  105. if ($indent > 1) {
  106. $map .= "<ul>\n";
  107. }
  108. if ($depth > $indent) {
  109. $p.="/".shift(@bits);
  110. #$p=~s/^\///;
  111. $map .= "<li>"
  112. .htmllink($params{page}, $params{destpage},
  113. "/".$common_prefix.$p, class => "mapparent",
  114. noimageinline => 1)
  115. ."\n";
  116. $openli=1;
  117. $dummy=1;
  118. }
  119. else {
  120. $openli=0;
  121. }
  122. }
  123. $map .= "</li>\n" if $openli;
  124. $map .= "<li>"
  125. .htmllink($params{page}, $params{destpage},
  126. "/".$common_prefix."/".$item,
  127. @linktext,
  128. class => "mapitem", noimageinline => 1)
  129. ."\n";
  130. $openli=1;
  131. $parent=$item;
  132. }
  133. while ($indent > 0) {
  134. $indent--;
  135. $map .= "</li>\n</ul>\n";
  136. }
  137. $map .= "</div>\n";
  138. return $map;
  139. } # }}}
  140. 1