page-type.pl 3.5 KB

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