","
bla
#{{note:bla}}
$pattern = '/(.+?)<\/code>/';
$replacement = '{{note|$1}}';
$html = preg_replace($pattern,$replacement,$html);
return $html;
}
/**
*
*/
public static function render($markdown,$channelId,$queryId=null,$mode='read'){
return MdRender::render2($markdown,$channelId,$queryId,$mode);
$html = MdRender::markdown2wiki($markdown);
/**
* 转换为Mustache模版
*/
$pattern = "/\{\{(.+?)\}\}/";
$replacement = "\n{{#function}}\n$1\n{{/function}}\n";
$html = preg_replace($pattern,$replacement,$html);
/**
* Mustache_Engine 处理Mustache模版
* 把Mustache模版内容转换为react组件
*/
$m = new \Mustache_Engine(array('entity_flags' => ENT_QUOTES));
$html = $m->render($html, array(
'function' => function($text) use($m,$channelId) {
//1: 解析
$param = explode("|",$text);
//3: 处理业务逻辑
$tplName = trim($param[0]);
$innerString = "";
switch($tplName){
case 'term':
//获取实际的参数
$word = trim($param[1]);
$props = Cache::remember("/term/{$channelId}/{$word}",
60,
function() use($word,$channelId){
$tplParam = DhammaTerm::where("word",$word)->first();
$output = [
"word" => $word,
"channel" => $channelId,
];
$innerString = $output["word"];
if($tplParam){
$output["id"] = $tplParam->guid;
$output["meaning"] = $tplParam->meaning;
$innerString = $output["meaning"];
if(!empty($tplParam->other_meaning)){
$output["meaning2"] = $tplParam->other_meaning;
}
}
return $output;
});
break;
case 'note':
if(isset($param[1])){
$props = ["note"=>trim($param[1])];
}
if(isset($param[2])){
$props["trigger"] = trim($param[2]);
$innerString = $props["trigger"];
}
break;
case 'sent':
$tplName = "sentedit";
$innerString = "";
$sentInfo = explode('@',trim($param[1]));
$sentId = $sentInfo[0];
$Sent = new CorpusController();
if(empty($channelId)){
$channels = [];
}else{
$channels = [$channelId];
}
if(isset($sentInfo[1])){
$channels = [$sentInfo[1]];
}
$html = $Sent->getSentTpl($param[1],$channels);
return $html;
break;
case 'quote':
$paraId = trim($param[1]);
$props = Cache::remember("/quote/{$channelId}/{$paraId}",
60,
function() use($paraId,$channelId){
$para = \explode('-',$paraId);
$output = [
"paraId" => $paraId,
"channel" => $channelId,
"innerString" => $paraId,
];
if(count($para)<2){
return $output;
}
$PaliText = PaliText::where("book",$para[0])
->where("paragraph",$para[1])
->select(['toc','path'])
->first();
if($PaliText){
$output["pali"] = $PaliText->toc;
$output["paliPath"] = \json_decode($PaliText->path);
$innerString = $PaliText->toc;
}
return $output;
});
break;
case 'exercise':
$exeId = trim($param[1]);
$exeContent = trim($param[2]);
$props = Cache::remember("/quote/{$channelId}/{$exeId}",
60,
function() use($exeId,$channelId){
$output = [
"id" => $exeId,
"channel" => $channelId,
];
return $output;
});
#替换句子
$pattern = "/\(\((.+?)\)\)/";
$replacement = '{{sent|$1}}';
Log::info("content{$exeContent}");
$exeContent = preg_replace($pattern,$replacement,$exeContent);
Log::info("content{$exeContent}");
$innerString = MdRender::render($exeContent,$channelId);
break;
default:
break;
}
//4: 返回拼好的字符串
$props = base64_encode(\json_encode($props));
$html = "{$innerString} ";
return $html;
}
));
if(substr_count($html,"") === 1){
$html = \str_replace(['
','
'],'',$html);
}
//LOG::info($html);
return "{$html} ";
}
}