MdRender.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\Http\Controllers\CorpusController;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Support\Facades\Log;
  10. class MdRender{
  11. public static function wiki2xml(string $wiki):string{
  12. /**
  13. * 替换{{}} 到xml之前 要先把换行符号去掉
  14. */
  15. $html = str_replace("\n","",$wiki);
  16. $pattern = "/\{\{(.+?)\|/";
  17. $replacement = '<MdTpl name="$1"><param>';
  18. $html = preg_replace($pattern,$replacement,$html);
  19. $html = str_replace("}}","</param></MdTpl>",$html);
  20. $html = str_replace("|","</param><param>",$html);
  21. /**
  22. * 替换变量名
  23. */
  24. $pattern = "/<param>([a-z]+?)=/";
  25. $replacement = '<param name="$1">';
  26. $html = preg_replace($pattern,$replacement,$html);
  27. $html = str_replace("<p>","<div>",$html);
  28. $html = str_replace("</p>","</div>",$html);
  29. $html = "<span>".$html."</span>";
  30. return $html;
  31. }
  32. public static function xmlQueryId(string $xml, string $id):string{
  33. $dom = simplexml_load_string($xml);
  34. $tpl_list = $dom->xpath('//MdTpl');
  35. foreach ($tpl_list as $key => $tpl) {
  36. foreach ($tpl->children() as $param) {
  37. # 处理每个参数
  38. if($param->getName() === "param"){
  39. foreach($param->attributes() as $pa => $pa_value){
  40. $pValue = $pa_value->__toString();
  41. if($pa === "name" && $pValue === "id"){
  42. if($param->__toString() === $id){
  43. return $tpl->asXML();
  44. }
  45. }
  46. }
  47. }
  48. }
  49. }
  50. return "<div></div>";
  51. }
  52. public static function take_sentence(string $xml):array{
  53. $output = [];
  54. $dom = simplexml_load_string($xml);
  55. $tpl_list = $dom->xpath('//MdTpl');
  56. foreach ($tpl_list as $key => $tpl) {
  57. foreach($tpl->attributes() as $a => $a_value){
  58. if($a==="name"){
  59. if($a_value->__toString() ==="sent"){
  60. foreach ($tpl->children() as $param) {
  61. # 处理每个参数
  62. if($param->getName() === "param"){
  63. $sent = $param->__toString();
  64. if(!empty($sent)){
  65. $output[] = $sent;
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. return $output;
  75. }
  76. public static function xml2tpl(string $xml, $channelId="",$mode='read'):string{
  77. /**
  78. * 解析xml
  79. * 获取模版参数
  80. * 生成react 组件参数
  81. */
  82. //$xml = str_replace(['<b>','</b>'],['',''],$xml);
  83. $dom = simplexml_load_string($xml);
  84. $tpl_list = $dom->xpath('//MdTpl');
  85. foreach ($tpl_list as $key => $tpl) {
  86. /**
  87. * 遍历 MdTpl 处理参数
  88. */
  89. $props = [];
  90. $tpl_name = '';
  91. foreach($tpl->attributes() as $a => $a_value){
  92. if($a==="name"){
  93. $tpl_name = $a_value;
  94. }
  95. }
  96. $param_id = 0;
  97. foreach ($tpl->children() as $param) {
  98. # 处理每个参数
  99. if($param->getName() === "param"){
  100. $param_id++;
  101. $props["{$param_id}"] = $param->__toString();
  102. foreach($param->attributes() as $pa => $pa_value){
  103. if($pa === "name"){
  104. $props["{$pa_value}"] = $param->__toString();
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * 生成模版参数
  111. */
  112. $tplRender = new TemplateRender($props,$channelId,$mode);
  113. $tplProps = $tplRender->render($tpl_name);
  114. if($tplProps){
  115. $tpl->addAttribute("props",$tplProps['props']);
  116. $tpl->addAttribute("tpl",$tplProps['tpl']);
  117. $tpl->addChild($tplProps['tag'],$tplProps['html']);
  118. }
  119. }
  120. $html = str_replace('<?xml version="1.0"?>','',$dom->asXML()) ;
  121. $html = str_replace(['<xml>','</xml>'],['<span>','</span>'],$html);
  122. return $html;
  123. }
  124. public static function render2($markdown,$channelId='',$queryId=null,$mode='read'){
  125. $wiki = MdRender::markdown2wiki($markdown);
  126. $html = MdRender::wiki2xml($wiki);
  127. if(!is_null($queryId)){
  128. $html = MdRender::xmlQueryId($html, $queryId);
  129. }
  130. $tpl = MdRender::xml2tpl($html,$channelId,$mode);
  131. return $tpl;
  132. }
  133. public static function markdown2wiki(string $markdown): string{
  134. /**
  135. * 替换换行符
  136. * react 无法处理 <br> 替换为<div></div>代替换行符作用
  137. */
  138. $markdown = str_replace('<br>','<div></div>',$markdown);
  139. /**
  140. * markdown -> html
  141. */
  142. $html = Str::markdown($markdown);
  143. #替换术语
  144. $pattern = "/\[\[(.+?)\]\]/";
  145. $replacement = '{{term|$1}}';
  146. $html = preg_replace($pattern,$replacement,$html);
  147. #替换句子模版
  148. $pattern = "/\{\{([0-9].+?)\}\}/";
  149. $replacement = '{{sent|$1}}';
  150. $html = preg_replace($pattern,$replacement,$html);
  151. #替换注释
  152. #<code>bla</code>
  153. #{{note:bla}}
  154. $pattern = '/<code>(.+?)<\/code>/';
  155. $replacement = '{{note|$1}}';
  156. $html = preg_replace($pattern,$replacement,$html);
  157. return $html;
  158. }
  159. /**
  160. *
  161. */
  162. public static function render($markdown,$channelId,$queryId=null,$mode='read'){
  163. return MdRender::render2($markdown,$channelId,$queryId,$mode);
  164. }
  165. }