templates.pl 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. use strict;
  16. use v5.10;
  17. AddModuleDescription('templates.pl', 'Template Extension');
  18. # Any page with a name ending in "Template" is a valid template.
  19. # When creating a page, the $EditNote is prefixed with a list of
  20. # available templates. When the user clicks on one of the links,
  21. # The text area is filled with the template.
  22. our ($q, %IndexHash, %Action, $EditNote);
  23. our ($TemplatePattern);
  24. $Action{'edit'} = \&TemplateDoEdit;
  25. $TemplatePattern = q{Template$}; # strange quoting because of cperl-mode ;)
  26. sub TemplateDoEdit {
  27. my ($id, $newText, $preview) = @_;
  28. AllPagesList(); # prepare %IndexHash
  29. if (not $IndexHash{$id}) {
  30. local $EditNote = TemplateList($id) . $EditNote;
  31. if (GetParam('template', '') and $IndexHash{GetParam('template', '')}) {
  32. return DoEdit($id, GetPageContent(GetParam('template', '')), 1);
  33. } else {
  34. return DoEdit($id, $newText, $preview); # call with localized $EditNote
  35. }
  36. }
  37. DoEdit($id, $newText, $preview); # catch all
  38. }
  39. sub TemplateList {
  40. my $id = shift;
  41. my @list = map {
  42. my $page = $_;
  43. my $name = $_;
  44. $name =~ s/_/ /g;
  45. ScriptLink("action=edit;id=$id;template=$page", $name);
  46. } grep(/$TemplatePattern/, AllPagesList());
  47. return $q->div({-class=>'templates'},
  48. $q->p(T('Alternatively, use one of the following templates:')),
  49. $q->ul($q->li(\@list))) if @list;
  50. }