page-type.pl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright (C) 2004, 2005 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('page-type.pl', 'Page Type Extension');
  18. our ($q, %AdminPages, @MyInitVariables, $LinkPattern, $FreeLinks, $FreeLinkPattern, $WikiLinks);
  19. our ($PageTypesName);
  20. # You need to define the available types on the following page.
  21. $PageTypesName = 'PageTypes';
  22. # do this later so that the user can customize $SidebarName
  23. push(@MyInitVariables, \&PageTypeInit);
  24. sub PageTypeInit {
  25. $PageTypesName = FreeToNormal($PageTypesName); # spaces to underscores
  26. $AdminPages{$PageTypesName} = 1; # mod_perl!
  27. }
  28. # A page type has to appear as a bullet list item on the page.
  29. #
  30. # Example list defining three types:
  31. #
  32. # * foo
  33. # * bar
  34. # * quux baz
  35. # The page type will be prepended to the beginning of a page. If you
  36. # have page clustering enabled (see the manual), then the page type
  37. # will automatically act as a cluster.
  38. *OldPageTypeDoPost = \&DoPost;
  39. *DoPost = \&NewPageTypeDoPost;
  40. sub NewPageTypeDoPost {
  41. my $id = shift;
  42. my $type = GetParam('types', '');
  43. if ($type and $type ne T('None')) {
  44. $type = "[[$type]]" unless $WikiLinks and $type =~ /^$LinkPattern$/;
  45. my $text = $type . "\n\n" . GetParam('text','');
  46. # We can't use SetParam(), because we're trying to override a
  47. # parameter used by the script. GetParam prefers the actual
  48. # script parameters to parameters set by the cookie (which is what
  49. # SetParam manipulates). We also need to unquote, because
  50. # GetParam automatically unquotes.
  51. $q->param(-name=>'text', -value=>UnquoteHtml($text));
  52. }
  53. OldPageTypeDoPost($id);
  54. }
  55. *OldPageTypeGetTextArea = \&GetTextArea;
  56. *GetTextArea = \&NewPageTypeGetTextArea;
  57. sub NewPageTypeGetTextArea {
  58. my ($name, $text) = @_;
  59. return OldPageTypeGetTextArea(@_) if ($name ne 'text'); # comment box!
  60. my @types = (T('None'),);
  61. # read categories
  62. foreach (split ('\n', GetPageContent($PageTypesName))) {
  63. if ($WikiLinks and (m/^\*[ \t]($LinkPattern)/)) {
  64. push (@types, $1);
  65. } elsif ($FreeLinks and (m/^\*[ \t]\[\[($FreeLinkPattern)\]\]/)) {
  66. push (@types, $1);
  67. }
  68. }
  69. my $cluster;
  70. # This duplicates GetCluster code so that this works even when
  71. # $PageCluster==0.
  72. $cluster = $1 if ($WikiLinks && $text =~ /^$LinkPattern\n/)
  73. or ($FreeLinks && $text =~ /^\[\[$FreeLinkPattern\]\]\n/);
  74. if (grep(/^$cluster$/, @types)) {
  75. $text =~ s/^.*\n+//; # delete cluster line, and clean up further empty lines
  76. } else {
  77. $cluster = T('None');
  78. }
  79. #build the new input
  80. my $html = OldPageTypeGetTextArea($name, $text);
  81. my $list = T('Type') . ': <select name="types">';
  82. foreach my $type (@types) {
  83. if ($type eq $cluster) {
  84. $list .= "<option value=\"$type\" selected>$type";
  85. } else {
  86. $list .= "<option value=\"$type\">$type";
  87. }
  88. }
  89. $list .= "</select>";
  90. $html .= $q->p($list);
  91. return $html;
  92. }