# FILE: src-board-subs-8
# Formatting and conversions
#-------------------------------------------------------------------------------
# DISCUS VERSION 3.10 COPYRIGHT NOTICE
#
# Discus 3.10 is copyright (c) 2000 by DiscusWare, LLC, all rights reserved.
# The use of Discus is governed by the Discus License Agreement which is
# available from the Discus WWW site at:
#    http://www.discusware.com/discus/license
#
# Pursuant to the Discus License Agreement, this copyright notice may not be
# removed or altered in any way.
#-------------------------------------------------------------------------------

#---SEPARATOR---#

sub search_stop {
	my ($input, $flag) = @_;
	return ($input, "") if ($GLOBAL_OPTIONS{'search_stopwords'} ne "" && $GLOBAL_OPTIONS{'search_stopwords'} == 0);
	$x = scalar(keys(%STOP_WORDS));
	if ($x == 0) {
		open (STOP, "$admin_dir/stopwords.conf");
		while (<STOP>) {
			next if m|^#|;
			next if !m|\S|;
			s/\s//g;
			tr/A-Z/a-z/;
			$STOP_WORDS{$_} = 1;
		}
		close (STOP);
	}
	$x = &remove_html($input, 1);
	if ($flag) {
		$x =~ s/\\b//g;
	}
	@x = split(/\s+/, $x);
	undef @output; undef @junk;
	foreach $x (@x) {
		$x =~ s/&#(\d+);/pack("c", $1)/ge;
		$x =~ s/[,\[\]\(\)]//g;
		next if $x !~ /\w/;
		$x2 = $x; $x2 =~ tr/A-Z/a-z/;
		if ($STOP_WORDS{$x2}) {
			push (@junk, $x2);
		} else {
			push (@output, $x);
		}
	}
	$output = join(" ", @output);
	$junk = join(" ", @junk);
	$output =~ s/^\s+//;
	$output =~ s/\s+$//;
	$output =~ s/\s+/ /g;
	return ($output, $junk);
}

#---SEPARATOR---#

sub html_to_webtags {
	my ($message_in) = @_;
	return $message_in if $GLOBAL_OPTIONS{'allow_arb_html'};
	$message_in =~ s/\\/\\\\/g;
	$message_in =~ s/\{/\\\{/g;
	$message_in =~ s/\}/\\\}/g;
	while ($message_in =~ m|<(\s+)([^>]+)(\s*)>|) {
		$two = $2;
		$message_in = join("", $`, "<", $two, ">", $');
	}
	while ($message_in =~ m|<(\s*)([^>]+)(\s+)>|) {
		$two = $2;
		$message_in = join("", $`, "<", $two, ">", $');
	}
	$message_in = &html_to_webtags_1($message_in, '<B>', '</B>', 'b');
	$message_in = &html_to_webtags_1($message_in, '<I>', '</I>', 'i');
	$message_in = &html_to_webtags_1($message_in, '<U>', '</U>', 'u');
	$message_in = &html_to_webtags_1($message_in, '<C>', '</C>', 'c');
	$message_in = &html_to_webtags_1($message_in, '<CENTER>', '</CENTER>', 'c');
	$message_in = &html_to_webtags_1($message_in, '<STRIKE>', '</STRIKE>', 'strike');
	$message_in = &html_to_webtags_1($message_in, '<BLINK>', '</BLINK>', 'blink');
	$message_in = &html_to_webtags_1($message_in, '<H1>', '</H1>', '2');
	$message_in = &html_to_webtags_1($message_in, '<H2>', '</H2>', '2');
	$message_in = &html_to_webtags_1($message_in, '<H3>', '</H3>', '1');
	$message_in = &html_to_webtags_1($message_in, '<H4>', '</H4>', '0');
	$message_in = &html_to_webtags_1($message_in, '<SUP>', '</SUP>', '+');
	$message_in = &html_to_webtags_1($message_in, '<SUB>', '</SUB>', '-');
	$message_in = &html_to_webtags_1($message_in, '<PRE>', '</PRE>', 'pre');
	while ($message_in =~ m|<A ([^>]+)>|i) {
		$args = " $1"; $bef = $`; $aft = $';
		($b, $a) = &match_close($aft, '</A>');
		if ($b ne "") {
			$begin_tag = ""; $end_tag = "";
			$args =~ s|(HREF)\s*=\s*([^"])(\S*)|\1="\2\3"|gi;
			if ($args =~ m|\s+HREF\s*=\s*"([^"]+)"|i) {
				$col = $1;
				$begin_tag .= "\\topurl{$col,"; $end_tag .= "}";
			}
			$message_in = join("", $bef, $begin_tag, $b, $end_tag, $a);
		} else {
			$message_in = join("", $bef, $aft);
		}
	}
	while ($message_in =~ m|<FONT ([^>]+)>|i) {
		$aft = $';
		$bef = $`;
		$args = " " . $1;
		($b, $a) = &match_close($aft, '</FONT>');
		if ($b ne "") {
			$begin_tag = ""; $end_tag = "";
			$args =~ s|(\w+)\s*=\s*([^"])(\S*)|$1="$2$3"|g;
			if ($args =~ m|\s+COLOR\s*=\s*"([^"]+)"|i) {
				$col = $1; $col =~ s/#//;
				$col =~ s/"//g;
				$begin_tag .= "\\rgb{$col,"; $end_tag .= "}";
			}
			if ($args =~ m|\s+FACE\s*=\s*"([^"]+)"|i) {
				$col = $1;
				$col =~ s/#//;
				$col =~ s/"//g;
				$col =~ s/,/\\,/g;
				$begin_tag .= "\\font{$col,"; $end_tag .= "}";
			}
			if ($args =~ m|\s+SIZE\s*=\s*"([^"]+)"|i) {
				$col = $1; $col =~ s/#//; $col =~ s/"//g;
				if ($col eq "-1") {
					$begin_tag .= "\\-1{"; $end_tag .= "}";
				} elsif ($col eq "-2") {
					$begin_tag .= "\\-2{"; $end_tag .= "}";
				} elsif ($col eq "-2") {
					$begin_tag .= "\\-2{"; $end_tag .= "}";
				} elsif ($col eq "+2") {
					$begin_tag .= "\\2{"; $end_tag .= "}";
				} elsif ($col eq "+1") {
					$begin_tag .= "\\1{"; $end_tag .= "}";
				} elsif ($col eq "+0") {
					$begin_tag .= "\\0{"; $end_tag .= "}";
				} elsif ($col eq "-0") {
					$begin_tag .= "\\0{"; $end_tag .= "}";
				} elsif ($col eq "1") {
					$begin_tag .= "\\-2{"; $end_tag .= "}";
				} elsif ($col eq "2") {
					$begin_tag .= "\\-1{"; $end_tag .= "}";
				} elsif ($col eq "3") {
					$begin_tag .= "\\0{"; $end_tag .= "}";
				} elsif ($col eq "4") {
					$begin_tag .= "\\1{"; $end_tag .= "}";
				} elsif ($col > 4) {
					$begin_tag .= "\\2{"; $end_tag .= "}";
				}
			}
			$message_in = join("", $bef, $begin_tag, $b, $end_tag, $a);
		} else {
			$message_in = join("", $bef, $aft);
		}
	}
	return $message_in;
}

sub html_to_webtags_1 {
	my ($message_in, $opening, $closing, $webtag) = @_;
	while ($message_in =~ m|$opening|i) {
		$after1 = $'; $before1 = $`;
		if ($after1 =~ m|$closing|i) {
			$before2 = $`; $after2 = $';
			$message_in = join("", $before1, "\\", $webtag, "{", $before2, "}", $after2);
		} else {
			return $message_in;
		}
	}
	return $message_in;
}

sub match_close {
	my ($stringin, $expected_close) = @_;
	if ($stringin =~ m|$expected_close|i) {
		return ($`, $');
	} else {
		return ("", "");
	}
}

#---SEPARATOR---#

sub remove_swearing {
	my ($message_in, $code, $star_flag) = @_;
	$message_in = " $message_in ";
	my (@naughty); undef @naughty;
	my $u = join("", chr(0), "-", chr(47), chr(58), "-", chr(64), chr(91), "-", chr(96), chr(123), "-", chr(191));
	if (open(CUSS, "$admin_dir/cuss.txt")) {
		@cuss = <CUSS>;
		close (CUSS);
		@cuss = grep(!/^#/, @cuss);
		foreach $line (@cuss) {
			$line =~ s/\s//g;
			next if length($line) == 0;
			if ($line =~ m|!|) {
				$line = $';
				$line =~ tr/a-zA-Z/b-zaB-ZA/;
			}
			$stars = "\\char{149}" x length($line);
			$stars = join("", "\\red{", $stars, "}") if $code != 3;
			$stars = "&#149;" x length($line) if $star_flag;
			$line = quotemeta($line);
			if ($GLOBAL_OPTIONS{'profanity_detected'} == 2) {
				push (@naughty, $line) if $message_in =~ m|([$u])$line([$u])|i;
			} else {
				if ($message_in =~ /$line/i) {
					while ($message_in =~ /([$u])$line([$u])/i) {
						$message_in = join("", $`, $1, $stars, $2, $');
					}
				}
			}
		}
	}
	if ($GLOBAL_OPTIONS{'profanity_detected'} == 2) {
		$x = join("<LI>", @naughty);
		return "" if $x eq "";
		$xnew = "<LI>$x";
		return $xnew;
	} else {
		$message_in =~ s/^ //; $message_in =~ s/ $//;
		return $message_in;
	}
}

#---SEPARATOR---#

sub webtags {
	$webtags_conf_file = "$admin_dir/webtags.conf";
	$NEST_FLAG = 0;
	my ($text_in, $context_code, $situation, $is_moderator, $is_superuser) = @_;
	return ("", $text_in) if ($GLOBAL_OPTIONS{'allow_arb_html'} && $situation != 0);
	return ($text_in, $text_in) if ($GLOBAL_OPTIONS{'allow_arb_html'} && $situation == 0);
	$text_in = &escape_input($text_in);
	if ($text_in !~ m|\\(\S+)\{|) {
		return ("", $text_in) if ($situation != 0 && $text_in !~ m|\\(\S+)\{|);
		return ($text_in, $text_in) if ($situation == 0 && $text_in !~ m|\\(\S+)\{|);
	}
	&read_tags ($context_code, $is_moderator, $is_superuser);
	($messages, $formatted) = &parse("", $text_in);
	if ($messages ne "!Error") {
		$formatted =~ s/\\/&#92;/g;
		while ($formatted =~ m|/(\w+)&#40;|g) {
			$messages .= "<LI><B>/$1(...)</B> $L{COULDMEAN} <B>\\$1\{...\}</B><BR>\n" if ($tags{$1} ne "" && $flagged{$1} == 0);
			$flagged{$1} = 1;
		}
		while ($formatted =~ m|\\(\w+)&#40;|g) {
			$messages .= "<LI><B>&#92;$1(...)</B> $L{COULDMEAN} <B>\\$1\{...\}</B><BR>\n" if ($tags{$1} ne "" && $flagged{$1} == 0);
			$flagged{$1} = 1;
		}
		while ($formatted =~ m|/(\w+)\{|g) {
			$messages .= "<LI><B>/$1\{...\}</B> $L{COULDMEAN} <B>\\$1\{...\}</B><BR>\n" if ($tags{$1} ne "" && $flagged{$1} == 0);
			$flagged{$1} = 1;
		}
		while ($formatted =~ m|/(\w+) \{|g) {
			$messages .= "<LI><B>/$1\{...\}</B> $L{COULDMEAN} <B>\\$1\{...\}</B><BR>\n" if ($tags{$1} ne "" && $flagged{$1} == 0);
			$flagged{$1} = 1;
		}
		while ($formatted =~ m|/(\w+) &#40;|g) {
			$messages .= "<LI><B>/$1\{...\}</B> $L{COULDMEAN} <B>\\$1\{...\}</B><BR>\n" if ($tags{$1} ne "" && $flagged{$1} == 0);
			$flagged{$1} = 1;
		}
		while ($formatted =~ m|\\(\w+) \{|g) {
			$messages .= "<LI><B>/$1\{...\}</B> $L{COULDMEAN} <B>\\$1\{...\}</B><BR>\n" if ($tags{$1} ne "" && $flagged{$1} == 0);
			$flagged{$1} = 1;
		}
		while ($formatted =~ m|\\(\w+) &#40;|g) {
			$messages .= "<LI><B>/$1\{...\}</B> $L{COULDMEAN} <B>\\$1\{...\}</B><BR>\n" if ($tags{$1} ne "" && $flagged{$1} == 0);
			$flagged{$1} = 1;
		}
	} else {
		$formatted = "<H3>$L{FORMATTINGERROR}</H3>$formatted";
	}
	if ($situation == 0) {
		return ($formatted, $formatted);
	} else {
		return ($messages, $formatted);
	}
}

sub parse {
	my ($messages, $text_in) = @_;
	return ("!Error", $text_in) if $messages eq "!Error";
	my ($v1, $v2, $v3, $v4, $v5, $v6, $v7);
	while ($text_in =~ m|\\([^\{\s]*)\{|) {
		($v1, $v2, $v3) = ($`, $1, $');
		($v4, $v3) = &parse($messages, $v3);
		return ($v4, $v3) if $v4 eq "!Error";
		$messages .= $v4;
		if ($v3 =~ m|^([^\}]*)\}|) {
			($v6, $v7) = ($1, $');
		} else {
			$ncb = $L{NOCLOSINGBRACE};
			$ncb =~ s/\%code/$v2/g;
			return ("!Error", "$ncb");
		}
		if ($v2 eq "ch" && $char{$v6} ne "") {
			$text_in = join("", $v1, $char{$v6}, $v7);
		} elsif ($v2 eq "ch") {
			$sc = $L{NOSPECIALCHARACTER};
			$sc =~ s/\%char/$v6/g;
			return ("!Error", "$sc");
		} elsif ($tags{$v2} ne "") {
			$temp = $tags{$v2};
			$maxtest = 0;
			if ($temp =~ m%\|(\d+)\|%) {
				while ($temp =~ m%\|(\d+)\|%g) {
					$maxtest = $1 if $1 > $maxtest;
				}
			}
			if ($maxtest > 0) {
				@splits = split(/,/, $v6, $maxtest);
			}
			$sc = scalar(@splits);
			if ($v2 eq "link" || $v2 eq "topurl" || $v2 eq "newurl") {
				$splits[0] =~ s/\&#44;/\%2C/g;
				$splits[0] =~ s/\&#43;/\%2B/g;
				if ($splits[0] =~ m|<| || $splits[0] =~ m|>|) {
					$f = $L{NONESTHTML};
					$f =~ s/\%code/$v2/g;
					return ("!Error", $f);
				}
				if ($splits[0] =~ m|^\s*javascript:|i) {
					$f = $L{NONESTHTML};
					$f =~ s/\%code/$v2/g;
					return ("!Error", $f);
				}
			}
			if ($v2 eq "attach" || $v2 eq $L{ATTACH_TAG_NAME} || $v2 eq "image" || $v2 eq $L{IMAGE_TAG_NAME}) {
				$f = $L{NONESTHTML};
				$f =~ s/\%code/$v2/g;
				return ("!Error", $f) if ($splits[0] =~ m|<| || $splits[0] =~ m|>|);
			}
			while ($temp =~ m%\|(\d+)\|%) {
				$temp = join("", $`, $splits[($1)-1], $');
			}
			if ($sc < $maxtest) {
				$wnarg = $L{WRONGNUMBEROFARGUMENTS};
				$wnarg =~ s/\%code/$v2/g;
				$wnarg =~ s/\%required/$maxtest/g;
				$wnarg =~ s/\%your/$sc/g;
				return ("!Error", "$wnarg");
			}
			if ($v2 eq "table" || $v2 eq "tablenb" || $v2 eq $L{TABLE_TAG} || $v2 eq $L{TABLE_NB_TAG}) {
				if ($temp =~ m|\t|) {
					$temp =~ s%\t%</TD><TD>%g;
				} else {
					$temp =~ s%,%</TD><TD>%g;
				}
				$temp =~ s%<BR>%</TD></TR><TR><TD>%g;
			} elsif ($v2 eq "list" || $v2 eq $L{LIST_TAG} || $v2 eq "olist" || $L{OLIST_TAG} eq $v2) {
				$temp =~ s%<BR>%<LI>%g;
			}
			$text_in = join("", $v1, $temp, $v7);
		} else {
			$sc = $L{CODEDOESNOTEXIST};
			$sc =~ s/\%code/$v2/g;
			if ($v2 eq "attach" && !$pro) {
				$sc .= "<P>$L{FEATURE_NOT_SUPPORTED_DESCR}\n";
			}
			return ("!Error", "$sc");
		}
	}
	return ($messages, $text_in);
}

sub escape_input {
	my ($stringin) = @_;
	return &char_convert($stringin);
}

sub read_tags {
	my ($cont_code, $is_moderator, $is_superuser) = @_;
	undef %char; undef %tags; undef $str;
	open (WTCONF, "<$webtags_conf_file");
	while (<WTCONF>) {
		next if /^#/;
		s/^\s*//;
		s/\s*$//;
		if ($str) {
			if (m|\\$|) {
				$str .= $`;
			} else {
				if ($cont =~ m|^[cC]|) {
					$char{$code} = join("", $str, $_);
				} elsif ($cont > $cont_code || (($is_moderator || $is_superuser) && $cont != 1) || ($is_moderator && $cont =~ m|^[mM]|) || ($is_superuser && $cont =~ m|^[sS]|)) {
					$tags{$code} = join("", $str, $_);
					$str_ = $tags{$code};
					while ($str_ =~ m|!VAR\((\w+)\)!|) {
						$before = $`;
						$after = $';
						$f = $1;
						$f = "\$" . $f;
						$f =~ s/"/\\"/g;
						$result = eval qq/"$f"/;
						$str_ = $before . $result . $after;
					}
					$tags{$code} = $str_;
				}
				$str = "";
				next;
			}
		}
		if (($cont, $code, $act) = m|^([cC0-9sSmM])\s*(\S+)\s*(.*)|) {
			if ($act =~ m|\\$|) {
				$str = $`; next;
			}
			$code = &escape_input($code);
			if ($cont =~ m|^[cC]|) {
				$char{$code} = $act;
			} elsif ($cont > $cont_code || (($is_moderator || $is_superuser) && $cont != 1) || ($is_moderator && $cont =~ m|^[mM]|) || ($is_superuser && $cont =~ m|^[sS]|)) {
				next if ($cont == 1 && $GLOBAL_OPTIONS{'images'} == 0 && $GLOBAL_OPTIONS{'options_used'} == 1);
				next if $code eq "attach" && !$pro;
				next if ($cont =~ m|^[mM]| && !$is_moderator);
				next if ($cont =~ m|^[sS]| && !$is_superuser);
				$tags{$code} = $act;
				$str_ = $tags{$code};
				while ($str_ =~ m|!VAR\((\w+)\)!|) {
					$before = $`;
					$after = $';
					$f = $1;
					$f = "\$" . $f;
					$f =~ s/"/\\"/g;
					$result = eval qq/"$f"/;
					$str_ = $before . $result . $after;
				}
				$tags{$code} = $str_;
			}
		}
	}
	close (WTCONF);
}

#---SEPARATOR---#
#REQ:webtags

sub reapply_webtags {
	my ($text, $message) = @_;
	my (%source);
	$flag = 0;
	@lines = split(/\n/, $text);
	foreach $line (@lines) {
		if ($line =~ m|<!-Source: (\d+)-!>|) {
			$flag = $1;
		} elsif ($line =~ m|<!-/Source: $flag-!>|) {
			$flag = 0;
		} elsif ($flag) {
			$source{$flag} .= $line;
		}
	}
	foreach $key (keys(%source)) {
		$u = &unescape($source{$key});
		if ($u =~ m|\\image_alreadyuploaded| || $u =~ m|\\jpeg_alreadyuploaded| || $u =~ m|attachment_alreadyuploaded|) {
			($lint, $message_var_replace) = &webtags($u, 0, 1);
			@lines = split(/\n/, $message);
			$ctr = 0; $flag = 0;
			foreach $line (@lines) {
				$ctr += 1;
				if ($line =~ m|<!-/Post: $key-!>|) {
					$flag = ($ctr - 1);
					last;
				}
			}
			if ($flag) {
				$lines[$flag-1] = $message_var_replace;
			}
			$message = join("\n", @lines);
		}
	}
	return $message;
}

#---SEPARATOR---#

sub manage_images {
	my ($source_in, $topic_number, $queue, $action) = @_;
	my (@file);
	while ($source_in =~ m|\\image_alreadyuploaded\{(\d+)|) {
		push (@file, "$1.gif");
		$source_in = join("", $`, $');
	}
	while ($source_in =~ m|\\jpeg_alreadyuploaded\{(\d+)|) {
		push (@file, "$1.jpg");
		$source_in = join("", $`, $');
	}
	if ($queue != 0) {
		$dir = "";
	} else {
		if (-e "$message_dir/$topic_number") {
			$dir = "$message_dir/$topic_number";
		} else {
			$dir = "$secdir/$topic_number";
		}
	}
	if ($action eq "delete") {
		foreach $line (@file) {
			(unlink("$dir/$line"));
		}
	} elsif ($action eq "list") {
		foreach $line (@file) {
			$line = "$dir/$line";
		}
		return @file;
	}
}

#---SEPARATOR---#

sub inverse_webtags {
	my ($string, $tn) = @_;
	my (@wt, @y, $type, @r, $stringtest, $code, $b, $a, $i2, $mycode, $inter, $flag, $code, $stringin, $global_max, $key, $kqm, %x);
	$topic_number = $tn if $tn;
	$stringin = $string; $global_max = 0;
	if (scalar(keys(%wtdef)) == 0) {
		open (WTCONF, "<$admin_dir/webtags.conf");
		@wt = <WTCONF>;
		close (WT);
		$flag = "";
		foreach $_ (@wt) {
			next if m|^#|;
			next if !m|\S|;
			if ($flag ne "") {
				s/^\s+//; s/\s+$//;
				while ($_ =~ m|!VAR\((\w+)\)!|) {
					$o = $1;
					$_ = join("", $`, ${$o}, $');
				}
				if (m|(\s*)\\$|) {
					$wtdef{$flag} .= " " . $` if $type ne "C";
					$chdef{$flag} .= " " . $` if $type eq "C";
				} else {
					$wtdef{$flag} .= " " . $_ if $type ne "C";
					$chdef{$code} .= " " . $_ if $type eq "C";
					while ($inter =~ m%\|(\d+)\|%g) {
						$max{$flag} = $1 if $1 > $max{$flag};
						$global_max = $1 if $1 > $global_max;
					}
					$flag = "";
				}
				next;
			}
			if (($type, $code, $inter) = split(/\s+/, $_, 3)) {
				$inter =~ s/\s+$//;
				while ($inter =~ m|!VAR\((\w+)\)!|) {
					$o = $1;
					$inter = join("", $`, ${$o}, $');
				}
				if ($inter =~ m|(\s*)\\$|) {
					$wtdef{$code} = $` if $type ne "C";
					$chdef{$code} = $` if $type eq "C";
					$flag = $code;
					next;
				}
				if ($type =~ m|^[cC]|) {
					$chdef{$code} = $inter;
				} else {
					$wtdef{$code} = $inter;
					while ($inter =~ m%\|(\d+)\|%g) {
						$max{$code} = $1 if $1 > $max{$code};
						$global_max = $1 if $1 > $global_max;
					}
				}
			}
		}
		$GLOBAL_MAX = $global_max;
		%savemax = %max;
	} else {
		$global_max = $GLOBAL_MAX;
		%max = %savemax;
	}
	$string = &char_convert($string, 1);
	delete $wtdef{"image"}; delete $max{"image"};
	delete $wtdef{"attach"}; delete $max{"attach"};
	foreach $key (keys(%chdef)) {
		$chdef{$key} = &char_convert($chdef{$key}, 1);
		$chdef{$key} =~ s/&#60;/</g;
		$chdef{$key} =~ s/&#62;/</g;
		$kqm = quotemeta($chdef{$key});
		$string =~ s/$kqm/\\ch\{$key\}/g;
	}
O:	for ($i = $global_max; $i >= 1; $i-= 1) {
I:		foreach $key (keys(%max)) {
			next I if $max{$key} != $i;
			next I if $key eq "rgb";
			next I if $key eq ("link" || $key eq "topurl" || $key eq "newurl" || $key eq "mail");
			next I if $key eq "font";
			next I if $key eq "imagelink";
			$string = &transit($key, $wtdef{$key}, $max{$key}, $string);
			delete $max{$key};
		}
		$string = &transit("mail", $wtdef{mail}, $max{mail}, $string) if $i == 2;
		$string = &transit("newurl", $wtdef{newurl}, $max{newurl}, $string) if $i == 2;
		$string = &transit("topurl", $wtdef{topurl}, $max{topurl}, $string) if $i == 2;
		$string = &transit("link", $wtdef{link}, $max{link}, $string) if $i == 2;
	}
	$string = &transit("rgb", $wtdef{rgb}, $max{rgb}, $string);
	$string = &transit("font", $wtdef{font}, $max{font}, $string);
	$string = &transit("imagelink", $wtdef{imagelink}, $max{imagelink}, $string);
	$string =~ s/<BR>/\n/g;
	return $string;
}

sub transit {
	my ($key, $definition, $max, $string) = @_;
	my ($flag, $inter, %x, %y, $type, @r, @y, $i2, $kqm);
	return $string if $definition eq "";
	$flag = 0;
	$inter = $definition;
	undef %x; undef %y;
	$type = "";
	while ($inter =~ m%\|(\d+)\|%) {
		$inter = $';
		if ($y{$1}) {
			$type .= quotemeta($`);
			$type .= "\\$y{$1}";
		} else {
			$flag += 1;
			$x{$flag} = $1;
			$type .= quotemeta($`);
			$type .= "(.*?)";
		}
		$y{$1} = $flag;
	}
	$type .= quotemeta($inter);
	while (@r = ($string =~ m%$type%)) {
		$b = $`; $a = $';
		undef @y;
		for ($i2 = 1; $i2 <= $max; $i2++) {
			$y[$x{$i2}-1] = $r[$i2-1];
		}
		$kqm = join(",", @y);
		if ($key eq "table" || $key eq "tablenb" || $key eq $L{TABLE_TAG} || $key eq $L{TABLE_NB_TAG}) {
			if (grep(/[^\\],/, @y)) {
				$kqm =~ s%</TD><TD>%\t%g;
			} else {
				$kqm =~ s%</TD><TD>%,%g;
			}
			$kqm =~ s%\s?</TD></TR><TR><TD>%<BR>%g;
		} elsif ($key eq "list" || $key eq $L{LIST_TAG} || $key eq "olist" || $L{OLIST_TAG} eq $key) {
			$kqm =~ s%\s?<LI>%<BR>%g;
		}
		$string = join("", $b, "\\$key", "{", $kqm, "}", $a);
	}
	return $string;
}

# END - FILE IS CORRECTLY UPLOADED #
