checkbox.pl 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright (C) 2006 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('checkbox.pl', 'Checklist Extension');
  18. our ($q, $bol, @HtmlStack, %Action, %Page, $OpenPageName, @MyRules);
  19. # [[ : To do]]
  20. # [[X: Done]]
  21. # [[save: Save]]
  22. push(@MyRules, \&CheckBoxRule);
  23. sub CheckBoxRule {
  24. if ($bol and /\G\[\[( |x):([^]]+)\]\]/cgi) {
  25. my $html;
  26. if (not InElement('form')) {
  27. # We want to use GetFormStart so we have to trade-off using
  28. # AddHtmlEnvironment.
  29. $html = CloseHtmlEnvironments()
  30. . GetFormStart(undef, 'get', 'checkboxes');
  31. unshift(@HtmlStack, 'form');
  32. $html .= AddHtmlEnvironment('p');
  33. }
  34. $html .= $q->checkbox(-name => FreeToNormal($2),
  35. -checked => ($1 eq ' '? 0 : 1),
  36. -label => $2)
  37. . $q->br();
  38. return $html;
  39. } elsif (/\G\[\[save:([^]]+)\]\]/cgi) {
  40. if (InElement('form')) {
  41. return ($q->input({-type => 'hidden',
  42. -name => 'action',
  43. -value => 'checkbox'})
  44. . $q->input({-type => 'hidden',
  45. -name => 'id',
  46. -value => $OpenPageName})
  47. . $q->submit(-name => $1));
  48. } else {
  49. return $1;
  50. }
  51. }
  52. return;
  53. }
  54. $Action{checkbox} = \&DoCheckBox;
  55. sub DoCheckBox{
  56. my $id = shift;
  57. OpenPage($id);
  58. my $text = $Page{text};
  59. my %summary;
  60. $text =~ s{(\A|\n)\[\[( |x):([^]]+)\]\]}{
  61. # no search and replace in this loop
  62. if (GetParam(FreeToNormal($3))) {
  63. $summary{$3} = 1 if $2 eq ' ';
  64. "${1}[[x:${3}]]";
  65. } else {
  66. $summary{$3} = 0 if $2 eq 'x' or $2 eq 'X';
  67. "${1}[[ :${3}]]";
  68. }
  69. }egi;
  70. SetParam('text', $text);
  71. SetParam('summary', join(', ', map {
  72. if ($summary{$_}) {
  73. Ts('set %s', $_);
  74. } else {
  75. Ts('unset %s', $_);
  76. }
  77. } keys %summary));
  78. DoPost($id);
  79. }