[
'title' => '基本资料',
'float' => 'right',
'titleColor' => '#0088dd',
],
'基本资料' => [
'title' => '基本资料',
'float' => 'right',
'titleColor' => '#0088dd',
],
'参数' => [
'title' => '基本参数 (0-10)',
'titleColor' => '#33ccff',
],
'基本参数' => [
'title' => '基本参数 (0-10)',
'titleColor' => '#33ccff',
],
];
public function __construct($args){
$this->parseArgs($args);
}
public function parseArgs($args){
foreach($args as $one){
if(is_string($one)){
if($one == '居右'){
$this->float = 'right';
} elseif($one == '居左'){
$this->float = 'left';
} elseif($this->avatar == '' && preg_match('/\[\[文件:/', $one)){
$this->avatar = str_replace(['|缩略图', '|有框'], '|无框', $one);
} elseif($this->title == '') {
preg_match('/\[(#[0-9a-fA-F]{3,6}|#[0-9a-fA-F]{3,6},#[0-9a-fA-F]{3,6}|,#[0-9a-fA-F]{3,6})\]/', $one, $match);
if(count($match) > 0){
$this->titleColor = str_replace(['[', ']'], '', $match[0]);
$this->title = trim(str_replace($match[0], '', $one));
} else {
$this->title = $one;
}
if(isset($this->defaultSet[$this->title])){
$data = $this->defaultSet[$this->title];
if(isset($data['title'])){
$this->title = $data['title'];
}
if(isset($data['float']) && $this->float == ''){
$this->float = $data['float'];
}
if(isset($data['titleColor']) && $this->titleColor == ''){
$this->titleColor = $data['titleColor'];
}
}
}
} else {
$this->datas[$one[0]] = $one[1];
}
}
}
public function getHtml(){
$ret = "
float){
case 'left':
$ret .= ' style="float: left; margin-right: 5px;"';
break;
case 'right':
$ret .= ' style="float: right; margin-left: 5px;"';
break;
}
$ret .= ">\r\n{| class=\"wikitable\"\r\n";
if($this->avatar != ''){
$ret .= "|-\r\n| colspan=2 | " . $this->avatar . "\r\n";
}
if($this->title != ''){
$ret .= "|-\r\n| colspan=2 style=\"text-align:center;";
//开始填充style项目
if($this->titleColor != ''){
$ret .= 'background-color:' . $this->titleColor . ';';
if($this->isLightColor($this->titleColor)){
$ret .= 'color:#000;';
} else {
$ret .= 'color:#fff;';
}
}
//填充内容
$ret .= '" | ' . $this->title . "\r\n";
}
foreach($this->datas as $name => $value){
$ret .= "|-\r\n| " . $name . " || style=\"text-align:center\" | " . $value . "\r\n";
}
$ret .= "|}\r\n
";
return $ret;
}
public function getRgb($hex){
$hex = str_replace('#', '', $hex);
$rgb = [];
if(strlen($hex) == 3){
$rgb[0] = $hex[0] . $hex[0];
$rgb[1] = $hex[1] . $hex[1];
$rgb[2] = $hex[2] . $hex[2];
} else {
$rgb[0] = $hex[0] . $hex[1];
$rgb[1] = $hex[2] . $hex[3];
$rgb[2] = $hex[4] . $hex[5];
}
foreach($rgb as $key => $one){
$rgb[$key] = ord(hex2bin($one));
}
return $rgb;
}
public function isLightColor($hex){
$rgb = $this->getRgb($hex);
return array_sum($rgb) >= 382;
}
public static function onParserSetup(&$parser){
$parser->setFunctionHook('infobox', 'Isekai\\InfoBox::createInfoBox');
return true;
}
public static function createInfoBox(){
$args = func_get_args();
$targetArgs = [];
for($i = 1; $i < count($args); $i ++){
$t = explode('=', $args[$i]);
if(count($t) == 2){
$targetArgs[] = [trim($t[0]), trim($t[1])];
} else {
$targetArgs[] = $args[$i];
}
}
$infobox = new InfoBox($targetArgs);
return $infobox->getHtml();
}
}