summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/shortcut.pm
blob: 9479306a8c37d4558b05cead14ee47119c9477e9 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::shortcut;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. sub import { #{{{
  7. hook(type => "checkconfig", id => "shortcut", call => \&checkconfig);
  8. hook(type => "preprocess", id => "shortcut", call => \&preprocess_shortcut);
  9. } #}}}
  10. sub checkconfig () { #{{{
  11. # Preprocess the shortcuts page to get all the available shortcuts
  12. # defined before other pages are rendered.
  13. IkiWiki::preprocess("shortcuts", "shortcuts",
  14. readfile(srcfile("shortcuts.mdwn")));
  15. } # }}}
  16. sub preprocess_shortcut (@) { #{{{
  17. my %params=@_;
  18. if (! defined $params{name} || ! defined $params{url}) {
  19. return "[[".gettext("shortcut missing name or url parameter")."]]";
  20. }
  21. hook(type => "preprocess", no_override => 1, id => $params{name},
  22. call => sub { shortcut_expand($params{url}, $params{desc}, @_) });
  23. return sprintf(gettext("shortcut %s points to %s"), $params{name}, $params{url});
  24. } # }}}
  25. sub shortcut_expand ($$@) { #{{{
  26. my $url=shift;
  27. my $desc=shift;
  28. my %params=@_;
  29. # Get params in original order.
  30. my @params;
  31. while (@_) {
  32. my $key=shift;
  33. my $value=shift;
  34. push @params, $key if ! length $value;
  35. }
  36. # If the shortcuts page changes, all pages that use shortcuts will
  37. # need to be updated.
  38. add_depends($params{destpage}, "shortcuts");
  39. my $text=join(" ", @params);
  40. my $encoded_text=$text;
  41. $encoded_text=~s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
  42. if (defined $desc) {
  43. $desc=~s/\%s/$text/g;
  44. }
  45. else {
  46. $desc=$text;
  47. }
  48. $url=~s/\%s/$encoded_text/g;
  49. return "<a href=\"$url\">$desc</a>";
  50. } #}}}
  51. 1