comment-div-wrapper.pl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (C) 2014 Alex-Daniel Jakimenko <alex.jakimenko@gmail.com>
  2. # Copyright (C) 2014 Alex Schroeder <alex@gnu.org>
  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('comment-div-wrapper.pl', 'Comment Div Wrapper Extension');
  17. our ($q, $bol, $OpenPageName, @MyRules, %RuleOrder, $CommentsPrefix, $CommentsPattern, $FS);
  18. my $CommentDiv = 0;
  19. push(@MyRules, \&CommentDivWrapper);
  20. $RuleOrder{\&CommentDivWrapper} = -50;
  21. sub CommentDivWrapper {
  22. if (substr($OpenPageName, 0, length($CommentsPrefix)) eq $CommentsPrefix) {
  23. if (pos == 0 and not $CommentDiv) {
  24. $CommentDiv = 1;
  25. return $q->start_div({-class=>'userComment'});
  26. }
  27. }
  28. if ($OpenPageName =~ /$CommentsPattern/) {
  29. if ($bol and m/\G(\s*\n)*----+[ \t]*\n?/cg) {
  30. my $html = CloseHtmlEnvironments()
  31. . ($CommentDiv++ > 0 ? $q->end_div() : $q->h2({-class=>'commentsHeading'}, T('Comments:'))) . $q->start_div({-class=>'userComment'})
  32. . AddHtmlEnvironment('p');
  33. return $html;
  34. }
  35. }
  36. return;
  37. }
  38. # close final div
  39. *OldCommentDivApplyRules = \&ApplyRules;
  40. *ApplyRules = \&NewCommentDivApplyRules;
  41. sub NewCommentDivApplyRules {
  42. my ($blocks, $flags) = OldCommentDivApplyRules(@_);
  43. if ($CommentDiv) {
  44. print $q->end_div();
  45. $blocks .= $FS . $q->end_div();
  46. $flags .= $FS . 0;
  47. $CommentDiv = 0;
  48. }
  49. return ($blocks, $flags);
  50. }