live-templates.pl 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright (C) 2004, 2005, 2007 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('live-templates.pl', 'Live Template Extension');
  21. our ($q, $bol, @MyRules, $FreeLinkPattern);
  22. push(@MyRules, \&LiveTemplateRule);
  23. sub LiveTemplateRule {
  24. if ($bol and /\G(&lt;&lt;$FreeLinkPattern\n)/cg) {
  25. Clean(CloseHtmlEnvironments());
  26. my $str = $1;
  27. my $template = FreeToNormal($2);
  28. /\G((.*?\n)$template(\n|\Z))/cgs or print $q->p($q->strong(T('Template without parameters')));
  29. $str .= $1;
  30. Dirty($str);
  31. my $oldpos = pos;
  32. my $old_ = $_;
  33. my $hash = ParseData($2);
  34. my $text = GetPageContent($template);
  35. return $q->p($q->strong(Ts('The template %s is either empty or does not exist.',
  36. $template))) . AddHtmlEnvironment('p') unless $text;
  37. foreach my $key (keys %$hash) {
  38. $text =~ s/\$$key\$/$hash->{$key}/g;
  39. }
  40. print "<div class=\"template $template\">";
  41. ApplyRules(QuoteHtml($text), 1, 1, undef, 'p');
  42. $_ = $old_;
  43. pos = $oldpos;
  44. print '</div>';
  45. Clean(AddHtmlEnvironment('p'));
  46. return '';
  47. }
  48. return;
  49. }