MdRender.php 20 KB

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