MdRender.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. define("STACK_DEEP",8);
  12. class MdRender{
  13. /**
  14. * 按照{{}}把字符串切分成三个部分。模版之前的,模版,和模版之后的
  15. */
  16. public static function tplSplit($tpl){
  17. $before = strpos($tpl,'{{');
  18. if($before === FALSE){
  19. //未找到
  20. return ['data'=>[$tpl,'',''],'error'=>0];
  21. }else{
  22. $pointer = $before;
  23. $stack = array();
  24. $stack[] = $pointer;
  25. $after = substr($tpl,$pointer+2) ;
  26. while (!empty($after) && count($stack)>0 && count($stack)<STACK_DEEP) {
  27. $nextBegin = strpos($after,"{{");
  28. $nextEnd = strpos($after,"}}");
  29. if($nextBegin !== FALSE){
  30. if($nextBegin < $nextEnd){
  31. //有嵌套找到最后一个}}
  32. $pointer = $pointer + 2 + $nextBegin;
  33. $stack[] = $pointer;
  34. $after = substr($tpl,$pointer+2);
  35. }else if($nextEnd !== FALSE){
  36. //无嵌套有结束
  37. $pointer = $pointer + 2 + $nextEnd;
  38. array_pop($stack);
  39. $after = substr($tpl,$pointer+2);
  40. }else{
  41. //无结束符 没找到
  42. break;
  43. }
  44. }else if($nextEnd !== FALSE){
  45. $pointer = $pointer + 2 + $nextEnd;
  46. array_pop($stack);
  47. $after = substr($tpl,$pointer+2);
  48. }else{
  49. //没找到
  50. break;
  51. }
  52. }
  53. if(count($stack)>0){
  54. if(count($stack) === STACK_DEEP){
  55. return ['data'=>[$tpl,'',''],'error'=>2];
  56. }else{
  57. //未关闭
  58. return ['data'=>[$tpl,'',''],'error'=>1];
  59. }
  60. }else{
  61. return ['data'=>
  62. [
  63. substr($tpl,0,$before),
  64. substr($tpl,$before,$pointer-$before+2),
  65. substr($tpl,$pointer+2)
  66. ],
  67. 'error'=>0
  68. ];
  69. }
  70. }
  71. }
  72. public static function wiki2xml(string $wiki,$channelId=[],$mode='read'):string{
  73. /**
  74. * 替换{{}} 到xml之前 要先把换行符号去掉
  75. */
  76. //$wiki = str_replace("\n","",$wiki);
  77. /**
  78. * 把模版转换为xml
  79. */
  80. $remain = $wiki;
  81. $buffer = array();
  82. do {
  83. $arrWiki = MdRender::tplSplit($remain);
  84. $buffer[] = $arrWiki['data'][0];
  85. $tpl = $arrWiki['data'][1];
  86. if(!empty($tpl)){
  87. /**
  88. * 处理模版 提取参数
  89. */
  90. $pattern = "/\{\{(.+?)\|/";
  91. $replacement = '<MdTpl name="$1"><param>';
  92. $tpl = preg_replace($pattern,$replacement,$tpl);
  93. $tpl = str_replace("}}","</param></MdTpl>",$tpl);
  94. $tpl = str_replace("|","</param><param>",$tpl);
  95. /**
  96. * 替换变量名
  97. */
  98. $pattern = "/<param>([a-z]+?)=/";
  99. $replacement = '<param name="$1">';
  100. $tpl = preg_replace($pattern,$replacement,$tpl);
  101. //tpl to react
  102. $tpl = MdRender::xml2tpl($tpl,$channelId,$mode);
  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'):string{
  165. /**
  166. * 解析xml
  167. * 获取模版参数
  168. * 生成react 组件参数
  169. */
  170. try{
  171. $dom = simplexml_load_string($xml);
  172. }catch(\Exception $e){
  173. Log::error($e);
  174. Log::error($xml);
  175. return "<span>xml解析错误{$e}</span>";
  176. }
  177. if(!$dom){
  178. Log::error($xml);
  179. return "<span>xml解析错误</span>";
  180. }
  181. /*
  182. $doc = new \DOMDocument();
  183. $xml = str_replace('MdTpl','dfn',$xml);
  184. $ok = $doc->loadHTML($xml,LIBXML_HTML_NODEFDTD | LIBXML_DTDVALID);
  185. if(!$ok){
  186. return "<span>xml解析错误</span>";
  187. }
  188. */
  189. $tpl_list = $dom->xpath('//MdTpl');
  190. foreach ($tpl_list as $key => $tpl) {
  191. /**
  192. * 遍历 MdTpl 处理参数
  193. */
  194. $props = [];
  195. $tpl_name = '';
  196. foreach($tpl->attributes() as $a => $a_value){
  197. if($a==="name"){
  198. $tpl_name = $a_value;
  199. }
  200. }
  201. $param_id = 0;
  202. foreach ($tpl->children() as $param) {
  203. # 处理每个参数
  204. if($param->getName() === "param"){
  205. $param_id++;
  206. $paramName = "";
  207. foreach($param->attributes() as $pa => $pa_value){
  208. if($pa === "name"){
  209. $props["{$pa_value}"] = $param->__toString();
  210. $paramName = $pa_value;
  211. }
  212. }
  213. if(empty($paramName)){
  214. $props["{$param_id}"] = $param->__toString();
  215. }
  216. }
  217. }
  218. /**
  219. * 生成模版参数
  220. */
  221. $channelInfo = Channel::whereIn('uid',$channelId)->get();
  222. $tplRender = new TemplateRender($props,$channelInfo,$mode);
  223. $tplProps = $tplRender->render($tpl_name);
  224. if($tplProps){
  225. $tpl->addAttribute("props",$tplProps['props']);
  226. $tpl->addAttribute("tpl",$tplProps['tpl']);
  227. $tpl->addChild($tplProps['tag'],$tplProps['html']);
  228. }
  229. }
  230. $html = str_replace('<?xml version="1.0"?>','',$dom->asXML()) ;
  231. $html = str_replace(['<xml>','</xml>'],['<span>','</span>'],$html);
  232. return trim($html);
  233. }
  234. public static function render2($markdown,$channelId=[],$queryId=null,$mode='read',$channelType,$contentType="markdown"){
  235. if(empty($markdown)){
  236. return "<span></span>";
  237. }
  238. $wiki = MdRender::markdown2wiki($markdown,$channelType,$contentType);
  239. $html = MdRender::wiki2xml($wiki,$channelId,$mode);
  240. if(!is_null($queryId)){
  241. $html = MdRender::xmlQueryId($html, $queryId);
  242. }
  243. $html = MdRender::markdownToHtml($html);
  244. //$tpl = MdRender::xml2tpl($html,$channelId,$mode);
  245. //生成可展开组件
  246. $html = str_replace("<div/>","<div></div>",$html);
  247. $pattern = '/<li><div>(.+?)<\/div><\/li>/';
  248. $replacement = '<li><MdTpl name="toggle" tpl="toggle" props=""><div>$1</div></MdTpl></li>';
  249. $html = preg_replace($pattern,$replacement,$html);
  250. return $html;
  251. }
  252. public static function markdown2wiki(string $markdown,$channelType=null,$contentType=null): string{
  253. //$markdown = mb_convert_encoding($markdown,'UTF-8','UTF-8');
  254. $markdown = iconv('UTF-8','UTF-8//IGNORE',$markdown);
  255. /**
  256. * nissaya
  257. * aaa=bbb\n
  258. * {{nissaya|aaa|bbb}}
  259. */
  260. if($channelType==='nissaya'){
  261. if($contentType === "json"){
  262. $json = json_decode($markdown);
  263. $nissayaWord = [];
  264. foreach ($json as $word) {
  265. if(count($word->sn) === 1){
  266. //只输出第一层级
  267. $str = "{{nissaya|";
  268. if(isset($word->word->value)){
  269. $str .= $word->word->value;
  270. }
  271. $str .= "|";
  272. if(isset($word->meaning->value)){
  273. $str .= $word->meaning->value;
  274. }
  275. $str .= "}}";
  276. $nissayaWord[] = $str;
  277. }
  278. }
  279. $markdown = implode('',$nissayaWord);
  280. }else if($contentType === "markdown"){
  281. $lines = explode("\n",$markdown);
  282. $newLines = array();
  283. foreach ($lines as $line) {
  284. if(strstr($line,'=') === FALSE){
  285. $newLines[] = $line;
  286. }else{
  287. $nissaya = explode('=',$line);
  288. $meaning = array_slice($nissaya,1);
  289. $meaning = implode('=',$meaning);
  290. $newLines[] = "{{nissaya|{$nissaya[0]}|{$meaning}}}";
  291. }
  292. }
  293. $markdown = implode("\n",$newLines);
  294. }
  295. }
  296. //$markdown = preg_replace("/\n\n/","<div></div>",$markdown);
  297. /**
  298. * 处理 mermaid
  299. */
  300. if(strpos($markdown,"```mermaid") !== false){
  301. $lines = explode("\n",$markdown);
  302. $newLines = array();
  303. $mermaidBegin = false;
  304. $mermaidString = array();
  305. foreach ($lines as $line) {
  306. if($line === "```mermaid"){
  307. $mermaidBegin = true;
  308. $mermaidString = [];
  309. continue;
  310. }
  311. if($mermaidBegin){
  312. if($line === "```"){
  313. $newLines[] = "{{mermaid|".base64_encode(\json_encode($mermaidString))."}}";
  314. $mermaidBegin = false;
  315. }else{
  316. $mermaidString[] = $line;
  317. }
  318. }else{
  319. $newLines[] = $line;
  320. }
  321. }
  322. $markdown = implode("\n",$newLines);
  323. }
  324. /**
  325. * 替换换行符
  326. * react 无法处理 <br> 替换为<div></div>代替换行符作用
  327. */
  328. $markdown = str_replace('<br>','<div></div>',$markdown);
  329. /**
  330. * markdown -> html
  331. */
  332. Log::info('markdown -> html');
  333. /*
  334. $markdown = str_replace(['[[',']]'],['㐛','㐚'],$markdown);
  335. $html = Str::markdown($markdown);
  336. $html = str_replace(['㐛','㐚'],['[[',']]'],$html);
  337. $html = MdRender::fixHtml($html);
  338. //给H1-6 添加uuid
  339. for ($i=1; $i<7 ; $i++) {
  340. if(strpos($html,"<h{$i}>")===false){
  341. continue;
  342. }
  343. $output = array();
  344. $input = $html;
  345. $hPos = strpos($input,"<h{$i}>");
  346. while ($hPos !== false) {
  347. $output[] = substr($input,0,$hPos);
  348. $output[] = "<h{$i} id='".Str::uuid()."'>";
  349. $input = substr($input,$hPos+4);
  350. $hPos = strpos($input,"<h{$i}>");
  351. }
  352. $output[] = $input;
  353. $html = implode('',$output);
  354. }
  355. */
  356. #替换术语
  357. $pattern = "/\[\[(.+?)\]\]/";
  358. $replacement = '{{term|$1}}';
  359. $markdown = preg_replace($pattern,$replacement,$markdown);
  360. #替换句子模版
  361. $pattern = "/\{\{([0-9].+?)\}\}/";
  362. $replacement = '{{sent|$1}}';
  363. $markdown = preg_replace($pattern,$replacement,$markdown);
  364. /**
  365. * 替换多行注释
  366. * ```
  367. * bla
  368. * bla
  369. * ```
  370. * {{note|
  371. * bla
  372. * bla
  373. * }}
  374. */
  375. if(strpos($markdown,"```\n") !== false){
  376. $lines = explode("\n",$markdown);
  377. $newLines = array();
  378. $noteBegin = false;
  379. $noteString = array();
  380. foreach ($lines as $line) {
  381. if($noteBegin){
  382. if($line === "```"){
  383. $newLines[] = "}}";
  384. $noteBegin = false;
  385. }else{
  386. $newLines[] = $line;
  387. }
  388. }else{
  389. if($line === "```"){
  390. $noteBegin = true;
  391. $newLines[] = "{{note|";
  392. continue;
  393. }else{
  394. $newLines[] = $line;
  395. }
  396. }
  397. }
  398. if($noteBegin){
  399. $newLines[] = "}}";
  400. }
  401. $markdown = implode("\n",$newLines);
  402. }
  403. /**
  404. * 替换单行注释
  405. * `bla bla`
  406. * {{note|bla}}
  407. */
  408. $pattern = "/`(.+?)`/";
  409. $replacement = '{{note|$1}}';
  410. $markdown = preg_replace($pattern,$replacement,$markdown);
  411. /*
  412. #替换多行注释
  413. #<pre><code>bla</code></pre>
  414. #{{note|bla}}
  415. $pattern = '/<pre><code>([\w\W]+?)<\/code><\/pre>/';
  416. $replacement = '{{note|$1}}';
  417. $html = preg_replace($pattern,$replacement,$html);
  418. #替换单行注释
  419. #<code>bla</code>
  420. #{{note|bla}}
  421. $pattern = '/<code>(.+?)<\/code>/';
  422. $replacement = '{{note|$1}}';
  423. $html = preg_replace($pattern,$replacement,$html);
  424. */
  425. return $markdown;
  426. }
  427. public static function markdownToHtml($markdown){
  428. $markdown = str_replace('MdTpl','mdtpl',$markdown);
  429. $markdown = str_replace(['<param>','</param>'],['<span>','</span>'],$markdown);
  430. $html = Str::markdown($markdown);
  431. $html = MdRender::fixHtml($html);
  432. $html = str_replace('<hr>','<hr />',$html);
  433. //给H1-6 添加uuid
  434. for ($i=1; $i<7 ; $i++) {
  435. if(strpos($html,"<h{$i}>")===false){
  436. continue;
  437. }
  438. $output = array();
  439. $input = $html;
  440. $hPos = strpos($input,"<h{$i}>");
  441. while ($hPos !== false) {
  442. $output[] = substr($input,0,$hPos);
  443. $output[] = "<h{$i} id='".Str::uuid()."'>";
  444. $input = substr($input,$hPos+4);
  445. $hPos = strpos($input,"<h{$i}>");
  446. }
  447. $output[] = $input;
  448. $html = implode('',$output);
  449. }
  450. $html = str_replace('mdtpl','MdTpl',$html);
  451. return $html;
  452. }
  453. /**
  454. * string[] $channelId
  455. */
  456. public static function render($markdown,$channelId,$queryId=null,$mode='read',$channelType='translation',$contentType="markdown"){
  457. return MdRender::render2($markdown,$channelId,$queryId,$mode,$channelType,$contentType);
  458. }
  459. public static function fixHtml($html) {
  460. $doc = new \DOMDocument();
  461. libxml_use_internal_errors(true);
  462. $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
  463. $doc->loadHTML('<span>'.$html.'</span>',LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  464. $fixed = $doc->saveHTML();
  465. $fixed = mb_convert_encoding($fixed, "UTF-8", 'HTML-ENTITIES');
  466. return $fixed;
  467. }
  468. }