TextFormat: reduce hella duplicated code in toHTML()

This commit is contained in:
Dylan K. Taylor 2024-12-10 14:11:11 +00:00
parent 6817215683
commit ba93665fe7
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -224,136 +224,46 @@ abstract class TextFormat{
$newString = ""; $newString = "";
$tokens = 0; $tokens = 0;
foreach(self::tokenize($string) as $token){ foreach(self::tokenize($string) as $token){
switch($token){ $formatString = match($token){
case TextFormat::BOLD: TextFormat::BLACK => "color:#000",
$newString .= "<span style=font-weight:bold>"; TextFormat::DARK_BLUE => "color:#00A",
++$tokens; TextFormat::DARK_GREEN => "color:#0A0",
break; TextFormat::DARK_AQUA => "color:#0AA",
case TextFormat::OBFUSCATED: TextFormat::DARK_RED => "color:#A00",
//$newString .= "<span style=text-decoration:line-through>"; TextFormat::DARK_PURPLE => "color:#A0A",
//++$tokens; TextFormat::GOLD => "color:#FA0",
break; TextFormat::GRAY => "color:#AAA",
case TextFormat::ITALIC: TextFormat::DARK_GRAY => "color:#555",
$newString .= "<span style=font-style:italic>"; TextFormat::BLUE => "color:#55F",
++$tokens; TextFormat::GREEN => "color:#5F5",
break; TextFormat::AQUA => "color:#5FF",
case TextFormat::RESET: TextFormat::RED => "color:#F55",
$newString .= str_repeat("</span>", $tokens); TextFormat::LIGHT_PURPLE => "color:#F5F",
$tokens = 0; TextFormat::YELLOW => "color:#FF5",
break; TextFormat::WHITE => "color:#FFF",
TextFormat::MINECOIN_GOLD => "color:#dd0",
//Colors TextFormat::MATERIAL_QUARTZ => "color:#e2d3d1",
case TextFormat::BLACK: TextFormat::MATERIAL_IRON => "color:#cec9c9",
$newString .= "<span style=color:#000>"; TextFormat::MATERIAL_NETHERITE => "color:#44393a",
++$tokens; TextFormat::MATERIAL_REDSTONE => "color:#961506",
break; TextFormat::MATERIAL_COPPER => "color:#b4684d",
case TextFormat::DARK_BLUE: TextFormat::MATERIAL_GOLD => "color:#deb02c",
$newString .= "<span style=color:#00A>"; TextFormat::MATERIAL_EMERALD => "color:#119f36",
++$tokens; TextFormat::MATERIAL_DIAMOND => "color:#2cb9a8",
break; TextFormat::MATERIAL_LAPIS => "color:#20487a",
case TextFormat::DARK_GREEN: TextFormat::MATERIAL_AMETHYST => "color:#9a5cc5",
$newString .= "<span style=color:#0A0>"; TextFormat::BOLD => "font-weight:bold",
++$tokens; TextFormat::ITALIC => "font-style:italic",
break; default => null
case TextFormat::DARK_AQUA: };
$newString .= "<span style=color:#0AA>"; if($formatString !== null){
++$tokens; $newString .= "<span style=\"$formatString\">";
break; ++$tokens;
case TextFormat::DARK_RED: }elseif($token === TextFormat::RESET){
$newString .= "<span style=color:#A00>"; $newString .= str_repeat("</span>", $tokens);
++$tokens; $tokens = 0;
break; }else{
case TextFormat::DARK_PURPLE: $newString .= $token;
$newString .= "<span style=color:#A0A>";
++$tokens;
break;
case TextFormat::GOLD:
$newString .= "<span style=color:#FA0>";
++$tokens;
break;
case TextFormat::GRAY:
$newString .= "<span style=color:#AAA>";
++$tokens;
break;
case TextFormat::DARK_GRAY:
$newString .= "<span style=color:#555>";
++$tokens;
break;
case TextFormat::BLUE:
$newString .= "<span style=color:#55F>";
++$tokens;
break;
case TextFormat::GREEN:
$newString .= "<span style=color:#5F5>";
++$tokens;
break;
case TextFormat::AQUA:
$newString .= "<span style=color:#5FF>";
++$tokens;
break;
case TextFormat::RED:
$newString .= "<span style=color:#F55>";
++$tokens;
break;
case TextFormat::LIGHT_PURPLE:
$newString .= "<span style=color:#F5F>";
++$tokens;
break;
case TextFormat::YELLOW:
$newString .= "<span style=color:#FF5>";
++$tokens;
break;
case TextFormat::WHITE:
$newString .= "<span style=color:#FFF>";
++$tokens;
break;
case TextFormat::MINECOIN_GOLD:
$newString .= "<span style=color:#dd0>";
++$tokens;
break;
case TextFormat::MATERIAL_QUARTZ:
$newString .= "<span style=color:#e2d3d1>";
++$tokens;
break;
case TextFormat::MATERIAL_IRON:
$newString .= "<span style=color:#cec9c9>";
++$tokens;
break;
case TextFormat::MATERIAL_NETHERITE:
$newString .= "<span style=color:#44393a>";
++$tokens;
break;
case TextFormat::MATERIAL_REDSTONE:
$newString .= "<span style=color:#961506>";
++$tokens;
break;
case TextFormat::MATERIAL_COPPER:
$newString .= "<span style=color:#b4684d>";
++$tokens;
break;
case TextFormat::MATERIAL_GOLD:
$newString .= "<span style=color:#deb02c>";
++$tokens;
break;
case TextFormat::MATERIAL_EMERALD:
$newString .= "<span style=color:#119f36>";
++$tokens;
break;
case TextFormat::MATERIAL_DIAMOND:
$newString .= "<span style=color:#2cb9a8>";
++$tokens;
break;
case TextFormat::MATERIAL_LAPIS:
$newString .= "<span style=color:#20487a>";
++$tokens;
break;
case TextFormat::MATERIAL_AMETHYST:
$newString .= "<span style=color:#9a5cc5>";
++$tokens;
break;
default:
$newString .= $token;
break;
} }
} }