MdRender.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. return "<span>xml解析错误</span>";
  97. }
  98. $tpl_list = $dom->xpath('//MdTpl');
  99. foreach ($tpl_list as $key => $tpl) {
  100. /**
  101. * 遍历 MdTpl 处理参数
  102. */
  103. $props = [];
  104. $tpl_name = '';
  105. foreach($tpl->attributes() as $a => $a_value){
  106. if($a==="name"){
  107. $tpl_name = $a_value;
  108. }
  109. }
  110. $param_id = 0;
  111. foreach ($tpl->children() as $param) {
  112. # 处理每个参数
  113. if($param->getName() === "param"){
  114. $param_id++;
  115. $props["{$param_id}"] = $param->__toString();
  116. foreach($param->attributes() as $pa => $pa_value){
  117. if($pa === "name"){
  118. $props["{$pa_value}"] = $param->__toString();
  119. }
  120. }
  121. }
  122. }
  123. /**
  124. * 生成模版参数
  125. */
  126. $tplRender = new TemplateRender($props,$channelId,$mode);
  127. $tplProps = $tplRender->render($tpl_name);
  128. if($tplProps){
  129. $tpl->addAttribute("props",$tplProps['props']);
  130. $tpl->addAttribute("tpl",$tplProps['tpl']);
  131. $tpl->addChild($tplProps['tag'],$tplProps['html']);
  132. }
  133. }
  134. $html = str_replace('<?xml version="1.0"?>','',$dom->asXML()) ;
  135. $html = str_replace(['<xml>','</xml>'],['<span>','</span>'],$html);
  136. return $html;
  137. }
  138. public static function render2($markdown,$channelId='',$queryId=null,$mode='read',$channelType){
  139. $wiki = MdRender::markdown2wiki($markdown,$channelType);
  140. $html = MdRender::wiki2xml($wiki);
  141. if(!is_null($queryId)){
  142. $html = MdRender::xmlQueryId($html, $queryId);
  143. }
  144. $tpl = MdRender::xml2tpl($html,$channelId,$mode);
  145. return $tpl;
  146. }
  147. public static function markdown2wiki(string $markdown,$channelType): string{
  148. /**
  149. * nissaya
  150. * aaa=bbb\n
  151. * {{nissaya|aaa|bbb}}
  152. */
  153. if($channelType==='nissaya'){
  154. $pattern = '/(.+?)=(.+?)\n/';
  155. $replacement = '{{nissaya|$1|$2}}';
  156. $markdown = preg_replace($pattern,$replacement,$markdown);
  157. $pattern = '/(.+?)=(.?)\n/';
  158. $replacement = '{{nissaya|$1|$2}}';
  159. $markdown = preg_replace($pattern,$replacement,$markdown);
  160. $pattern = '/(.?)=(.+?)\n/';
  161. $replacement = '{{nissaya|$1|$2}}';
  162. $markdown = preg_replace($pattern,$replacement,$markdown);
  163. }
  164. /**
  165. * 替换换行符
  166. * react 无法处理 <br> 替换为<div></div>代替换行符作用
  167. */
  168. $markdown = str_replace('<br>','<div></div>',$markdown);
  169. /**
  170. * markdown -> html
  171. */
  172. $html = Str::markdown($markdown);
  173. #替换术语
  174. $pattern = "/\[\[(.+?)\]\]/";
  175. $replacement = '{{term|$1}}';
  176. $html = preg_replace($pattern,$replacement,$html);
  177. #替换句子模版
  178. $pattern = "/\{\{([0-9].+?)\}\}/";
  179. $replacement = '{{sent|$1}}';
  180. $html = preg_replace($pattern,$replacement,$html);
  181. #替换注释
  182. #<code>bla</code>
  183. #{{note|bla}}
  184. $pattern = '/<code>(.+?)<\/code>/';
  185. $replacement = '{{note|$1}}';
  186. $html = preg_replace($pattern,$replacement,$html);
  187. return $html;
  188. }
  189. /**
  190. *
  191. */
  192. public static function render($markdown,$channelId,$queryId=null,$mode='read',$channelType='translation'){
  193. return MdRender::render2($markdown,$channelId,$queryId,$mode,$channelType);
  194. }
  195. }