MdRender.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. $tpl = str_replace("|\n","|",$tpl);
  91. $pattern = "/\{\{(.+?)\|/";
  92. $replacement = '<MdTpl name="$1"><param>';
  93. $tpl = preg_replace($pattern,$replacement,$tpl);
  94. $tpl = str_replace("}}","</param></MdTpl>",$tpl);
  95. $tpl = str_replace("|","</param><param>",$tpl);
  96. /**
  97. * 替换变量名
  98. */
  99. $pattern = "/<param>([a-z]+?)=/";
  100. $replacement = '<param name="$1">';
  101. $tpl = preg_replace($pattern,$replacement,$tpl);
  102. //tpl to react
  103. $tpl = MdRender::xml2tpl($tpl,$channelId,$mode);
  104. $buffer[] = $tpl;
  105. }
  106. $remain = $arrWiki['data'][2];
  107. } while (!empty($remain));
  108. $html = implode('' , $buffer);
  109. return $html;
  110. }
  111. public static function xmlQueryId(string $xml, string $id):string{
  112. try{
  113. $dom = simplexml_load_string($xml);
  114. }catch(\Exception $e){
  115. Log::error($e);
  116. return "<div></div>";
  117. }
  118. $tpl_list = $dom->xpath('//MdTpl');
  119. foreach ($tpl_list as $key => $tpl) {
  120. foreach ($tpl->children() as $param) {
  121. # 处理每个参数
  122. if($param->getName() === "param"){
  123. foreach($param->attributes() as $pa => $pa_value){
  124. $pValue = $pa_value->__toString();
  125. if($pa === "name" && $pValue === "id"){
  126. if($param->__toString() === $id){
  127. return $tpl->asXML();
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. return "<div></div>";
  135. }
  136. public static function take_sentence(string $xml):array{
  137. $output = [];
  138. try{
  139. $dom = simplexml_load_string($xml);
  140. }catch(\Exception $e){
  141. Log::error($e);
  142. return $output;
  143. }
  144. $tpl_list = $dom->xpath('//MdTpl');
  145. foreach ($tpl_list as $key => $tpl) {
  146. foreach($tpl->attributes() as $a => $a_value){
  147. if($a==="name"){
  148. if($a_value->__toString() ==="sent"){
  149. foreach ($tpl->children() as $param) {
  150. # 处理每个参数
  151. if($param->getName() === "param"){
  152. $sent = $param->__toString();
  153. if(!empty($sent)){
  154. $output[] = $sent;
  155. break;
  156. }
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. return $output;
  164. }
  165. public static function xml2tpl(string $xml, $channelId=[],$mode='read'):string{
  166. /**
  167. * 解析xml
  168. * 获取模版参数
  169. * 生成react 组件参数
  170. */
  171. try{
  172. $dom = simplexml_load_string($xml);
  173. }catch(\Exception $e){
  174. Log::error($e);
  175. Log::error($xml);
  176. return "<span>xml解析错误{$e}</span>";
  177. }
  178. if(!$dom){
  179. Log::error($xml);
  180. return "<span>xml解析错误</span>";
  181. }
  182. /*
  183. $doc = new \DOMDocument();
  184. $xml = str_replace('MdTpl','dfn',$xml);
  185. $ok = $doc->loadHTML($xml,LIBXML_HTML_NODEFDTD | LIBXML_DTDVALID);
  186. if(!$ok){
  187. return "<span>xml解析错误</span>";
  188. }
  189. */
  190. $tpl_list = $dom->xpath('//MdTpl');
  191. foreach ($tpl_list as $key => $tpl) {
  192. /**
  193. * 遍历 MdTpl 处理参数
  194. */
  195. $props = [];
  196. $tpl_name = '';
  197. foreach($tpl->attributes() as $a => $a_value){
  198. if($a==="name"){
  199. $tpl_name = $a_value;
  200. }
  201. }
  202. $param_id = 0;
  203. foreach ($tpl->children() as $param) {
  204. # 处理每个参数
  205. if($param->getName() === "param"){
  206. $param_id++;
  207. $paramName = "";
  208. foreach($param->attributes() as $pa => $pa_value){
  209. if($pa === "name"){
  210. $props["{$pa_value}"] = $param->__toString();
  211. $paramName = $pa_value;
  212. }
  213. }
  214. if(empty($paramName)){
  215. $props["{$param_id}"] = $param->__toString();
  216. }
  217. }
  218. }
  219. /**
  220. * 生成模版参数
  221. *
  222. */
  223. //TODO 判断$channelId里面的是否都是uuid
  224. $channelInfo = Channel::whereIn('uid',$channelId)->get();
  225. $tplRender = new TemplateRender($props,$channelInfo,$mode);
  226. $tplProps = $tplRender->render($tpl_name);
  227. if($tplProps){
  228. $tpl->addAttribute("props",$tplProps['props']);
  229. $tpl->addAttribute("tpl",$tplProps['tpl']);
  230. $tpl->addChild($tplProps['tag'],$tplProps['html']);
  231. }
  232. }
  233. $html = str_replace('<?xml version="1.0"?>','',$dom->asXML()) ;
  234. $html = str_replace(['<xml>','</xml>'],['<span>','</span>'],$html);
  235. return trim($html);
  236. }
  237. public static function render2($markdown,$channelId=[],$queryId=null,$mode='read',$channelType,$contentType="markdown"){
  238. if(empty($markdown)){
  239. return "<span></span>";
  240. }
  241. $wiki = MdRender::markdown2wiki($markdown,$channelType,$contentType);
  242. $html = MdRender::wiki2xml($wiki,$channelId,$mode);
  243. if(!is_null($queryId)){
  244. $html = MdRender::xmlQueryId($html, $queryId);
  245. }
  246. $html = MdRender::markdownToHtml($html);
  247. //$tpl = MdRender::xml2tpl($html,$channelId,$mode);
  248. //生成可展开组件
  249. $html = str_replace("<div/>","<div></div>",$html);
  250. $pattern = '/<li><div>(.+?)<\/div><\/li>/';
  251. $replacement = '<li><MdTpl name="toggle" tpl="toggle" props=""><div>$1</div></MdTpl></li>';
  252. $html = preg_replace($pattern,$replacement,$html);
  253. return $html;
  254. }
  255. public static function markdown2wiki(string $markdown,$channelType=null,$contentType=null): string{
  256. //$markdown = mb_convert_encoding($markdown,'UTF-8','UTF-8');
  257. $markdown = iconv('UTF-8','UTF-8//IGNORE',$markdown);
  258. /**
  259. * nissaya
  260. * aaa=bbb\n
  261. * {{nissaya|aaa|bbb}}
  262. */
  263. if($channelType==='nissaya'){
  264. if($contentType === "json"){
  265. $json = json_decode($markdown);
  266. $nissayaWord = [];
  267. foreach ($json as $word) {
  268. if(count($word->sn) === 1){
  269. //只输出第一层级
  270. $str = "{{nissaya|";
  271. if(isset($word->word->value)){
  272. $str .= $word->word->value;
  273. }
  274. $str .= "|";
  275. if(isset($word->meaning->value)){
  276. $str .= $word->meaning->value;
  277. }
  278. $str .= "}}";
  279. $nissayaWord[] = $str;
  280. }
  281. }
  282. $markdown = implode('',$nissayaWord);
  283. }else if($contentType === "markdown"){
  284. $lines = explode("\n",$markdown);
  285. $newLines = array();
  286. foreach ($lines as $line) {
  287. if(strstr($line,'=') === FALSE){
  288. $newLines[] = $line;
  289. }else{
  290. $nissaya = explode('=',$line);
  291. $meaning = array_slice($nissaya,1);
  292. $meaning = implode('=',$meaning);
  293. $newLines[] = "{{nissaya|{$nissaya[0]}|{$meaning}}}";
  294. }
  295. }
  296. $markdown = implode("\n",$newLines);
  297. }
  298. }
  299. //$markdown = preg_replace("/\n\n/","<div></div>",$markdown);
  300. /**
  301. * 处理 mermaid
  302. */
  303. if(strpos($markdown,"```mermaid") !== false){
  304. $lines = explode("\n",$markdown);
  305. $newLines = array();
  306. $mermaidBegin = false;
  307. $mermaidString = array();
  308. foreach ($lines as $line) {
  309. if($line === "```mermaid"){
  310. $mermaidBegin = true;
  311. $mermaidString = [];
  312. continue;
  313. }
  314. if($mermaidBegin){
  315. if($line === "```"){
  316. $newLines[] = "{{mermaid|".base64_encode(\json_encode($mermaidString))."}}";
  317. $mermaidBegin = false;
  318. }else{
  319. $mermaidString[] = $line;
  320. }
  321. }else{
  322. $newLines[] = $line;
  323. }
  324. }
  325. $markdown = implode("\n",$newLines);
  326. }
  327. /**
  328. * 替换换行符
  329. * react 无法处理 <br> 替换为<div></div>代替换行符作用
  330. */
  331. $markdown = str_replace('<br>','<div></div>',$markdown);
  332. /**
  333. * markdown -> html
  334. */
  335. /*
  336. $html = MdRender::fixHtml($html);
  337. */
  338. #替换术语
  339. $pattern = "/\[\[(.+?)\]\]/";
  340. $replacement = '{{term|$1}}';
  341. $markdown = preg_replace($pattern,$replacement,$markdown);
  342. #替换句子模版
  343. $pattern = "/\{\{([0-9].+?)\}\}/";
  344. $replacement = '{{sent|$1}}';
  345. $markdown = preg_replace($pattern,$replacement,$markdown);
  346. /**
  347. * 替换多行注释
  348. * ```
  349. * bla
  350. * bla
  351. * ```
  352. * {{note|
  353. * bla
  354. * bla
  355. * }}
  356. */
  357. if(strpos($markdown,"```\n") !== false){
  358. $lines = explode("\n",$markdown);
  359. $newLines = array();
  360. $noteBegin = false;
  361. $noteString = array();
  362. foreach ($lines as $line) {
  363. if($noteBegin){
  364. if($line === "```"){
  365. $newLines[] = "}}";
  366. $noteBegin = false;
  367. }else{
  368. $newLines[] = $line;
  369. }
  370. }else{
  371. if($line === "```"){
  372. $noteBegin = true;
  373. $newLines[] = "{{note|";
  374. continue;
  375. }else{
  376. $newLines[] = $line;
  377. }
  378. }
  379. }
  380. if($noteBegin){
  381. $newLines[] = "}}";
  382. }
  383. $markdown = implode("\n",$newLines);
  384. }
  385. /**
  386. * 替换单行注释
  387. * `bla bla`
  388. * {{note|bla}}
  389. */
  390. $pattern = "/`(.+?)`/";
  391. $replacement = '{{note|$1}}';
  392. $markdown = preg_replace($pattern,$replacement,$markdown);
  393. /*
  394. #替换多行注释
  395. #<pre><code>bla</code></pre>
  396. #{{note|bla}}
  397. $pattern = '/<pre><code>([\w\W]+?)<\/code><\/pre>/';
  398. $replacement = '{{note|$1}}';
  399. $html = preg_replace($pattern,$replacement,$html);
  400. #替换单行注释
  401. #<code>bla</code>
  402. #{{note|bla}}
  403. $pattern = '/<code>(.+?)<\/code>/';
  404. $replacement = '{{note|$1}}';
  405. $html = preg_replace($pattern,$replacement,$html);
  406. */
  407. return $markdown;
  408. }
  409. public static function markdownToHtml($markdown){
  410. $markdown = str_replace('MdTpl','mdtpl',$markdown);
  411. $markdown = str_replace(['<param','</param>'],['<span','</span>'],$markdown);
  412. $Parsedown = new \Parsedown();
  413. $html = $Parsedown->text($markdown);
  414. $html = MdRender::fixHtml($html);
  415. $html = str_replace('<hr>','<hr />',$html);
  416. //给H1-6 添加uuid
  417. for ($i=1; $i<7 ; $i++) {
  418. if(strpos($html,"<h{$i}>")===false){
  419. continue;
  420. }
  421. $output = array();
  422. $input = $html;
  423. $hPos = strpos($input,"<h{$i}>");
  424. while ($hPos !== false) {
  425. $output[] = substr($input,0,$hPos);
  426. $output[] = "<h{$i} id='".Str::uuid()."'>";
  427. $input = substr($input,$hPos+4);
  428. $hPos = strpos($input,"<h{$i}>");
  429. }
  430. $output[] = $input;
  431. $html = implode('',$output);
  432. }
  433. $html = str_replace('mdtpl','MdTpl',$html);
  434. return $html;
  435. }
  436. /**
  437. * string[] $channelId
  438. */
  439. public static function render($markdown,$channelId,$queryId=null,$mode='read',$channelType='translation',$contentType="markdown"){
  440. return MdRender::render2($markdown,$channelId,$queryId,$mode,$channelType,$contentType);
  441. }
  442. public static function fixHtml($html) {
  443. $doc = new \DOMDocument();
  444. libxml_use_internal_errors(true);
  445. $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
  446. $doc->loadHTML('<span>'.$html.'</span>',LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  447. $fixed = $doc->saveHTML();
  448. $fixed = mb_convert_encoding($fixed, "UTF-8", 'HTML-ENTITIES');
  449. return $fixed;
  450. }
  451. }