summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/shortcut.pm
blob: 26385f3a6e83569ab0d89e8d1e968fc535d8776d (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. #translators: This is used to display what shortcuts are defined.
  24. #translators: First parameter is the name of the shortcut, the second
  25. #translators: is an URL.
  26. return sprintf(gettext("shortcut %s points to %s"), $params{name}, $params{url});
  27. } # }}}
  28. sub shortcut_expand ($$@) { #{{{
  29. my $url=shift;
  30. my $desc=shift;
  31. my %params=@_;
  32. # Get params in original order.
  33. my @params;
  34. while (@_) {
  35. my $key=shift;
  36. my $value=shift;
  37. push @params, $key if ! length $value;
  38. }
  39. # If the shortcuts page changes, all pages that use shortcuts will
  40. # need to be updated.
  41. add_depends($params{destpage}, "shortcuts");
  42. my $text=join(" ", @params);
  43. my $encoded_text=$text;
  44. $encoded_text=~s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
  45. $text=~s/_/ /g;
  46. if (defined $desc) {
  47. $desc=~s/\%s/$text/g;
  48. }
  49. else {
  50. $desc=$text;
  51. }
  52. $url=~s/\%s/$encoded_text/g;
  53. return "<a href=\"$url\">$desc</a>";
  54. } #}}}
  55. 1