MdRender.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. $channelInfo = Channel::whereIn('uid',$channelId)->get();
  223. $tplRender = new TemplateRender($props,$channelInfo,$mode);
  224. $tplProps = $tplRender->render($tpl_name);
  225. if($tplProps){
  226. $tpl->addAttribute("props",$tplProps['props']);
  227. $tpl->addAttribute("tpl",$tplProps['tpl']);
  228. $tpl->addChild($tplProps['tag'],$tplProps['html']);
  229. }
  230. }
  231. $html = str_replace('<?xml version="1.0"?>','',$dom->asXML()) ;
  232. $html = str_replace(['<xml>','</xml>'],['<span>','</span>'],$html);
  233. return trim($html);
  234. }
  235. public static function render2($markdown,$channelId=[],$queryId=null,$mode='read',$channelType,$contentType="markdown"){
  236. if(empty($markdown)){
  237. return "<span></span>";
  238. }
  239. $wiki = MdRender::markdown2wiki($markdown,$channelType,$contentType);
  240. $html = MdRender::wiki2xml($wiki,$channelId,$mode);
  241. if(!is_null($queryId)){
  242. $html = MdRender::xmlQueryId($html, $queryId);
  243. }
  244. $html = MdRender::markdownToHtml($html);
  245. //$tpl = MdRender::xml2tpl($html,$channelId,$mode);
  246. //生成可展开组件
  247. $html = str_replace("<div/>","<div></div>",$html);
  248. $pattern = '/<li><div>(.+?)<\/div><\/li>/';
  249. $replacement = '<li><MdTpl name="toggle" tpl="toggle" props=""><div>$1</div></MdTpl></li>';
  250. $html = preg_replace($pattern,$replacement,$html);
  251. return $html;
  252. }
  253. public static function markdown2wiki(string $markdown,$channelType=null,$contentType=null): string{
  254. //$markdown = mb_convert_encoding($markdown,'UTF-8','UTF-8');
  255. $markdown = iconv('UTF-8','UTF-8//IGNORE',$markdown);
  256. /**
  257. * nissaya
  258. * aaa=bbb\n
  259. * {{nissaya|aaa|bbb}}
  260. */
  261. if($channelType==='nissaya'){
  262. if($contentType === "json"){
  263. $json = json_decode($markdown);
  264. $nissayaWord = [];
  265. foreach ($json as $word) {
  266. if(count($word->sn) === 1){
  267. //只输出第一层级
  268. $str = "{{nissaya|";
  269. if(isset($word->word->value)){
  270. $str .= $word->word->value;
  271. }
  272. $str .= "|";
  273. if(isset($word->meaning->value)){
  274. $str .= $word->meaning->value;
  275. }
  276. $str .= "}}";
  277. $nissayaWord[] = $str;
  278. }
  279. }
  280. $markdown = implode('',$nissayaWord);
  281. }else if($contentType === "markdown"){
  282. $lines = explode("\n",$markdown);
  283. $newLines = array();
  284. foreach ($lines as $line) {
  285. if(strstr($line,'=') === FALSE){
  286. $newLines[] = $line;
  287. }else{
  288. $nissaya = explode('=',$line);
  289. $meaning = array_slice($nissaya,1);
  290. $meaning = implode('=',$meaning);
  291. $newLines[] = "{{nissaya|{$nissaya[0]}|{$meaning}}}";
  292. }
  293. }
  294. $markdown = implode("\n",$newLines);
  295. }
  296. }
  297. //$markdown = preg_replace("/\n\n/","<div></div>",$markdown);
  298. /**
  299. * 处理 mermaid
  300. */
  301. if(strpos($markdown,"```mermaid") !== false){
  302. $lines = explode("\n",$markdown);
  303. $newLines = array();
  304. $mermaidBegin = false;
  305. $mermaidString = array();
  306. foreach ($lines as $line) {
  307. if($line === "```mermaid"){
  308. $mermaidBegin = true;
  309. $mermaidString = [];
  310. continue;
  311. }
  312. if($mermaidBegin){
  313. if($line === "```"){
  314. $newLines[] = "{{mermaid|".base64_encode(\json_encode($mermaidString))."}}";
  315. $mermaidBegin = false;
  316. }else{
  317. $mermaidString[] = $line;
  318. }
  319. }else{
  320. $newLines[] = $line;
  321. }
  322. }
  323. $markdown = implode("\n",$newLines);
  324. }
  325. /**
  326. * 替换换行符
  327. * react 无法处理 <br> 替换为<div></div>代替换行符作用
  328. */
  329. $markdown = str_replace('<br>','<div></div>',$markdown);
  330. /**
  331. * markdown -> html
  332. */
  333. /*
  334. $html = MdRender::fixHtml($html);
  335. */
  336. #替换术语
  337. $pattern = "/\[\[(.+?)\]\]/";
  338. $replacement = '{{term|$1}}';
  339. $markdown = preg_replace($pattern,$replacement,$markdown);
  340. #替换句子模版
  341. $pattern = "/\{\{([0-9].+?)\}\}/";
  342. $replacement = '{{sent|$1}}';
  343. $markdown = preg_replace($pattern,$replacement,$markdown);
  344. /**
  345. * 替换多行注释
  346. * ```
  347. * bla
  348. * bla
  349. * ```
  350. * {{note|
  351. * bla
  352. * bla
  353. * }}
  354. */
  355. if(strpos($markdown,"```\n") !== false){
  356. $lines = explode("\n",$markdown);
  357. $newLines = array();
  358. $noteBegin = false;
  359. $noteString = array();
  360. foreach ($lines as $line) {
  361. if($noteBegin){
  362. if($line === "```"){
  363. $newLines[] = "}}";
  364. $noteBegin = false;
  365. }else{
  366. $newLines[] = $line;
  367. }
  368. }else{
  369. if($line === "```"){
  370. $noteBegin = true;
  371. $newLines[] = "{{note|";
  372. continue;
  373. }else{
  374. $newLines[] = $line;
  375. }
  376. }
  377. }
  378. if($noteBegin){
  379. $newLines[] = "}}";
  380. }
  381. $markdown = implode("\n",$newLines);
  382. }
  383. /**
  384. * 替换单行注释
  385. * `bla bla`
  386. * {{note|bla}}
  387. */
  388. $pattern = "/`(.+?)`/";
  389. $replacement = '{{note|$1}}';
  390. $markdown = preg_replace($pattern,$replacement,$markdown);
  391. /*
  392. #替换多行注释
  393. #<pre><code>bla</code></pre>
  394. #{{note|bla}}
  395. $pattern = '/<pre><code>([\w\W]+?)<\/code><\/pre>/';
  396. $replacement = '{{note|$1}}';
  397. $html = preg_replace($pattern,$replacement,$html);
  398. #替换单行注释
  399. #<code>bla</code>
  400. #{{note|bla}}
  401. $pattern = '/<code>(.+?)<\/code>/';
  402. $replacement = '{{note|$1}}';
  403. $html = preg_replace($pattern,$replacement,$html);
  404. */
  405. return $markdown;
  406. }
  407. public static function markdownToHtml($markdown){
  408. $markdown = str_replace('MdTpl','mdtpl',$markdown);
  409. $markdown = str_replace(['<param','</param>'],['<span','</span>'],$markdown);
  410. $Parsedown = new \Parsedown();
  411. $html = $Parsedown->text($markdown);
  412. Log::info($html);
  413. $html = MdRender::fixHtml($html);
  414. $html = str_replace('<hr>','<hr />',$html);
  415. //给H1-6 添加uuid
  416. for ($i=1; $i<7 ; $i++) {
  417. if(strpos($html,"<h{$i}>")===false){
  418. continue;
  419. }
  420. $output = array();
  421. $input = $html;
  422. $hPos = strpos($input,"<h{$i}>");
  423. while ($hPos !== false) {
  424. $output[] = substr($input,0,$hPos);
  425. $output[] = "<h{$i} id='".Str::uuid()."'>";
  426. $input = substr($input,$hPos+4);
  427. $hPos = strpos($input,"<h{$i}>");
  428. }
  429. $output[] = $input;
  430. $html = implode('',$output);
  431. }
  432. $html = str_replace('mdtpl','MdTpl',$html);
  433. return $html;
  434. }
  435. /**
  436. * string[] $channelId
  437. */
  438. public static function render($markdown,$channelId,$queryId=null,$mode='read',$channelType='translation',$contentType="markdown"){
  439. return MdRender::render2($markdown,$channelId,$queryId,$mode,$channelType,$contentType);
  440. }
  441. public static function fixHtml($html) {
  442. $doc = new \DOMDocument();
  443. libxml_use_internal_errors(true);
  444. $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
  445. $doc->loadHTML('<span>'.$html.'</span>',LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  446. $fixed = $doc->saveHTML();
  447. $fixed = mb_convert_encoding($fixed, "UTF-8", 'HTML-ENTITIES');
  448. return $fixed;
  449. }
  450. }