weblog-3.pl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # Copyright (C) 2005–2014 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation; either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. use strict;
  15. use v5.10;
  16. AddModuleDescription('weblog-3.pl', 'Create a Blog Front Page');
  17. # Categories
  18. our ($q, %Action, %Page, $OpenPageName, $HomePage, $ScriptName, @MyInitVariables, $FreeLinkPattern, $UserGotoBar, @UserGotoBarPages, $UsePathInfo);
  19. our ($CategoriesPage);
  20. $CategoriesPage = 'Categories';
  21. *CategoriesOldOpenPage = \&OpenPage;
  22. *OpenPage = \&CategoriesNewOpenPage;
  23. my %Category = (); # fast checking
  24. my @Categories = (); # correct order
  25. my $CategoryInit = 0;
  26. *OldCategoriesNewText = \&NewText;
  27. *NewText = \&NewCategoriesNewText;
  28. sub NewCategoriesNewText {
  29. my $id = shift;
  30. if ($id eq $HomePage) {
  31. return '<journal>';
  32. } elsif (GetParam('tag','')
  33. or $Category{$id}) {
  34. # if the page is either on the categories page, or the tag=1
  35. # parameter was added, show a journal
  36. $Page{text} = T('Matching pages:')
  37. . "\n\n"
  38. . '<journal "^\d\d\d\d-\d\d-\d\d.*'
  39. . $OpenPageName
  40. . '">';
  41. } else {
  42. return OldCategoriesNewText($id, @_);
  43. }
  44. }
  45. sub CategoriesNewOpenPage {
  46. CategoryInit() unless $CategoryInit;
  47. CategoriesOldOpenPage(@_);
  48. }
  49. # Category page
  50. sub CategoryParse {
  51. my @paragraphs = split(/\n\n+/, shift);
  52. my @result;
  53. foreach (@paragraphs) {
  54. next unless /^\*/;
  55. while (/\*+\s*\[\[$FreeLinkPattern\]\]/g) {
  56. my $id = FreeToNormal($1);
  57. push(@result, $id);
  58. }
  59. last;
  60. }
  61. return @result;
  62. }
  63. sub CategoryInit {
  64. $CategoryInit = 1;
  65. @Categories = CategoryParse(GetPageContent($CategoriesPage));
  66. map { $Category{$_} = 1 } @Categories;
  67. }
  68. # New Action
  69. $Action{new} = \&DoCategories;
  70. sub DoCategories {
  71. print GetHeader('', T('New')), $q->start_div({-class=>'content categories'}),
  72. GetFormStart(undef, 'get', 'cat');
  73. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime();
  74. my $today = sprintf("%d-%02d-%02d", $year + 1900, $mon + 1, $mday);
  75. CategoryInit() unless $CategoryInit;
  76. print $q->p({-class=>'table'}, map {GetEditLink("$today $_", $_)} @Categories);
  77. print $q->p($q->textfield('id', $today), GetHiddenValue('action', 'edit'));
  78. print $q->p(Ts('Edit %s.', GetPageLink($CategoriesPage)));
  79. print $q->submit("Go!");
  80. print $q->end_form, $q->end_div();
  81. PrintFooter();
  82. }
  83. # Goto Bar
  84. my $GotoBarInit = 0;
  85. sub GotoBarInit {
  86. $GotoBarInit = 1;
  87. @UserGotoBarPages = (@UserGotoBarPages,
  88. CategoryParse(GetPageContent($HomePage)));
  89. }
  90. *GetGotoBar = * NewGetGotoBar;
  91. sub NewGetGotoBar {
  92. my $id = shift;
  93. GotoBarInit() unless $GotoBarInit;
  94. my @links;
  95. foreach my $name (@UserGotoBarPages) {
  96. push (@links, GetPageLink($name, $name));
  97. }
  98. my @parts = split(/_/, GetId());
  99. CategoryInit() unless $CategoryInit;
  100. if ($parts[0] =~ /\d\d\d\d-\d\d-\d\d/) {
  101. shift(@parts);
  102. push(@links, map {
  103. if ($Category{$_}) {
  104. $q->a({-href=>$ScriptName . ($UsePathInfo ? '/' : '?') . UrlEncode($_),
  105. -class=>'local tag',
  106. -rel=>'tag'}, $_);
  107. } else {
  108. # provide tag=1 parameter to tell OpenPage to add journal tag
  109. $q->a({-href=>$ScriptName . '?tag=1;action=browse;id=' . UrlEncode($_),
  110. -class=>'local tag',
  111. -rel=>'tag'}, $_);
  112. }
  113. } @parts);
  114. }
  115. push (@links, ScriptLink('action=new', T('New')));
  116. return $q->span({-class=>'gotobar bar'}, @links, $UserGotoBar);
  117. }