MdRender.php 19 KB

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