bbcode.pl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Copyright (C) 2007, 2008, 2013 Alex Schroeder <alex@gnu.org>
  2. #
  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('bbcode.pl', 'bbCode Extension');
  17. our ($q, @HtmlStack, @MyRules, %RuleOrder, $UrlProtocols, $FullUrlPattern);
  18. push(@MyRules, \&bbCodeRule);
  19. $RuleOrder{\&bbCodeRule} = 100; # must come after PortraitSupportRule
  20. our ($bbBlock);
  21. my %bbTitle = qw(h1 1 h2 1 h3 1 h4 1 h5 1 h6 1);
  22. # This code does not allow the nesting of block elements such as quote
  23. # and list.
  24. sub bbCodeRule {
  25. if (/\G(\[([a-z*][a-z1-6]*)(?:=([^]"]+))?\])/cgi) {
  26. my $bbcode = $1;
  27. my $tag = lc($2);
  28. my $option = $3; # sanitize?
  29. if ($tag eq 'b') {
  30. return AddHtmlEnvironment('b'); }
  31. elsif ($tag eq 'i') {
  32. return AddHtmlEnvironment('i'); }
  33. elsif ($tag eq 'u') {
  34. return AddHtmlEnvironment('em', qq{style="text-decoration: underline; }
  35. . qq{font-style: normal;"}); }
  36. elsif ($tag eq 's' or $tag eq 'strike') {
  37. return AddHtmlEnvironment('del'); }
  38. elsif ($tag eq 'tt') {
  39. return AddHtmlEnvironment('tt'); }
  40. elsif ($tag eq 'sub') {
  41. return AddHtmlEnvironment('sub'); }
  42. elsif ($tag eq 'sup') {
  43. return AddHtmlEnvironment('sup'); }
  44. elsif ($tag eq 'color') {
  45. return AddHtmlEnvironment('em', qq{style="color: $option; }
  46. . qq{font-style: normal;"}); }
  47. elsif ($tag eq 'size') {
  48. $option *= 100;
  49. return $bbcode unless $option; # non-numeric?
  50. return AddHtmlEnvironment('em', qq{style="font-size: $option%; }
  51. . qq{font-style: normal;"}); }
  52. elsif ($tag eq 'font') {
  53. return AddHtmlEnvironment('span', qq{style="font-family: $option;"}); }
  54. elsif ($tag eq 'highlight') {
  55. return AddHtmlEnvironment('strong', qq{class="highlight"}); }
  56. elsif ($tag eq 'url') {
  57. if ($option) {
  58. $option =~ /^($UrlProtocols)/;
  59. my $class = "url $1";
  60. return AddHtmlEnvironment('a', qq{href="$option" class="$class"}); }
  61. elsif (/\G$FullUrlPattern\s*\[\/url\]/cgi) {
  62. return GetUrl($1); }}
  63. elsif ($tag eq 'img' and /\G$FullUrlPattern\s*\[\/img\]/cgi) {
  64. return GetUrl($1, undef, undef, 1); } # force image
  65. elsif ($tag eq 'quote') {
  66. my $html = CloseHtmlEnvironments();
  67. $html .= "</$bbBlock>" if $bbBlock;
  68. $html .= "<blockquote>";
  69. $bbBlock = 'blockquote';
  70. return $html . AddHtmlEnvironment('p'); }
  71. elsif ($tag eq 'center') {
  72. my $html = CloseHtmlEnvironments();
  73. $html .= "</$bbBlock>" if $bbBlock;
  74. $html .= qq{<div class="center" style="text-align: $tag">};
  75. $bbBlock = 'div';
  76. return $html . AddHtmlEnvironment('p'); }
  77. elsif ($tag eq 'left' or $tag eq 'right') {
  78. my $html = CloseHtmlEnvironments();
  79. $html .= "</$bbBlock>" if $bbBlock;
  80. $html .= qq{<div class="$tag" style="float: $tag">};
  81. $bbBlock = 'div';
  82. return $html . AddHtmlEnvironment('p'); }
  83. elsif ($tag eq 'list') {
  84. my $html = CloseHtmlEnvironments();
  85. $html .= "</$bbBlock>" if $bbBlock;
  86. $bbBlock = 'ul';
  87. return $html . '<ul>'; }
  88. elsif ($tag eq '*' and $bbBlock eq 'ul') {
  89. return CloseHtmlEnvironmentUntil('ul') . AddHtmlEnvironment('li'); }
  90. elsif ($tag eq 'code' and /\G((?:.*\n)*?.*?)\[\/code\]\n?/cgi) {
  91. return CloseHtmlEnvironments() . $q->pre($1) . AddHtmlEnvironment('p'); }
  92. elsif ($bbTitle{$tag}) {
  93. return CloseHtmlEnvironments() . AddHtmlEnvironment($tag); }
  94. # no opening tag after all
  95. return $bbcode; }
  96. # closing tags
  97. elsif (/\G(\[\/([a-z][a-z1-6]*)\])/cgi) {
  98. my $bbcode = $1;
  99. my $tag = lc($2);
  100. my %translate = qw{b b i i u em color em size em font span url a
  101. quote blockquote h1 h1 h2 h2 h3 h3 h4 h4 h5 h5
  102. h6 h6 center div left div right div list ul
  103. s del strike del sub sub sup sup highlight strong
  104. tt tt};
  105. # closing a block level element closes all elements
  106. if ($bbBlock eq $translate{$tag}) {
  107. /\G([ \t]*\n)*/cg; # eat whitespace after closing block level element
  108. $bbBlock = undef;
  109. return CloseHtmlEnvironments() . "</$translate{$tag}>"
  110. . AddHtmlEnvironment('p'); }
  111. # ordinary elements just close themselves
  112. elsif (@HtmlStack and $HtmlStack[0] eq $translate{$tag}) {
  113. my $html = CloseHtmlEnvironment();
  114. $html .= AddHtmlEnvironment('p') unless @HtmlStack;
  115. return $html; }
  116. # no closing tag after all
  117. else {
  118. return $bbcode; }}
  119. # smiley
  120. elsif (/\G(:-?[()])/cg) {
  121. if (substr($1,-1) eq ')') {
  122. # 😊 1F60A SMILING FACE WITH SMILING EYES
  123. return '&#x1F60A;'; }
  124. else {
  125. # 😟 1F61F WORRIED FACE
  126. return '&#x1F61F;'; }}
  127. elsif (/\G:(?:smile|happy):/cg) {
  128. return '&#x1F60A;'; }
  129. elsif (/\G:(?:sad|frown):/cg) {
  130. return '&#x1F61F;'; }
  131. # no match
  132. return;
  133. }