2
0

MdRender.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. <?php
  2. namespace App\Http\Api;
  3. use Illuminate\Support\Str;
  4. use mustache\mustache;
  5. use App\Models\DhammaTerm;
  6. use App\Models\PaliText;
  7. use App\Models\Channel;
  8. use App\Http\Controllers\CorpusController;
  9. use Illuminate\Support\Facades\Cache;
  10. use App\Tools\RedisClusters;
  11. use Illuminate\Support\Facades\Log;
  12. use App\Tools\Markdown;
  13. define("STACK_DEEP",8);
  14. class MdRender{
  15. /**
  16. * 文字渲染模式
  17. * read 阅读模式
  18. * edit 编辑模式
  19. */
  20. protected $options = [
  21. 'mode' => 'read',
  22. 'channelType'=>'translation',
  23. 'contentType'=>"markdown",
  24. 'format'=>'react',
  25. 'debug'=>[],
  26. 'studioId'=>null,
  27. 'lang'=>'zh-Hans',
  28. ];
  29. public function __construct($options=[])
  30. {
  31. foreach ($options as $key => $value) {
  32. $this->options[$key] = $value;
  33. }
  34. }
  35. /**
  36. * 将句子模版组成的段落复制一份,为了实现巴汉逐段对读
  37. */
  38. private function preprocessingForParagraph($input){
  39. if(!$this->options['paragraph']){
  40. return $input;
  41. }
  42. $paragraphs = explode("\n\n",$input);
  43. $output = [];
  44. foreach ($paragraphs as $key => $paragraph) {
  45. # 判断是否是纯粹的句子模版
  46. $pattern = "/\{\{sent\|id=([0-9].+?)\}\}/";
  47. $replacement = '';
  48. $space = preg_replace($pattern,$replacement,$paragraph);
  49. if(empty(trim($space))){
  50. $output[] = str_replace('}}','|text=origin}}',$paragraph);
  51. $output[] = str_replace('}}','|text=translation}}',$paragraph);
  52. }else{
  53. $output[] = $paragraph;
  54. }
  55. }
  56. return implode("\n\n",$output);
  57. }
  58. /**
  59. * 按照{{}}把字符串切分成三个部分。模版之前的,模版,和模版之后的
  60. */
  61. private function tplSplit($tpl){
  62. $before = strpos($tpl,'{{');
  63. if($before === FALSE){
  64. //未找到
  65. return ['data'=>[$tpl,'',''],'error'=>0];
  66. }else{
  67. $pointer = $before;
  68. $stack = array();
  69. $stack[] = $pointer;
  70. $after = substr($tpl,$pointer+2) ;
  71. while (!empty($after) && count($stack)>0 && count($stack)<STACK_DEEP) {
  72. $nextBegin = strpos($after,"{{");
  73. $nextEnd = strpos($after,"}}");
  74. if($nextBegin !== FALSE){
  75. if($nextBegin < $nextEnd){
  76. //有嵌套找到最后一个}}
  77. $pointer = $pointer + 2 + $nextBegin;
  78. $stack[] = $pointer;
  79. $after = substr($tpl,$pointer+2);
  80. }else if($nextEnd !== FALSE){
  81. //无嵌套有结束
  82. $pointer = $pointer + 2 + $nextEnd;
  83. array_pop($stack);
  84. $after = substr($tpl,$pointer+2);
  85. }else{
  86. //无结束符 没找到
  87. break;
  88. }
  89. }else if($nextEnd !== FALSE){
  90. $pointer = $pointer + 2 + $nextEnd;
  91. array_pop($stack);
  92. $after = substr($tpl,$pointer+2);
  93. }else{
  94. //没找到
  95. break;
  96. }
  97. }
  98. if(count($stack)>0){
  99. if(count($stack) === STACK_DEEP){
  100. return ['data'=>[$tpl,'',''],'error'=>2];
  101. }else{
  102. //未关闭
  103. return ['data'=>[$tpl,'',''],'error'=>1];
  104. }
  105. }else{
  106. return ['data'=>
  107. [
  108. substr($tpl,0,$before),
  109. substr($tpl,$before,$pointer-$before+2),
  110. substr($tpl,$pointer+2)
  111. ],
  112. 'error'=>0
  113. ];
  114. }
  115. }
  116. }
  117. private function wiki2xml(string $wiki,$channelId=[]):string{
  118. /**
  119. * 把模版转换为xml
  120. */
  121. $remain = $wiki;
  122. $buffer = array();
  123. do {
  124. $arrWiki = $this->tplSplit($remain);
  125. $buffer[] = $arrWiki['data'][0];
  126. $tpl = $arrWiki['data'][1];
  127. if(!empty($tpl)){
  128. /**
  129. * 处理模版 提取参数
  130. */
  131. $tpl = str_replace("|\n","|",$tpl);
  132. $pattern = "/\{\{(.+?)\|/";
  133. $replacement = '<MdTpl class="tpl" name="$1"><param>';
  134. $tpl = preg_replace($pattern,$replacement,$tpl);
  135. $tpl = str_replace("}}","</param></MdTpl>",$tpl);
  136. $tpl = str_replace("|","</param><param>",$tpl);
  137. /**
  138. * 替换变量名
  139. */
  140. $pattern = "/<param>([a-z]+?)=/";
  141. $replacement = '<param name="$1">';
  142. $tpl = preg_replace($pattern,$replacement,$tpl);
  143. //tpl to react
  144. $tpl = str_replace('<param','<span class="param"',$tpl);
  145. $tpl = str_replace('</param>','</span>',$tpl);
  146. $tpl = $this->xml2tpl($tpl,$channelId);
  147. $buffer[] = $tpl;
  148. }
  149. $remain = $arrWiki['data'][2];
  150. } while (!empty($remain));
  151. $html = implode('' , $buffer);
  152. return $html;
  153. }
  154. private function xmlQueryId(string $xml, string $id):string{
  155. try{
  156. $dom = simplexml_load_string($xml);
  157. }catch(\Exception $e){
  158. Log::error($e);
  159. return "<div></div>";
  160. }
  161. $tpl_list = $dom->xpath('//MdTpl');
  162. foreach ($tpl_list as $key => $tpl) {
  163. foreach ($tpl->children() as $param) {
  164. # 处理每个参数
  165. if($param->getName() === "param"){
  166. foreach($param->attributes() as $pa => $pa_value){
  167. $pValue = $pa_value->__toString();
  168. if($pa === "name" && $pValue === "id"){
  169. if($param->__toString() === $id){
  170. return $tpl->asXML();
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }
  177. return "<div></div>";
  178. }
  179. public static function take_sentence(string $xml):array{
  180. $output = [];
  181. try{
  182. $dom = simplexml_load_string($xml);
  183. }catch(\Exception $e){
  184. Log::error($e);
  185. return $output;
  186. }
  187. $tpl_list = $dom->xpath('//MdTpl');
  188. foreach ($tpl_list as $key => $tpl) {
  189. foreach($tpl->attributes() as $a => $a_value){
  190. if($a==="name"){
  191. if($a_value->__toString() ==="sent"){
  192. foreach ($tpl->children() as $param) {
  193. # 处理每个参数
  194. if($param->getName() === "param"){
  195. $sent = $param->__toString();
  196. if(!empty($sent)){
  197. $output[] = $sent;
  198. break;
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. }
  206. return $output;
  207. }
  208. private function xml2tpl(string $xml, $channelId=[]):string{
  209. /**
  210. * 解析xml
  211. * 获取模版参数
  212. * 生成react 组件参数
  213. */
  214. try{
  215. //$dom = simplexml_load_string($xml);
  216. $doc = new \DOMDocument();
  217. $xml = str_replace('MdTpl','dfn',$xml);
  218. $xml = mb_convert_encoding($xml, 'HTML-ENTITIES', "UTF-8");
  219. $ok = $doc->loadHTML($xml,LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  220. }catch(\Exception $e){
  221. Log::error($e);
  222. Log::error($xml);
  223. return "<span>xml解析错误{$e}</span>";
  224. }
  225. if(!$ok){
  226. return "<span>xml解析错误</span>";
  227. }
  228. /*
  229. if(!$dom){
  230. Log::error($xml);
  231. return "<span>xml解析错误</span>";
  232. }
  233. */
  234. //$tpl_list = $dom->xpath('//MdTpl');
  235. $tpl_list = $doc->getElementsByTagName('dfn');
  236. foreach ($tpl_list as $key => $tpl) {
  237. /**
  238. * 遍历 MdTpl 处理参数
  239. */
  240. $props = [];
  241. $tpl_name = '';
  242. foreach($tpl->attributes as $a => $a_value){
  243. if($a_value->nodeName==="name"){
  244. $tpl_name = $a_value->nodeValue;
  245. break;
  246. }
  247. }
  248. $param_id = 0;
  249. $child = $tpl->firstChild;
  250. while ($child) {
  251. # 处理每个参数
  252. if($child->nodeName === "span"){
  253. $param_id++;
  254. $paramName = "";
  255. foreach($child->attributes as $pa => $pa_value){
  256. if($pa_value->nodeName === "name"){
  257. $nodeText = $pa_value->nodeValue;
  258. $props["{$nodeText}"] = $child->nodeValue;
  259. $paramName = $pa_value;
  260. }
  261. }
  262. if(empty($paramName)){
  263. foreach ($child->childNodes as $param_child) {
  264. # code...
  265. if($param_child->nodeType ===3){
  266. $props["{$param_id}"] = $param_child->nodeValue;
  267. }
  268. }
  269. }
  270. }
  271. $child = $child->nextSibling;
  272. }
  273. /**
  274. * 生成模版参数
  275. *
  276. */
  277. //TODO 判断$channelId里面的是否都是uuid
  278. $channelInfo = [];
  279. foreach ($channelId as $key => $id) {
  280. $channelInfo[] = Channel::where('uid',$id)->first();
  281. }
  282. $tplRender = new TemplateRender($props,
  283. $channelInfo,
  284. $this->options['mode'],
  285. $this->options['format'],
  286. $this->options['studioId'],
  287. $this->options['debug'],
  288. $this->options['lang'],
  289. );
  290. $tplProps = $tplRender->render($tpl_name);
  291. if($this->options['format']==='react' && $tplProps){
  292. $props = $doc->createAttribute("props");
  293. $props->nodeValue = $tplProps['props'];
  294. $tpl->appendChild($props);
  295. $attTpl = $doc->createAttribute("tpl");
  296. $attTpl->nodeValue = $tplProps['tpl'];
  297. $tpl->appendChild($attTpl);
  298. $htmlElement = $doc->createElement($tplProps['tag']);
  299. $htmlElement->nodeValue=$tplProps['html'];
  300. $tpl->appendChild($htmlElement);
  301. }
  302. }
  303. $html = $doc->saveHTML();
  304. $html = str_replace(['<dfn','</dfn>'],['<MdTpl','</MdTpl>'],$html);
  305. switch ($this->options['format']) {
  306. case 'react':
  307. return trim($html);
  308. break;
  309. case 'unity':
  310. if($tplProps){
  311. return "{{"."{$tplProps['tpl']}|{$tplProps['props']}"."}}";
  312. }else{
  313. return '';
  314. }
  315. break;
  316. case 'html':
  317. if(isset($tplProps)){
  318. if(is_array($tplProps)){
  319. return '';
  320. }else{
  321. return $tplProps;
  322. }
  323. }else{
  324. Log::error('tplProps undefine');
  325. return '';
  326. }
  327. break;
  328. case 'text':
  329. case 'simple':
  330. if(isset($tplProps)){
  331. if(is_array($tplProps)){
  332. return '';
  333. }else{
  334. return $tplProps;
  335. }
  336. }else{
  337. Log::error('tplProps undefine');
  338. return '';
  339. }
  340. break;
  341. case 'tex':
  342. if(isset($tplProps)){
  343. if(is_array($tplProps)){
  344. return '';
  345. }else{
  346. return $tplProps;
  347. }
  348. }else{
  349. Log::error('tplProps undefine');
  350. return '';
  351. }
  352. break;
  353. default:
  354. return '';
  355. break;
  356. }
  357. }
  358. private function markdown2wiki(string $markdown): string{
  359. //$markdown = mb_convert_encoding($markdown,'UTF-8','UTF-8');
  360. $markdown = iconv('UTF-8','UTF-8//IGNORE',$markdown);
  361. /**
  362. * nissaya
  363. * aaa=bbb\n
  364. * {{nissaya|aaa|bbb}}
  365. */
  366. if($this->options['channelType']==='nissaya'){
  367. if($this->options['contentType'] === "json"){
  368. $json = json_decode($markdown);
  369. $nissayaWord = [];
  370. if(is_array($json)){
  371. foreach ($json as $word) {
  372. if(count($word->sn) === 1){
  373. //只输出第一层级
  374. $str = "{{nissaya|";
  375. if(isset($word->word->value)){
  376. $str .= $word->word->value;
  377. }
  378. $str .= "|";
  379. if(isset($word->meaning->value)){
  380. $str .= $word->meaning->value;
  381. }
  382. $str .= "}}";
  383. $nissayaWord[] = $str;
  384. }
  385. }
  386. }else{
  387. Log::error('json data is not array',['data'=>$markdown]);
  388. }
  389. $markdown = implode('',$nissayaWord);
  390. }else if($this->options['contentType'] === "markdown"){
  391. $lines = explode("\n",$markdown);
  392. $newLines = array();
  393. foreach ($lines as $line) {
  394. if(strstr($line,'=') === FALSE){
  395. $newLines[] = $line;
  396. }else{
  397. $nissaya = explode('=',$line);
  398. $meaning = array_slice($nissaya,1);
  399. $meaning = implode('=',$meaning);
  400. $newLines[] = "{{nissaya|{$nissaya[0]}|{$meaning}}}";
  401. }
  402. }
  403. $markdown = implode("\n",$newLines);
  404. }
  405. }
  406. //$markdown = preg_replace("/\n\n/","<div></div>",$markdown);
  407. /**
  408. * 处理 mermaid
  409. */
  410. if(strpos($markdown,"```mermaid") !== false){
  411. $lines = explode("\n",$markdown);
  412. $newLines = array();
  413. $mermaidBegin = false;
  414. $mermaidString = array();
  415. foreach ($lines as $line) {
  416. if($line === "```mermaid"){
  417. $mermaidBegin = true;
  418. $mermaidString = [];
  419. continue;
  420. }
  421. if($mermaidBegin){
  422. if($line === "```"){
  423. $newLines[] = "{{mermaid|".base64_encode(\json_encode($mermaidString))."}}";
  424. $mermaidBegin = false;
  425. }else{
  426. $mermaidString[] = $line;
  427. }
  428. }else{
  429. $newLines[] = $line;
  430. }
  431. }
  432. $markdown = implode("\n",$newLines);
  433. }
  434. /**
  435. * 替换换行符
  436. * react 无法处理 <br> 替换为<div></div>代替换行符作用
  437. */
  438. //$markdown = str_replace('<br>','<div></div>',$markdown);
  439. /**
  440. * markdown -> html
  441. */
  442. /*
  443. $html = MdRender::fixHtml($html);
  444. */
  445. #替换术语
  446. $pattern = "/\[\[(.+?)\]\]/";
  447. $replacement = '{{term|$1}}';
  448. $markdown = preg_replace($pattern,$replacement,$markdown);
  449. #替换句子模版
  450. $pattern = "/\{\{([0-9].+?)\}\}/";
  451. $replacement = '{{sent|$1}}';
  452. $markdown = preg_replace($pattern,$replacement,$markdown);
  453. /**
  454. * 替换多行注释
  455. * ```
  456. * bla
  457. * bla
  458. * ```
  459. * {{note|
  460. * bla
  461. * bla
  462. * }}
  463. */
  464. if(strpos($markdown,"```\n") !== false){
  465. $lines = explode("\n",$markdown);
  466. $newLines = array();
  467. $noteBegin = false;
  468. $noteString = array();
  469. foreach ($lines as $line) {
  470. if($noteBegin){
  471. if($line === "```"){
  472. $newLines[] = "}}";
  473. $noteBegin = false;
  474. }else{
  475. $newLines[] = $line;
  476. }
  477. }else{
  478. if($line === "```"){
  479. $noteBegin = true;
  480. $newLines[] = "{{note|";
  481. continue;
  482. }else{
  483. $newLines[] = $line;
  484. }
  485. }
  486. }
  487. if($noteBegin){
  488. $newLines[] = "}}";
  489. }
  490. $markdown = implode("\n",$newLines);
  491. }
  492. /**
  493. * 替换单行注释
  494. * `bla bla`
  495. * {{note|bla}}
  496. */
  497. $pattern = "/`(.+?)`/";
  498. $replacement = '{{note|$1}}';
  499. $markdown = preg_replace($pattern,$replacement,$markdown);
  500. return $markdown;
  501. }
  502. private function markdownToHtml($markdown){
  503. $markdown = str_replace('MdTpl','mdtpl',$markdown);
  504. $markdown = str_replace(['<param','</param>'],['<span','</span>'],$markdown);
  505. $html = Markdown::render($markdown);
  506. if($this->options['format']==='react'){
  507. $html = $this->fixHtml($html);
  508. }
  509. $html = str_replace('<hr>','<hr />',$html);
  510. //给H1-6 添加uuid
  511. for ($i=1; $i<7 ; $i++) {
  512. if(strpos($html,"<h{$i}>")===false){
  513. continue;
  514. }
  515. $output = array();
  516. $input = $html;
  517. $hPos = strpos($input,"<h{$i}>");
  518. while ($hPos !== false) {
  519. $output[] = substr($input,0,$hPos);
  520. $output[] = "<h{$i} id='".Str::uuid()."'>";
  521. $input = substr($input,$hPos+4);
  522. $hPos = strpos($input,"<h{$i}>");
  523. }
  524. $output[] = $input;
  525. $html = implode('',$output);
  526. }
  527. $html = str_replace('mdtpl','MdTpl',$html);
  528. return $html;
  529. }
  530. private function fixHtml($html) {
  531. $doc = new \DOMDocument();
  532. libxml_use_internal_errors(true);
  533. $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
  534. $doc->loadHTML('<span>'.$html.'</span>',LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  535. $fixed = $doc->saveHTML();
  536. $fixed = mb_convert_encoding($fixed, "UTF-8", 'HTML-ENTITIES');
  537. return $fixed;
  538. }
  539. public static function init(){
  540. $GLOBALS["MdRenderStack"] = 0;
  541. }
  542. public function convert($markdown,$channelId=[],$queryId=null){
  543. if(isset($GLOBALS["MdRenderStack"]) && is_numeric($GLOBALS["MdRenderStack"])){
  544. $GLOBALS["MdRenderStack"]++;
  545. }else{
  546. $GLOBALS["MdRenderStack"] = 1;
  547. }
  548. if($GLOBALS["MdRenderStack"]<3){
  549. $output = $this->_convert($markdown,$channelId,$queryId);
  550. }else{
  551. $output = $markdown;
  552. }
  553. $GLOBALS["MdRenderStack"]--;
  554. return $output;
  555. }
  556. private function _convert($markdown,$channelId=[],$queryId=null){
  557. if(empty($markdown)){
  558. switch ($this->options['format']) {
  559. case 'react':
  560. return "<span></span>";
  561. break;
  562. default:
  563. return "";
  564. break;
  565. }
  566. }
  567. $wiki = $this->markdown2wiki($markdown);
  568. $html = $this->wiki2xml($wiki,$channelId);
  569. if(!is_null($queryId)){
  570. $html = $this->xmlQueryId($html, $queryId);
  571. }
  572. $html = $this->markdownToHtml($html);
  573. //后期处理
  574. $output = '';
  575. switch ($this->options['format']) {
  576. case 'react':
  577. //生成可展开组件
  578. $html = str_replace("<div/>","<div></div>",$html);
  579. $pattern = '/<li><div>(.+?)<\/div><\/li>/';
  580. $replacement = '<li><MdTpl name="toggle" tpl="toggle" props=""><div>$1</div></MdTpl></li>';
  581. $output = preg_replace($pattern,$replacement,$html);
  582. break;
  583. case 'text':
  584. case 'simple':
  585. $html = strip_tags($html);
  586. $output = htmlspecialchars_decode($html,ENT_QUOTES);
  587. //$output = html_entity_decode($html);
  588. break;
  589. case 'tex':
  590. $html = strip_tags($html);
  591. $output = htmlspecialchars_decode($html,ENT_QUOTES);
  592. //$output = html_entity_decode($html);
  593. break;
  594. case 'unity':
  595. $html = str_replace(['<strong>','</strong>','<em>','</em>'],['[%b%]','[%/b%]','[%i%]','[%/i%]'],$html);
  596. $html = strip_tags($html);
  597. $html = str_replace(['[%b%]','[%/b%]','[%i%]','[%/i%]'],['<b>','</b>','<i>','</i>'],$html);
  598. $output = htmlspecialchars_decode($html,ENT_QUOTES);
  599. break;
  600. case 'html':
  601. $output = htmlspecialchars_decode($html,ENT_QUOTES);
  602. //处理脚注
  603. if($this->options['footnote'] && isset($GLOBALS['note']) && count($GLOBALS['note'])>0){
  604. $output .= '<div><h1>endnote</h1>';
  605. foreach ($GLOBALS['note'] as $footnote) {
  606. $output .= '<p><a name="footnote-'.$footnote['sn'].'">['.$footnote['sn'].']</a> '.$footnote['content'].'</p>';
  607. }
  608. $output .= '</div>';
  609. unset($GLOBALS['note']);
  610. }
  611. //处理图片链接
  612. $output = str_replace('<img src="','<img src="'.config('app.url'),$output);
  613. break;
  614. case 'markdown':
  615. $output = $markdownWithTpl;
  616. break;
  617. }
  618. return $output;
  619. }
  620. /**
  621. * string[] $channelId
  622. */
  623. public static function render($markdown,$channelId,$queryId=null,$mode='read',$channelType='translation',$contentType="markdown",$format='react'){
  624. $mdRender = new MdRender(
  625. [
  626. 'mode'=>$mode,
  627. 'channelType'=>$channelType,
  628. 'contentType'=>$contentType,
  629. 'format'=>$format
  630. ]);
  631. $output = $mdRender->convert($markdown,$channelId,$queryId);
  632. return $output;
  633. }
  634. }