dojo.pl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright (C) 2006, 2008 Alex Schroeder <alex@gnu.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('dojo.pl', 'Using Dojo Instead Of Wiki Markup');
  18. our ($q, $HtmlHeaders, @MyRules, @MyInitVariables, %CookieParameters);
  19. our (@DojoPlugins, $DojoTheme);
  20. $DojoTheme = 'tundra';
  21. # Configure toolbar example:
  22. # @DojoPlugins = qw(copy cut paste | bold);
  23. # Render all HTML tags except for <script>.
  24. push (@MyRules, \&WysiwygRule);
  25. sub WysiwygRule {
  26. if (m/\G(&lt;.*?&gt;)/cg) {
  27. return $1 if substr($1,5,6) eq 'script'
  28. or substr($1,4,6) eq 'script';
  29. return UnquoteHtml($1);
  30. }
  31. return;
  32. }
  33. # Add the dojo script to edit pages.
  34. push (@MyInitVariables, \&WysiwygScript);
  35. sub WysiwygScript {
  36. # cookie is not initialized yet so we cannot use GetParam
  37. if ($q->param('action') eq 'edit') {
  38. $HtmlHeaders .= qq{
  39. <style type="text/css">
  40. \@import "/dojoroot/dijit/themes/$DojoTheme/$DojoTheme.css";
  41. </style>
  42. <script type="text/javascript" src="/dojoroot/dojo/dojo.js"
  43. djConfig="parseOnLoad: true"></script>
  44. <script type="text/javascript">
  45. dojo.require("dijit.Editor");
  46. dojo.addOnLoad(function () {
  47. dojo.connect(dojo.query("form.edit")[0], 'onsubmit', function () {
  48. dojo.byId('dojotext').value = dijit.byId('dojoeditor').getValue(false);
  49. });
  50. });
  51. </script>
  52. };
  53. }
  54. # theme has to match $DojoTheme (so that it gets used for the body tag)
  55. delete $CookieParameters{theme};
  56. SetParam('theme', $DojoTheme);
  57. }
  58. *OldWysiwygGetTextArea = \&GetTextArea;
  59. *GetTextArea = \&NewWysiwygGetTextArea;
  60. sub NewWysiwygGetTextArea {
  61. my ($name, $text, $rows) = @_;
  62. if ($name eq 'text') {
  63. # The dojoeditor is the visible thing that is not submitted; we
  64. # need some javascript that will copy the content of the
  65. # dojoeditor to the dojotext field which has the right name.
  66. my %params = (-id=>'dojoeditor', -default=>$text,
  67. -rows=>$rows||25, -columns=>78, -override=>1,
  68. -dojoType=>'dijit.Editor');
  69. $params{-plugins} = "[" . join(",", map{"'$_'"} @DojoPlugins) . "]"
  70. if @DojoPlugins;
  71. return $q->textarea(%params)
  72. . $q->hidden(-id=>'dojotext', name=>$name);
  73. } else {
  74. return OldWysiwygGetTextArea(@_);
  75. }
  76. }
  77. *OldWysiwygImproveDiff = \&ImproveDiff;
  78. *ImproveDiff = \&NewWysiwygImproveDiff;
  79. sub NewWysiwygImproveDiff {
  80. my $old = OldWysiwygImproveDiff(@_);
  81. my $new = '';
  82. my $protected = 0;
  83. # fix diff inserting change boundaries inside tags
  84. $old =~ s!&<strong class="changes">([a-z]+);!</strong>&$1;!g;
  85. $old =~ s!&</strong>([a-z]+);!</strong>&$1;!g;
  86. # unquote named html entities
  87. $old =~ s/\&amp;([a-z]+);/&$1;/g;
  88. foreach my $str (split(/(<strong class="changes">|<\/strong>)/, $old)) {
  89. # Don't remove HTML tags affected by changes.
  90. $protected = 1 if $str eq '<strong class="changes">';
  91. # strip html tags and don't get confused with the < and > created
  92. # by diff!
  93. $str =~ s/\&lt;.*?\&gt;//g unless $protected;
  94. $protected = 0 if $str eq '</strong>';
  95. $new .= $str;
  96. }
  97. return $new;
  98. }