templates.pl 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 2 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, write to the
  15. # Free Software Foundation, Inc.
  16. # 59 Temple Place, Suite 330
  17. # Boston, MA 02111-1307 USA
  18. use strict;
  19. use v5.10;
  20. AddModuleDescription('templates.pl', 'Template Extension');
  21. # Any page with a name ending in "Template" is a valid template.
  22. # When creating a page, the $EditNote is prefixed with a list of
  23. # available templates. When the user clicks on one of the links,
  24. # The text area is filled with the template.
  25. our ($q, %IndexHash, %Action, $EditNote);
  26. our ($TemplatePattern);
  27. $Action{'edit'} = \&TemplateDoEdit;
  28. $TemplatePattern = q{Template$}; # strange quoting because of cperl-mode ;)
  29. sub TemplateDoEdit {
  30. my ($id, $newText, $preview) = @_;
  31. AllPagesList(); # prepare %IndexHash
  32. if (not $IndexHash{$id}) {
  33. local $EditNote = TemplateList($id) . $EditNote;
  34. if (GetParam('template', '') and $IndexHash{GetParam('template', '')}) {
  35. return DoEdit($id, GetPageContent(GetParam('template', '')), 1);
  36. } else {
  37. return DoEdit($id, $newText, $preview); # call with localized $EditNote
  38. }
  39. }
  40. DoEdit($id, $newText, $preview); # catch all
  41. }
  42. sub TemplateList {
  43. my $id = shift;
  44. my @list = map {
  45. my $page = $_;
  46. my $name = $_;
  47. $name =~ s/_/ /g;
  48. ScriptLink("action=edit;id=$id;template=$page", $name);
  49. } grep(/$TemplatePattern/, AllPagesList());
  50. return $q->div({-class=>'templates'},
  51. $q->p(T('Alternatively, use one of the following templates:')),
  52. $q->ul($q->li(\@list))) if @list;
  53. }