bbcode.pl 5.3 KB

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