MdRender.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. try{
  34. $dom = simplexml_load_string($xml);
  35. }catch(\Exception $e){
  36. Log::error($e);
  37. return "<div></div>";
  38. }
  39. $tpl_list = $dom->xpath('//MdTpl');
  40. foreach ($tpl_list as $key => $tpl) {
  41. foreach ($tpl->children() as $param) {
  42. # 处理每个参数
  43. if($param->getName() === "param"){
  44. foreach($param->attributes() as $pa => $pa_value){
  45. $pValue = $pa_value->__toString();
  46. if($pa === "name" && $pValue === "id"){
  47. if($param->__toString() === $id){
  48. return $tpl->asXML();
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }
  55. return "<div></div>";
  56. }
  57. public static function take_sentence(string $xml):array{
  58. $output = [];
  59. try{
  60. $dom = simplexml_load_string($xml);
  61. }catch(\Exception $e){
  62. Log::error($e);
  63. return $output;
  64. }
  65. $tpl_list = $dom->xpath('//MdTpl');
  66. foreach ($tpl_list as $key => $tpl) {
  67. foreach($tpl->attributes() as $a => $a_value){
  68. if($a==="name"){
  69. if($a_value->__toString() ==="sent"){
  70. foreach ($tpl->children() as $param) {
  71. # 处理每个参数
  72. if($param->getName() === "param"){
  73. $sent = $param->__toString();
  74. if(!empty($sent)){
  75. $output[] = $sent;
  76. break;
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. return $output;
  85. }
  86. public static function xml2tpl(string $xml, $channelId="",$mode='read'):string{
  87. /**
  88. * 解析xml
  89. * 获取模版参数
  90. * 生成react 组件参数
  91. */
  92. try{
  93. $dom = simplexml_load_string($xml);
  94. }catch(\Exception $e){
  95. Log::error($e);
  96. Log::error($xml);
  97. return "<span>xml解析错误{$e}</span>";
  98. }
  99. $tpl_list = $dom->xpath('//MdTpl');
  100. foreach ($tpl_list as $key => $tpl) {
  101. /**
  102. * 遍历 MdTpl 处理参数
  103. */
  104. $props = [];
  105. $tpl_name = '';
  106. foreach($tpl->attributes() as $a => $a_value){
  107. if($a==="name"){
  108. $tpl_name = $a_value;
  109. }
  110. }
  111. $param_id = 0;
  112. foreach ($tpl->children() as $param) {
  113. # 处理每个参数
  114. if($param->getName() === "param"){
  115. $param_id++;
  116. $props["{$param_id}"] = $param->__toString();
  117. foreach($param->attributes() as $pa => $pa_value){
  118. if($pa === "name"){
  119. $props["{$pa_value}"] = $param->__toString();
  120. }
  121. }
  122. }
  123. }
  124. /**
  125. * 生成模版参数
  126. */
  127. $tplRender = new TemplateRender($props,$channelId,$mode);
  128. $tplProps = $tplRender->render($tpl_name);
  129. if($tplProps){
  130. $tpl->addAttribute("props",$tplProps['props']);
  131. $tpl->addAttribute("tpl",$tplProps['tpl']);
  132. $tpl->addChild($tplProps['tag'],$tplProps['html']);
  133. }
  134. }
  135. $html = str_replace('<?xml version="1.0"?>','',$dom->asXML()) ;
  136. $html = str_replace(['<xml>','</xml>'],['<span>','</span>'],$html);
  137. return $html;
  138. }
  139. public static function render2($markdown,$channelId='',$queryId=null,$mode='read',$channelType){
  140. $wiki = MdRender::markdown2wiki($markdown,$channelType);
  141. $html = MdRender::wiki2xml($wiki);
  142. if(!is_null($queryId)){
  143. $html = MdRender::xmlQueryId($html, $queryId);
  144. }
  145. $tpl = MdRender::xml2tpl($html,$channelId,$mode);
  146. return $tpl;
  147. }
  148. public static function markdown2wiki(string $markdown,$channelType): string{
  149. /**
  150. * nissaya
  151. * aaa=bbb\n
  152. * {{nissaya|aaa|bbb}}
  153. */
  154. if($channelType==='nissaya'){
  155. $pattern = '/(.+?)=(.+?)\n/';
  156. $replacement = '{{nissaya|$1|$2}}';
  157. $markdown = preg_replace($pattern,$replacement,$markdown);
  158. $pattern = '/(.+?)=(.?)\n/';
  159. $replacement = '{{nissaya|$1|$2}}';
  160. $markdown = preg_replace($pattern,$replacement,$markdown);
  161. $pattern = '/(.?)=(.+?)\n/';
  162. $replacement = '{{nissaya|$1|$2}}';
  163. $markdown = preg_replace($pattern,$replacement,$markdown);
  164. }
  165. $markdown = preg_replace("/\n\n/","<div></div>",$markdown);
  166. /**
  167. * 替换换行符
  168. * react 无法处理 <br> 替换为<div></div>代替换行符作用
  169. */
  170. $markdown = str_replace('<br>','<div></div>',$markdown);
  171. /**
  172. * markdown -> html
  173. */
  174. $html = Str::markdown($markdown);
  175. #替换术语
  176. $pattern = "/\[\[(.+?)\]\]/";
  177. $replacement = '{{term|$1}}';
  178. $html = preg_replace($pattern,$replacement,$html);
  179. #替换句子模版
  180. $pattern = "/\{\{([0-9].+?)\}\}/";
  181. $replacement = '{{sent|$1}}';
  182. $html = preg_replace($pattern,$replacement,$html);
  183. #替换单行注释
  184. #<code>bla</code>
  185. #{{note|bla}}
  186. $pattern = '/<code>(.+?)<\/code>/';
  187. $replacement = '{{note|$1}}';
  188. $html = preg_replace($pattern,$replacement,$html);
  189. #替换多行注释
  190. #<pre><code>bla</code></pre>
  191. #{{note|bla}}
  192. $pattern = '/<pre><code>([\w\W]+?)<\/code><\/pre>/';
  193. $replacement = '{{note|$1}}';
  194. $html = preg_replace($pattern,$replacement,$html);
  195. return $html;
  196. }
  197. /**
  198. *
  199. */
  200. public static function render($markdown,$channelId,$queryId=null,$mode='read',$channelType='translation'){
  201. return MdRender::render2($markdown,$channelId,$queryId,$mode,$channelType);
  202. }
  203. }