html-template.pl 3.2 KB

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