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