summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/shortcut.pm
blob: d09d5879d27b5f5c640bedf259b65a0ee1e426ec (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 "[[shortcut missing name or url parameter]]";
  20. }
  21. hook(type => "preprocess", no_override => 1, id => $params{name},
  22. call => sub { shortcut_expand($params{name}, $params{url}, @_) });
  23. return "shortcut $params{name} points to $params{url}";
  24. } # }}}
  25. sub shortcut_expand ($$@) { #{{{
  26. my $name=shift;
  27. my $url=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. $url=~s/\%s/$encoded_text/g;
  43. return "<a href=\"$url\">$text</a>";
  44. } #}}}
  45. 1