header-and-footer-templates.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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('header-and-footer-templates.pl', 'Comments on HTML Templates');
  21. our ($q, $Now, $Message, $DataDir, %SpecialDays);
  22. our ($HtmlTemplateDir);
  23. use HTML::Template;
  24. $HtmlTemplateDir = "$DataDir/templates";
  25. sub HtmlTemplateLanguage {
  26. my $requested_language = $q->http('Accept-language');
  27. my @languages = split(/ *, */, $requested_language);
  28. my %Lang = ();
  29. foreach (@languages) {
  30. my $qual = 1;
  31. $qual = $1 if (/q=([0-9.]+)/);
  32. $Lang{$qual} = $1 if (/^([-a-z]+)/);
  33. }
  34. return map { $Lang{$_} } sort { $b <=> $a } keys %Lang;
  35. }
  36. sub HtmlTemplate {
  37. my $type = shift;
  38. # return header.de.html, or header.html, or error.html, or report an error...
  39. foreach my $f ((map { "$type.$_" } HtmlTemplateLanguage()), $type, "error") {
  40. return "$HtmlTemplateDir/$f.html" if -r "$HtmlTemplateDir/$f.html";
  41. }
  42. ReportError(Tss('Could not find %1.html template in %2', $type, $HtmlTemplateDir),
  43. '500 INTERNAL SERVER ERROR');
  44. }
  45. sub GetSpecialDays {
  46. if (%SpecialDays) {
  47. my ($sec, $min, $hour, $mday, $mon, $year) = gmtime($Now);
  48. return $SpecialDays{($mon + 1) . '-' . $mday};
  49. }
  50. }
  51. *GetHeader = \&HeaderTemplate;
  52. sub HeaderTemplate {
  53. my ($id, $title, $oldId, $nocache, $status) = @_;
  54. if ($oldId ne '') {
  55. $Message .= $q->p('(' . Ts('redirected from %s', GetEditLink($oldId, $oldId)) . ')');
  56. }
  57. my $template = HTML::Template->new(filename => HtmlTemplate('header'),
  58. die_on_bad_params => 0);
  59. $template->param(GOTO_BAR => GetGotoBar($id));
  60. $template->param(SPECIAL_DAYS => GetSpecialDays());
  61. $template->param(MESSAGE => $Message);
  62. $template->param(ID => $id);
  63. $template->param(TITLE => $title);
  64. $template->param(SEARCH => GetSearchForm());
  65. return GetHttpHeader('text/html', $nocache ? $Now : 0, $status)
  66. . $template->output;
  67. }
  68. *PrintFooter = \&PrintFooterTemplate;
  69. sub PrintFooterTemplate {
  70. my ($id, $rev, $comment) = @_;
  71. my $template = HTML::Template->new(filename => HtmlTemplate('footer'),
  72. die_on_bad_params => 0);
  73. $template->param(GOTO_BAR => GetGotoBar($id));
  74. $template->param(SPECIAL_DAYS => GetSpecialDays());
  75. $template->param(ID => $id);
  76. $template->param(COMMENT_FORM => GetCommentForm($id, $rev, $comment));
  77. $template->param(FOOTER_LINKS => GetFooterLinks($id, $rev));
  78. $template->param(ADMIN_LINKS => GetAdminBar($id, $rev)) if UserIsAdmin();
  79. $template->param(TIMESTAMP => GetFooterTimestamp($id, $rev));
  80. $template->param(SEARCH => GetSearchForm());
  81. $template->param(SISTER_SITES => GetSisterSites($id));
  82. $template->param(NEAR_LINKS_USED => GetNearLinksUsed($id));
  83. print $template->output;
  84. }