live-templates.pl 1.7 KB

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