summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/shortcut.pm
blob: 0e7769c67c7f22f36b9b1e857660cc9b8ba0ad9c (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::shortcut;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  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 ".gettext("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 <i>%s</i>"), $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. $url=~s{\%([sS])}{
  46. $1 eq 's' ? $encoded_text : $text
  47. }eg;
  48. $text=~s/_/ /g;
  49. if (defined $desc) {
  50. $desc=~s/\%s/$text/g;
  51. }
  52. else {
  53. $desc=$text;
  54. }
  55. return "<a href=\"$url\">$desc</a>";
  56. } #}}}
  57. 1