html-template.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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('html-template.pl', 'HTML Templates');
  21. # The entire mechanism of how pages are built is now upside down.
  22. # Instead of writing code that assembles pages, we load templates,
  23. # that refer to pieces of code.
  24. #
  25. # This is the beginning of PHP-in-Perl. :(
  26. our ($q, %Action, $DataDir, $UseCache, $LastUpdate);
  27. our ($HtmlTemplateDir);
  28. $HtmlTemplateDir = "$DataDir/templates";
  29. *BrowsePage = \&DoHtmlTemplate;
  30. # replace all actions with DoHtmlTemplate!
  31. foreach my $key (keys %Action) {
  32. $Action{$key} = \&DoHtmlTemplate;
  33. }
  34. sub DoHtmlTemplate {
  35. my ($id, $raw, $comment, $status) = @_;
  36. if ($q->http('HTTP_IF_MODIFIED_SINCE')
  37. and $q->http('HTTP_IF_MODIFIED_SINCE') eq gmtime($LastUpdate)
  38. and GetParam('cache', $UseCache) >= 2) {
  39. print $q->header(-status=>'304 NOT MODIFIED');
  40. return;
  41. }
  42. OpenPage($id) if $id;
  43. print GetHttpHeader('text/html');
  44. print GetHtmlTemplate();
  45. }
  46. # Some subroutines from the script need a wrapper in order to return a
  47. # string instead of printing directly.
  48. sub HtmlTemplateRc {
  49. my $result = ToString(sub { DoRc(\&GetRcHtml) });
  50. return $result;
  51. }
  52. # Processing instructions are processed as Perl code, and its result
  53. # is substituted. Examples:
  54. #
  55. # <?&foo?> -- This will call the subroutine &foo. It's return value
  56. # will be substituted for the processing instruction.
  57. #
  58. # <?$foo?> -- This substitutes the value of variable $foo.
  59. #
  60. # Since the processing instruction is valid XHTML, the template should
  61. # be valid XHTML as well.
  62. sub GetHtmlTemplate {
  63. my $template = shift || GetActionHtmlTemplate();
  64. my $html = ReadFileOrDie($template);
  65. $html =~ s/<\?(.*?)\?>/HtmlTemplateEval($1)/egs;
  66. return $html;
  67. }
  68. sub HtmlTemplateEval {
  69. my $code = shift;
  70. my $result = eval($code) || $@;
  71. }
  72. sub GetActionHtmlTemplate {
  73. my $action = GetParam('action', 'browse');
  74. # return browse.de.html, or browse.html, or error.html, or report an error...
  75. foreach my $f ((map { "$action.$_" } HtmlTemplateLanguage()), $action, "error") {
  76. return "$HtmlTemplateDir/$f.html" if -r "$HtmlTemplateDir/$f.html";
  77. }
  78. ReportError(Tss('Could not find %1.html template in %2', $action, $HtmlTemplateDir),
  79. '500 INTERNAL SERVER ERROR');
  80. }
  81. sub HtmlTemplateLanguage {
  82. my $requested_language = $q->http('Accept-language');
  83. my @languages = split(/ *, */, $requested_language);
  84. my %Lang = ();
  85. foreach (@languages) {
  86. my $qual = 1;
  87. $qual = $1 if (/q=([0-9.]+)/);
  88. $Lang{$qual} = $1 if (/^([-a-z]+)/);
  89. }
  90. return map { $Lang{$_} } sort { $b <=> $a } keys %Lang;
  91. }