summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/shortcut.pm
blob: 8df60cfe2a34b5e428e1eafcae88ac9c71a3c0ed (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. my $srcfile=srcfile("shortcuts.mdwn", 1);
  14. if (! defined $srcfile) {
  15. error(gettext("shortcut plugin will not work without a shortcuts.mdwn"));
  16. }
  17. IkiWiki::preprocess("shortcuts", "shortcuts", readfile($srcfile));
  18. } # }}}
  19. sub preprocess_shortcut (@) { #{{{
  20. my %params=@_;
  21. if (! defined $params{name} || ! defined $params{url}) {
  22. error gettext("missing name or url parameter");
  23. }
  24. hook(type => "preprocess", no_override => 1, id => $params{name},
  25. call => sub { shortcut_expand($params{url}, $params{desc}, @_) });
  26. #translators: This is used to display what shortcuts are defined.
  27. #translators: First parameter is the name of the shortcut, the second
  28. #translators: is an URL.
  29. return sprintf(gettext("shortcut %s points to <i>%s</i>"), $params{name}, $params{url});
  30. } # }}}
  31. sub shortcut_expand ($$@) { #{{{
  32. my $url=shift;
  33. my $desc=shift;
  34. my %params=@_;
  35. # Get params in original order.
  36. my @params;
  37. while (@_) {
  38. my $key=shift;
  39. my $value=shift;
  40. push @params, $key if ! length $value;
  41. }
  42. # If the shortcuts page changes, all pages that use shortcuts will
  43. # need to be updated.
  44. add_depends($params{destpage}, "shortcuts");
  45. my $text=join(" ", @params);
  46. my $encoded_text=$text;
  47. $encoded_text=~s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
  48. $url=~s{\%([sS])}{
  49. $1 eq 's' ? $encoded_text : $text
  50. }eg;
  51. $text=~s/_/ /g;
  52. if (defined $params{desc}) {
  53. $desc=$params{desc};
  54. }
  55. if (defined $desc) {
  56. $desc=~s/\%s/$text/g;
  57. }
  58. else {
  59. $desc=$text;
  60. }
  61. return "<a href=\"$url\">$desc</a>";
  62. } #}}}
  63. 1