';
$html = preg_replace($pattern,$replacement,$html);
$html = str_replace("}}","",$html);
$html = str_replace("|","",$html);
/**
* 替换变量名
*/
$pattern = "/([a-z]+?)=/";
$replacement = '';
$html = preg_replace($pattern,$replacement,$html);
$html = str_replace("","
",$html);
$html = str_replace("","
",$html);
$html = "".$html."";
return $html;
}
public static function xmlQueryId(string $xml, string $id):string{
try{
$dom = simplexml_load_string($xml);
}catch(\Exception $e){
Log::error($e);
return "";
}
$tpl_list = $dom->xpath('//MdTpl');
foreach ($tpl_list as $key => $tpl) {
foreach ($tpl->children() as $param) {
# 处理每个参数
if($param->getName() === "param"){
foreach($param->attributes() as $pa => $pa_value){
$pValue = $pa_value->__toString();
if($pa === "name" && $pValue === "id"){
if($param->__toString() === $id){
return $tpl->asXML();
}
}
}
}
}
}
return "";
}
public static function take_sentence(string $xml):array{
$output = [];
try{
$dom = simplexml_load_string($xml);
}catch(\Exception $e){
Log::error($e);
return $output;
}
$tpl_list = $dom->xpath('//MdTpl');
foreach ($tpl_list as $key => $tpl) {
foreach($tpl->attributes() as $a => $a_value){
if($a==="name"){
if($a_value->__toString() ==="sent"){
foreach ($tpl->children() as $param) {
# 处理每个参数
if($param->getName() === "param"){
$sent = $param->__toString();
if(!empty($sent)){
$output[] = $sent;
break;
}
}
}
}
}
}
}
return $output;
}
public static function xml2tpl(string $xml, $channelId="",$mode='read'):string{
/**
* 解析xml
* 获取模版参数
* 生成react 组件参数
*/
try{
$dom = simplexml_load_string($xml);
}catch(\Exception $e){
Log::error($e);
Log::error($xml);
return "xml解析错误{$e}";
}
$channelInfo = Channel::find($channelId);
$tpl_list = $dom->xpath('//MdTpl');
foreach ($tpl_list as $key => $tpl) {
/**
* 遍历 MdTpl 处理参数
*/
$props = [];
$tpl_name = '';
foreach($tpl->attributes() as $a => $a_value){
if($a==="name"){
$tpl_name = $a_value;
}
}
$param_id = 0;
foreach ($tpl->children() as $param) {
# 处理每个参数
if($param->getName() === "param"){
$param_id++;
$props["{$param_id}"] = $param->__toString();
foreach($param->attributes() as $pa => $pa_value){
if($pa === "name"){
$props["{$pa_value}"] = $param->__toString();
}
}
}
}
/**
* 生成模版参数
*/
$tplRender = new TemplateRender($props,$channelInfo,$mode);
$tplProps = $tplRender->render($tpl_name);
if($tplProps){
$tpl->addAttribute("props",$tplProps['props']);
$tpl->addAttribute("tpl",$tplProps['tpl']);
$tpl->addChild($tplProps['tag'],$tplProps['html']);
}
}
$html = str_replace('','',$dom->asXML()) ;
$html = str_replace(['',''],['',''],$html);
return $html;
}
public static function render2($markdown,$channelId='',$queryId=null,$mode='read',$channelType,$contentType="markdown"){
$wiki = MdRender::markdown2wiki($markdown,$channelType,$contentType);
$html = MdRender::wiki2xml($wiki);
if(!is_null($queryId)){
$html = MdRender::xmlQueryId($html, $queryId);
}
$tpl = MdRender::xml2tpl($html,$channelId,$mode);
return $tpl;
}
public static function markdown2wiki(string $markdown,$channelType,$contentType): string{
/**
* nissaya
* aaa=bbb\n
* {{nissaya|aaa|bbb}}
*/
if($channelType==='nissaya'){
if($contentType === "json"){
$json = json_decode($markdown);
$nissayaWord = [];
foreach ($json as $word) {
if(count($word->sn) === 1){
//只输出第一层级
$str = "{{nissaya|";
if(isset($word->word->value)){
$str .= $word->word->value;
}
$str .= "|";
if(isset($word->meaning->value)){
$str .= $word->meaning->value;
}
$str .= "}}";
$nissayaWord[] = $str;
}
}
$markdown = implode('',$nissayaWord);
}else{
$pattern = '/(.+?)=(.+?)\n/';
$replacement = '{{nissaya|$1|$2}}';
$markdown = preg_replace($pattern,$replacement,$markdown);
$pattern = '/(.+?)=(.?)\n/';
$replacement = '{{nissaya|$1|$2}}';
$markdown = preg_replace($pattern,$replacement,$markdown);
$pattern = '/(.?)=(.+?)\n/';
$replacement = '{{nissaya|$1|$2}}';
$markdown = preg_replace($pattern,$replacement,$markdown);
}
}
$markdown = preg_replace("/\n\n/","",$markdown);
/**
* 替换换行符
* react 无法处理
替换为代替换行符作用
*/
$markdown = str_replace('
','',$markdown);
/**
* markdown -> html
*/
$html = Str::markdown($markdown);
#替换术语
$pattern = "/\[\[(.+?)\]\]/";
$replacement = '{{term|$1}}';
$html = preg_replace($pattern,$replacement,$html);
#替换句子模版
$pattern = "/\{\{([0-9].+?)\}\}/";
$replacement = '{{sent|$1}}';
$html = preg_replace($pattern,$replacement,$html);
#替换单行注释
#bla
#{{note|bla}}
$pattern = '/(.+?)<\/code>/';
$replacement = '{{note|$1}}';
$html = preg_replace($pattern,$replacement,$html);
#替换多行注释
#bla
#{{note|bla}}
$pattern = '/([\w\W]+?)<\/code><\/pre>/';
$replacement = '{{note|$1}}';
$html = preg_replace($pattern,$replacement,$html);
return $html;
}
/**
*
*/
public static function render($markdown,$channelId,$queryId=null,$mode='read',$channelType='translation',$contentType="markdown"){
return MdRender::render2($markdown,$channelId,$queryId,$mode,$channelType,$contentType);
}
}