| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- <?php
- namespace App\Http\Api;
- use Illuminate\Support\Str;
- use mustache\mustache;
- use App\Models\DhammaTerm;
- use App\Models\PaliText;
- use App\Models\Channel;
- use App\Http\Controllers\CorpusController;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- define("STACK_DEEP",8);
- class MdRender{
- /**
- * 按照{{}}把字符串切分成三个部分。模版之前的,模版,和模版之后的
- */
- public static function tplSplit($tpl){
- $before = strpos($tpl,'{{');
- if($before === FALSE){
- //未找到
- return ['data'=>[$tpl,'',''],'error'=>0];
- }else{
- $pointer = $before;
- $stack = array();
- $stack[] = $pointer;
- $after = substr($tpl,$pointer+2) ;
- while (!empty($after) && count($stack)>0 && count($stack)<STACK_DEEP) {
- $nextBegin = strpos($after,"{{");
- $nextEnd = strpos($after,"}}");
- if($nextBegin !== FALSE){
- if($nextBegin < $nextEnd){
- //有嵌套找到最后一个}}
- $pointer = $pointer + 2 + $nextBegin;
- $stack[] = $pointer;
- $after = substr($tpl,$pointer+2);
- }else if($nextEnd !== FALSE){
- //无嵌套有结束
- $pointer = $pointer + 2 + $nextEnd;
- array_pop($stack);
- $after = substr($tpl,$pointer+2);
- }else{
- //无结束符 没找到
- break;
- }
- }else if($nextEnd !== FALSE){
- $pointer = $pointer + 2 + $nextEnd;
- array_pop($stack);
- $after = substr($tpl,$pointer+2);
- }else{
- //没找到
- break;
- }
- }
- if(count($stack)>0){
- if(count($stack) === STACK_DEEP){
- return ['data'=>[$tpl,'',''],'error'=>2];
- }else{
- //未关闭
- return ['data'=>[$tpl,'',''],'error'=>1];
- }
- }else{
- return ['data'=>
- [
- substr($tpl,0,$before),
- substr($tpl,$before,$pointer-$before+2),
- substr($tpl,$pointer+2)
- ],
- 'error'=>0
- ];
- }
- }
- }
- public static function wiki2xml(string $wiki,$channelId=[],$mode='read'):string{
- /**
- * 替换{{}} 到xml之前 要先把换行符号去掉
- */
- //$wiki = str_replace("\n","",$wiki);
- /**
- * 把模版转换为xml
- */
- $remain = $wiki;
- $buffer = array();
- do {
- $arrWiki = MdRender::tplSplit($remain);
- $buffer[] = $arrWiki['data'][0];
- $tpl = $arrWiki['data'][1];
- if(!empty($tpl)){
- /**
- * 处理模版 提取参数
- */
- $tpl = str_replace("|\n","|",$tpl);
- $pattern = "/\{\{(.+?)\|/";
- $replacement = '<MdTpl name="$1"><param>';
- $tpl = preg_replace($pattern,$replacement,$tpl);
- $tpl = str_replace("}}","</param></MdTpl>",$tpl);
- $tpl = str_replace("|","</param><param>",$tpl);
- /**
- * 替换变量名
- */
- $pattern = "/<param>([a-z]+?)=/";
- $replacement = '<param name="$1">';
- $tpl = preg_replace($pattern,$replacement,$tpl);
- //tpl to react
- $tpl = MdRender::xml2tpl($tpl,$channelId,$mode);
- $buffer[] = $tpl;
- }
- $remain = $arrWiki['data'][2];
- } while (!empty($remain));
- $html = implode('' , $buffer);
- return $html;
- }
- public static function xmlQueryId(string $xml, string $id):string{
- try{
- $dom = simplexml_load_string($xml);
- }catch(\Exception $e){
- Log::error($e);
- return "<div></div>";
- }
- $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 "<div></div>";
- }
- 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 "<span>xml解析错误{$e}</span>";
- }
- if(!$dom){
- Log::error($xml);
- return "<span>xml解析错误</span>";
- }
- /*
- $doc = new \DOMDocument();
- $xml = str_replace('MdTpl','dfn',$xml);
- $ok = $doc->loadHTML($xml,LIBXML_HTML_NODEFDTD | LIBXML_DTDVALID);
- if(!$ok){
- return "<span>xml解析错误</span>";
- }
- */
- $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++;
- $paramName = "";
- foreach($param->attributes() as $pa => $pa_value){
- if($pa === "name"){
- $props["{$pa_value}"] = $param->__toString();
- $paramName = $pa_value;
- }
- }
- if(empty($paramName)){
- $props["{$param_id}"] = $param->__toString();
- }
- }
- }
- /**
- * 生成模版参数
- */
- $channelInfo = Channel::whereIn('uid',$channelId)->get();
- $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('<?xml version="1.0"?>','',$dom->asXML()) ;
- $html = str_replace(['<xml>','</xml>'],['<span>','</span>'],$html);
- return trim($html);
- }
- public static function render2($markdown,$channelId=[],$queryId=null,$mode='read',$channelType,$contentType="markdown"){
- if(empty($markdown)){
- return "<span></span>";
- }
- $wiki = MdRender::markdown2wiki($markdown,$channelType,$contentType);
- $html = MdRender::wiki2xml($wiki,$channelId,$mode);
- if(!is_null($queryId)){
- $html = MdRender::xmlQueryId($html, $queryId);
- }
- $html = MdRender::markdownToHtml($html);
- //$tpl = MdRender::xml2tpl($html,$channelId,$mode);
- //生成可展开组件
- $html = str_replace("<div/>","<div></div>",$html);
- $pattern = '/<li><div>(.+?)<\/div><\/li>/';
- $replacement = '<li><MdTpl name="toggle" tpl="toggle" props=""><div>$1</div></MdTpl></li>';
- $html = preg_replace($pattern,$replacement,$html);
- return $html;
- }
- public static function markdown2wiki(string $markdown,$channelType=null,$contentType=null): string{
- //$markdown = mb_convert_encoding($markdown,'UTF-8','UTF-8');
- $markdown = iconv('UTF-8','UTF-8//IGNORE',$markdown);
- /**
- * 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 if($contentType === "markdown"){
- $lines = explode("\n",$markdown);
- $newLines = array();
- foreach ($lines as $line) {
- if(strstr($line,'=') === FALSE){
- $newLines[] = $line;
- }else{
- $nissaya = explode('=',$line);
- $meaning = array_slice($nissaya,1);
- $meaning = implode('=',$meaning);
- $newLines[] = "{{nissaya|{$nissaya[0]}|{$meaning}}}";
- }
- }
- $markdown = implode("\n",$newLines);
- }
- }
- //$markdown = preg_replace("/\n\n/","<div></div>",$markdown);
- /**
- * 处理 mermaid
- */
- if(strpos($markdown,"```mermaid") !== false){
- $lines = explode("\n",$markdown);
- $newLines = array();
- $mermaidBegin = false;
- $mermaidString = array();
- foreach ($lines as $line) {
- if($line === "```mermaid"){
- $mermaidBegin = true;
- $mermaidString = [];
- continue;
- }
- if($mermaidBegin){
- if($line === "```"){
- $newLines[] = "{{mermaid|".base64_encode(\json_encode($mermaidString))."}}";
- $mermaidBegin = false;
- }else{
- $mermaidString[] = $line;
- }
- }else{
- $newLines[] = $line;
- }
- }
- $markdown = implode("\n",$newLines);
- }
- /**
- * 替换换行符
- * react 无法处理 <br> 替换为<div></div>代替换行符作用
- */
- $markdown = str_replace('<br>','<div></div>',$markdown);
- /**
- * markdown -> html
- */
- /*
- $html = MdRender::fixHtml($html);
- */
- #替换术语
- $pattern = "/\[\[(.+?)\]\]/";
- $replacement = '{{term|$1}}';
- $markdown = preg_replace($pattern,$replacement,$markdown);
- #替换句子模版
- $pattern = "/\{\{([0-9].+?)\}\}/";
- $replacement = '{{sent|$1}}';
- $markdown = preg_replace($pattern,$replacement,$markdown);
- /**
- * 替换多行注释
- * ```
- * bla
- * bla
- * ```
- * {{note|
- * bla
- * bla
- * }}
- */
- if(strpos($markdown,"```\n") !== false){
- $lines = explode("\n",$markdown);
- $newLines = array();
- $noteBegin = false;
- $noteString = array();
- foreach ($lines as $line) {
- if($noteBegin){
- if($line === "```"){
- $newLines[] = "}}";
- $noteBegin = false;
- }else{
- $newLines[] = $line;
- }
- }else{
- if($line === "```"){
- $noteBegin = true;
- $newLines[] = "{{note|";
- continue;
- }else{
- $newLines[] = $line;
- }
- }
- }
- if($noteBegin){
- $newLines[] = "}}";
- }
- $markdown = implode("\n",$newLines);
- }
- /**
- * 替换单行注释
- * `bla bla`
- * {{note|bla}}
- */
- $pattern = "/`(.+?)`/";
- $replacement = '{{note|$1}}';
- $markdown = preg_replace($pattern,$replacement,$markdown);
- /*
- #替换多行注释
- #<pre><code>bla</code></pre>
- #{{note|bla}}
- $pattern = '/<pre><code>([\w\W]+?)<\/code><\/pre>/';
- $replacement = '{{note|$1}}';
- $html = preg_replace($pattern,$replacement,$html);
- #替换单行注释
- #<code>bla</code>
- #{{note|bla}}
- $pattern = '/<code>(.+?)<\/code>/';
- $replacement = '{{note|$1}}';
- $html = preg_replace($pattern,$replacement,$html);
- */
- return $markdown;
- }
- public static function markdownToHtml($markdown){
- $markdown = str_replace('MdTpl','mdtpl',$markdown);
- $markdown = str_replace(['<param','</param>'],['<span','</span>'],$markdown);
- $Parsedown = new \Parsedown();
- $html = $Parsedown->text($markdown);
- Log::info($html);
- $html = MdRender::fixHtml($html);
- $html = str_replace('<hr>','<hr />',$html);
- //给H1-6 添加uuid
- for ($i=1; $i<7 ; $i++) {
- if(strpos($html,"<h{$i}>")===false){
- continue;
- }
- $output = array();
- $input = $html;
- $hPos = strpos($input,"<h{$i}>");
- while ($hPos !== false) {
- $output[] = substr($input,0,$hPos);
- $output[] = "<h{$i} id='".Str::uuid()."'>";
- $input = substr($input,$hPos+4);
- $hPos = strpos($input,"<h{$i}>");
- }
- $output[] = $input;
- $html = implode('',$output);
- }
- $html = str_replace('mdtpl','MdTpl',$html);
- return $html;
- }
- /**
- * string[] $channelId
- */
- public static function render($markdown,$channelId,$queryId=null,$mode='read',$channelType='translation',$contentType="markdown"){
- return MdRender::render2($markdown,$channelId,$queryId,$mode,$channelType,$contentType);
- }
- public static function fixHtml($html) {
- $doc = new \DOMDocument();
- libxml_use_internal_errors(true);
- $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
- $doc->loadHTML('<span>'.$html.'</span>',LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
- $fixed = $doc->saveHTML();
- $fixed = mb_convert_encoding($fixed, "UTF-8", 'HTML-ENTITIES');
- return $fixed;
- }
- }
|