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. $markdown = str_replace(['[[',']]'],['㐛','㐚'],$markdown);
  335. $html = Str::markdown($markdown);
  336. $html = str_replace(['㐛','㐚'],['[[',']]'],$html);
  337. $html = MdRender::fixHtml($html);
  338. */
  339. #替换术语
  340. $pattern = "/\[\[(.+?)\]\]/";
  341. $replacement = '{{term|$1}}';
  342. $markdown = preg_replace($pattern,$replacement,$markdown);
  343. #替换句子模版
  344. $pattern = "/\{\{([0-9].+?)\}\}/";
  345. $replacement = '{{sent|$1}}';
  346. $markdown = preg_replace($pattern,$replacement,$markdown);
  347. /**
  348. * 替换多行注释
  349. * ```
  350. * bla
  351. * bla
  352. * ```
  353. * {{note|
  354. * bla
  355. * bla
  356. * }}
  357. */
  358. if(strpos($markdown,"```\n") !== false){
  359. $lines = explode("\n",$markdown);
  360. $newLines = array();
  361. $noteBegin = false;
  362. $noteString = array();
  363. foreach ($lines as $line) {
  364. if($noteBegin){
  365. if($line === "```"){
  366. $newLines[] = "}}";
  367. $noteBegin = false;
  368. }else{
  369. $newLines[] = $line;
  370. }
  371. }else{
  372. if($line === "```"){
  373. $noteBegin = true;
  374. $newLines[] = "{{note|";
  375. continue;
  376. }else{
  377. $newLines[] = $line;
  378. }
  379. }
  380. }
  381. if($noteBegin){
  382. $newLines[] = "}}";
  383. }
  384. $markdown = implode("\n",$newLines);
  385. }
  386. /**
  387. * 替换单行注释
  388. * `bla bla`
  389. * {{note|bla}}
  390. */
  391. $pattern = "/`(.+?)`/";
  392. $replacement = '{{note|$1}}';
  393. $markdown = preg_replace($pattern,$replacement,$markdown);
  394. /*
  395. #替换多行注释
  396. #<pre><code>bla</code></pre>
  397. #{{note|bla}}
  398. $pattern = '/<pre><code>([\w\W]+?)<\/code><\/pre>/';
  399. $replacement = '{{note|$1}}';
  400. $html = preg_replace($pattern,$replacement,$html);
  401. #替换单行注释
  402. #<code>bla</code>
  403. #{{note|bla}}
  404. $pattern = '/<code>(.+?)<\/code>/';
  405. $replacement = '{{note|$1}}';
  406. $html = preg_replace($pattern,$replacement,$html);
  407. */
  408. return $markdown;
  409. }
  410. public static function markdownToHtml($markdown){
  411. $markdown = str_replace('MdTpl','mdtpl',$markdown);
  412. $markdown = str_replace(['<param','</param>'],['<span','</span>'],$markdown);
  413. $html = Str::markdown($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. }