summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/transient.pm
blob: c0ad5fc11476ef4442cb64e81c7f5580e3795942 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::transient;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.00;
  6. sub import {
  7. hook(type => "getsetup", id => "transient", call => \&getsetup);
  8. hook(type => "checkconfig", id => "transient", call => \&checkconfig);
  9. hook(type => "change", id => "transient", call => \&change);
  10. }
  11. sub getsetup () {
  12. return
  13. plugin => {
  14. # this plugin is safe but only makes sense as a
  15. # dependency; similarly, it needs a rebuild but
  16. # only if something else does
  17. safe => 0,
  18. rebuild => 0,
  19. },
  20. }
  21. our $transientdir;
  22. sub checkconfig () {
  23. if (defined $config{wikistatedir}) {
  24. $transientdir = $config{wikistatedir}."/transient";
  25. # add_underlay treats relative underlays as relative to the installed
  26. # location, not the cwd. That's not what we want here.
  27. IkiWiki::add_literal_underlay($transientdir);
  28. }
  29. }
  30. sub change (@) {
  31. foreach my $file (@_) {
  32. # If the corresponding file exists in the transient underlay
  33. # and isn't actually being used, we can get rid of it.
  34. # Assume that the file that just changed has the same extension
  35. # as the obsolete transient version: this'll be true for web
  36. # edits, and avoids invoking File::Find.
  37. my $casualty = "$transientdir/$file";
  38. if (srcfile($file) ne $casualty && -e $casualty) {
  39. debug(sprintf(gettext("removing transient version of %s"), $file));
  40. IkiWiki::prune($casualty);
  41. }
  42. }
  43. }
  44. 1;