2013-01-07 13:51:39 +01:00
|
|
|
# Copyright (C) 2007, 2008, 2013 Alex Schroeder <alex@gnu.org>
|
2007-01-30 15:36:05 +00:00
|
|
|
#
|
2013-01-07 13:51:39 +01:00
|
|
|
# This program is free software; you can redistribute it and/or modify it under
|
|
|
|
|
# the terms of the GNU General Public License as published by the Free Software
|
|
|
|
|
# Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
|
# version.
|
2007-01-30 15:36:05 +00:00
|
|
|
#
|
2013-01-07 13:51:39 +01:00
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
2007-01-30 15:36:05 +00:00
|
|
|
#
|
2013-01-07 13:51:39 +01:00
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
|
# this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-01-30 15:36:05 +00:00
|
|
|
|
2012-04-19 02:18:45 +02:00
|
|
|
$ModulesDescription .= '<p><a href="http://git.savannah.gnu.org/cgit/oddmuse.git/tree/modules/bbcode.pl">bbcode.pl</a>, see <a href="http://www.oddmuse.org/cgi-bin/oddmuse/bbCode_Extension">bbCode Extension</a></p>';
|
2007-01-30 15:36:05 +00:00
|
|
|
|
|
|
|
|
push(@MyRules, \&bbCodeRule);
|
|
|
|
|
|
2013-08-31 21:33:42 +02:00
|
|
|
$RuleOrder{\&bbCodeRule} = 100; # must come after PortraitSupportRule
|
|
|
|
|
|
2007-01-31 08:23:19 +00:00
|
|
|
use vars qw($bbBlock);
|
2007-01-31 23:49:38 +00:00
|
|
|
my %bbTitle = qw(h1 1 h2 1 h3 1 h4 1 h5 1 h6 1);
|
2007-01-31 08:23:19 +00:00
|
|
|
|
2010-01-21 18:00:48 +00:00
|
|
|
# This code does not allow the nesting of block elements such as quote
|
|
|
|
|
# and list.
|
|
|
|
|
|
2007-01-30 15:36:05 +00:00
|
|
|
sub bbCodeRule {
|
2011-03-15 11:01:07 +00:00
|
|
|
if (/\G(\[([a-z*][a-z1-6]*)(?:=([^]"]+))?\])/cgi) {
|
2007-01-30 15:36:05 +00:00
|
|
|
my $bbcode = $1;
|
2007-01-31 23:49:38 +00:00
|
|
|
my $tag = lc($2);
|
2011-03-15 11:01:07 +00:00
|
|
|
my $option = $3; # sanitize?
|
2008-12-16 01:16:59 +00:00
|
|
|
if ($tag eq 'b') {
|
2007-01-30 15:36:05 +00:00
|
|
|
return AddHtmlEnvironment('b'); }
|
2008-12-16 01:16:59 +00:00
|
|
|
elsif ($tag eq 'i') {
|
2007-01-30 15:36:05 +00:00
|
|
|
return AddHtmlEnvironment('i'); }
|
|
|
|
|
elsif ($tag eq 'u') {
|
|
|
|
|
return AddHtmlEnvironment('em', qq{style="text-decoration: underline; }
|
|
|
|
|
. qq{font-style: normal;"}); }
|
2010-01-28 10:51:37 +00:00
|
|
|
elsif ($tag eq 's' or $tag eq 'strike') {
|
|
|
|
|
return AddHtmlEnvironment('del'); }
|
2013-08-25 00:36:41 +02:00
|
|
|
elsif ($tag eq 'tt') {
|
|
|
|
|
return AddHtmlEnvironment('tt'); }
|
2013-01-07 13:51:39 +01:00
|
|
|
elsif ($tag eq 'sub') {
|
|
|
|
|
return AddHtmlEnvironment('sub'); }
|
|
|
|
|
elsif ($tag eq 'sup') {
|
|
|
|
|
return AddHtmlEnvironment('sup'); }
|
2007-01-30 15:36:05 +00:00
|
|
|
elsif ($tag eq 'color') {
|
|
|
|
|
return AddHtmlEnvironment('em', qq{style="color: $option; }
|
|
|
|
|
. qq{font-style: normal;"}); }
|
|
|
|
|
elsif ($tag eq 'size') {
|
|
|
|
|
$option *= 100;
|
|
|
|
|
return $bbcode unless $option; # non-numeric?
|
|
|
|
|
return AddHtmlEnvironment('em', qq{style="font-size: $option%; }
|
|
|
|
|
. qq{font-style: normal;"}); }
|
|
|
|
|
elsif ($tag eq 'font') {
|
|
|
|
|
return AddHtmlEnvironment('span', qq{style="font-family: $option;"}); }
|
2011-03-06 21:08:23 +00:00
|
|
|
elsif ($tag eq 'highlight') {
|
|
|
|
|
return AddHtmlEnvironment('strong', qq{class="highlight"}); }
|
2007-01-31 08:23:19 +00:00
|
|
|
elsif ($tag eq 'url') {
|
|
|
|
|
if ($option) {
|
|
|
|
|
$option =~ /^($UrlProtocols)/o;
|
|
|
|
|
my $class = "url $1";
|
|
|
|
|
return AddHtmlEnvironment('a', qq{href="$option" class="$class"}); }
|
|
|
|
|
elsif (/\G$FullUrlPattern\s*\[\/url\]/cogi) {
|
|
|
|
|
return GetUrl($1); }}
|
|
|
|
|
elsif ($tag eq 'img' and /\G$FullUrlPattern\s*\[\/img\]/cogi) {
|
|
|
|
|
return GetUrl($1, undef, undef, 1); } # force image
|
|
|
|
|
elsif ($tag eq 'quote') {
|
|
|
|
|
my $html = CloseHtmlEnvironments();
|
|
|
|
|
$html .= "</$bbBlock>" if $bbBlock;
|
|
|
|
|
$html .= "<blockquote>";
|
|
|
|
|
$bbBlock = 'blockquote';
|
|
|
|
|
return $html . AddHtmlEnvironment('p'); }
|
2008-06-26 07:46:53 +00:00
|
|
|
elsif ($tag eq 'center') {
|
|
|
|
|
my $html = CloseHtmlEnvironments();
|
|
|
|
|
$html .= "</$bbBlock>" if $bbBlock;
|
2011-03-15 11:01:07 +00:00
|
|
|
$html .= qq{<div class="center" style="text-align: $tag">};
|
2011-03-15 01:06:43 +00:00
|
|
|
$bbBlock = 'div';
|
|
|
|
|
return $html . AddHtmlEnvironment('p'); }
|
|
|
|
|
elsif ($tag eq 'left' or $tag eq 'right') {
|
|
|
|
|
my $html = CloseHtmlEnvironments();
|
|
|
|
|
$html .= "</$bbBlock>" if $bbBlock;
|
2011-03-15 11:01:07 +00:00
|
|
|
$html .= qq{<div class="$tag" style="float: $tag">};
|
2008-06-26 07:46:53 +00:00
|
|
|
$bbBlock = 'div';
|
|
|
|
|
return $html . AddHtmlEnvironment('p'); }
|
2010-01-21 18:00:48 +00:00
|
|
|
elsif ($tag eq 'list') {
|
|
|
|
|
my $html = CloseHtmlEnvironments();
|
|
|
|
|
$html .= "</$bbBlock>" if $bbBlock;
|
|
|
|
|
$bbBlock = 'ul';
|
|
|
|
|
return $html . '<ul>'; }
|
|
|
|
|
elsif ($tag eq '*' and $bbBlock eq 'ul') {
|
|
|
|
|
return CloseHtmlEnvironmentUntil('ul') . AddHtmlEnvironment('li'); }
|
2010-09-21 11:04:59 +00:00
|
|
|
elsif ($tag eq 'code' and /\G((?:.*\n)*?.*?)\[\/code\]\n?/cgi) {
|
2007-01-31 23:49:38 +00:00
|
|
|
return CloseHtmlEnvironments() . $q->pre($1) . AddHtmlEnvironment('p'); }
|
|
|
|
|
elsif ($bbTitle{$tag}) {
|
|
|
|
|
return CloseHtmlEnvironments() . AddHtmlEnvironment($tag); }
|
|
|
|
|
# no opening tag after all
|
|
|
|
|
return $bbcode; }
|
|
|
|
|
# closing tags
|
|
|
|
|
elsif (/\G(\[\/([a-z][a-z1-6]*)\])/cgi) {
|
2007-01-30 15:36:05 +00:00
|
|
|
my $bbcode = $1;
|
2007-01-31 23:49:38 +00:00
|
|
|
my $tag = lc($2);
|
2007-01-31 08:23:19 +00:00
|
|
|
%translate = qw{b b i i u em color em size em font span url a
|
2007-01-31 23:49:38 +00:00
|
|
|
quote blockquote h1 h1 h2 h2 h3 h3 h4 h4 h5 h5
|
2011-03-15 01:06:43 +00:00
|
|
|
h6 h6 center div left div right div list ul
|
2013-08-25 00:36:41 +02:00
|
|
|
s del strike del sub sub sup sup highlight strong
|
|
|
|
|
tt tt};
|
2008-05-09 14:02:47 +00:00
|
|
|
# closing a block level element closes all elements
|
|
|
|
|
if ($bbBlock eq $translate{$tag}) {
|
|
|
|
|
/\G([ \t]*\n)*/cg; # eat whitespace after closing block level element
|
2007-01-31 08:23:19 +00:00
|
|
|
$bbBlock = undef;
|
2007-01-31 23:49:38 +00:00
|
|
|
return CloseHtmlEnvironments() . "</$translate{$tag}>"
|
|
|
|
|
. AddHtmlEnvironment('p'); }
|
2008-05-09 14:02:47 +00:00
|
|
|
# ordinary elements just close themselves
|
|
|
|
|
elsif (@HtmlStack and $HtmlStack[0] eq $translate{$tag}) {
|
|
|
|
|
my $html = CloseHtmlEnvironment();
|
|
|
|
|
$html .= AddHtmlEnvironment('p') unless @HtmlStack;
|
|
|
|
|
return $html; }
|
2007-01-31 23:49:38 +00:00
|
|
|
# no closing tag after all
|
2007-01-30 15:36:05 +00:00
|
|
|
else {
|
2007-01-31 23:49:38 +00:00
|
|
|
return $bbcode; }}
|
|
|
|
|
# smiley
|
|
|
|
|
elsif (/\G(:-?[()])/cg) {
|
2007-01-31 08:31:34 +00:00
|
|
|
if (substr($1,-1) eq ')') {
|
|
|
|
|
# '☺' 0009786 00263a WHITE SMILING FACE, So, 0, ON, N,
|
2007-01-31 23:49:38 +00:00
|
|
|
return '☺'; }
|
|
|
|
|
else {
|
2007-01-31 08:31:34 +00:00
|
|
|
# '☹' 0009785 002639 WHITE FROWNING FACE, So, 0, ON, N,
|
2007-01-31 23:49:38 +00:00
|
|
|
return '☹'; }}
|
|
|
|
|
elsif (/\G:(?:smile|happy):/cg) {
|
|
|
|
|
return '☺'; }
|
|
|
|
|
elsif (/\G:(?:sad|frown):/cg) {
|
|
|
|
|
return '☹'; }
|
|
|
|
|
# no match
|
2007-01-30 15:36:05 +00:00
|
|
|
return undef;
|
|
|
|
|
}
|