MdRender.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. class MdRender{
  12. public static function wiki2xml(string $wiki):string{
  13. /**
  14. * 替换{{}} 到xml之前 要先把换行符号去掉
  15. */
  16. $html = str_replace("\n","",$wiki);
  17. $pattern = "/\{\{(.+?)\|/";
  18. $replacement = '<MdTpl name="$1"><param>';
  19. $html = preg_replace($pattern,$replacement,$html);
  20. $html = str_replace("}}","</param></MdTpl>",$html);
  21. $html = str_replace("|","</param><param>",$html);
  22. /**
  23. * 替换变量名
  24. */
  25. $pattern = "/<param>([a-z]+?)=/";
  26. $replacement = '<param name="$1">';
  27. $html = preg_replace($pattern,$replacement,$html);
  28. $html = str_replace("<p>","<div>",$html);
  29. $html = str_replace("</p>","</div>",$html);
  30. $html = "<span>".$html."</span>";
  31. return $html;
  32. }
  33. public static function xmlQueryId(string $xml, string $id):string{
  34. try{
  35. $dom = simplexml_load_string($xml);
  36. }catch(\Exception $e){
  37. Log::error($e);
  38. return "<div></div>";
  39. }
  40. $tpl_list = $dom->xpath('//MdTpl');
  41. foreach ($tpl_list as $key => $tpl) {
  42. foreach ($tpl->children() as $param) {
  43. # 处理每个参数
  44. if($param->getName() === "param"){
  45. foreach($param->attributes() as $pa => $pa_value){
  46. $pValue = $pa_value->__toString();
  47. if($pa === "name" && $pValue === "id"){
  48. if($param->__toString() === $id){
  49. return $tpl->asXML();
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. return "<div></div>";
  57. }
  58. public static function take_sentence(string $xml):array{
  59. $output = [];
  60. try{
  61. $dom = simplexml_load_string($xml);
  62. }catch(\Exception $e){
  63. Log::error($e);
  64. return $output;
  65. }
  66. $tpl_list = $dom->xpath('//MdTpl');
  67. foreach ($tpl_list as $key => $tpl) {
  68. foreach($tpl->attributes() as $a => $a_value){
  69. if($a==="name"){
  70. if($a_value->__toString() ==="sent"){
  71. foreach ($tpl->children() as $param) {
  72. # 处理每个参数
  73. if($param->getName() === "param"){
  74. $sent = $param->__toString();
  75. if(!empty($sent)){
  76. $output[] = $sent;
  77. break;
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. return $output;
  86. }
  87. public static function xml2tpl(string $xml, $channelId="",$mode='read'):string{
  88. /**
  89. * 解析xml
  90. * 获取模版参数
  91. * 生成react 组件参数
  92. */
  93. try{
  94. $dom = simplexml_load_string($xml);
  95. }catch(\Exception $e){
  96. Log::error($e);
  97. Log::error($xml);
  98. return "<span>xml解析错误{$e}</span>";
  99. }
  100. $channelInfo = Channel::find($channelId);
  101. $tpl_list = $dom->xpath('//MdTpl');
  102. foreach ($tpl_list as $key => $tpl) {
  103. /**
  104. * 遍历 MdTpl 处理参数
  105. */
  106. $props = [];
  107. $tpl_name = '';
  108. foreach($tpl->attributes() as $a => $a_value){
  109. if($a==="name"){
  110. $tpl_name = $a_value;
  111. }
  112. }
  113. $param_id = 0;
  114. foreach ($tpl->children() as $param) {
  115. # 处理每个参数
  116. if($param->getName() === "param"){
  117. $param_id++;
  118. $props["{$param_id}"] = $param->__toString();
  119. foreach($param->attributes() as $pa => $pa_value){
  120. if($pa === "name"){
  121. $props["{$pa_value}"] = $param->__toString();
  122. }
  123. }
  124. }
  125. }
  126. /**
  127. * 生成模版参数
  128. */
  129. $tplRender = new TemplateRender($props,$channelInfo,$mode);
  130. $tplProps = $tplRender->render($tpl_name);
  131. if($tplProps){
  132. $tpl->addAttribute("props",$tplProps['props']);
  133. $tpl->addAttribute("tpl",$tplProps['tpl']);
  134. $tpl->addChild($tplProps['tag'],$tplProps['html']);
  135. }
  136. }
  137. $html = str_replace('<?xml version="1.0"?>','',$dom->asXML()) ;
  138. $html = str_replace(['<xml>','</xml>'],['<span>','</span>'],$html);
  139. return $html;
  140. }
  141. public static function render2($markdown,$channelId='',$queryId=null,$mode='read',$channelType,$contentType="markdown"){
  142. $wiki = MdRender::markdown2wiki($markdown,$channelType,$contentType);
  143. $html = MdRender::wiki2xml($wiki);
  144. if(!is_null($queryId)){
  145. $html = MdRender::xmlQueryId($html, $queryId);
  146. }
  147. $tpl = MdRender::xml2tpl($html,$channelId,$mode);
  148. return $tpl;
  149. }
  150. public static function markdown2wiki(string $markdown,$channelType,$contentType): string{
  151. /**
  152. * nissaya
  153. * aaa=bbb\n
  154. * {{nissaya|aaa|bbb}}
  155. */
  156. if($channelType==='nissaya'){
  157. if($contentType === "json"){
  158. $json = json_decode($markdown);
  159. $nissayaWord = [];
  160. foreach ($json as $word) {
  161. if(count($word->sn) === 1){
  162. //只输出第一层级
  163. $str = "{{nissaya|";
  164. if(isset($word->word->value)){
  165. $str .= $word->word->value;
  166. }
  167. $str .= "|";
  168. if(isset($word->meaning->value)){
  169. $str .= $word->meaning->value;
  170. }
  171. $str .= "}}";
  172. $nissayaWord[] = $str;
  173. }
  174. }
  175. $markdown = implode('',$nissayaWord);
  176. }else{
  177. $pattern = '/(.+?)=(.+?)\n/';
  178. $replacement = '{{nissaya|$1|$2}}';
  179. $markdown = preg_replace($pattern,$replacement,$markdown);
  180. $pattern = '/(.+?)=(.?)\n/';
  181. $replacement = '{{nissaya|$1|$2}}';
  182. $markdown = preg_replace($pattern,$replacement,$markdown);
  183. $pattern = '/(.?)=(.+?)\n/';
  184. $replacement = '{{nissaya|$1|$2}}';
  185. $markdown = preg_replace($pattern,$replacement,$markdown);
  186. }
  187. }
  188. $markdown = preg_replace("/\n\n/","<div></div>",$markdown);
  189. /**
  190. * 替换换行符
  191. * react 无法处理 <br> 替换为<div></div>代替换行符作用
  192. */
  193. $markdown = str_replace('<br>','<div></div>',$markdown);
  194. /**
  195. * markdown -> html
  196. */
  197. $html = Str::markdown($markdown);
  198. #替换术语
  199. $pattern = "/\[\[(.+?)\]\]/";
  200. $replacement = '{{term|$1}}';
  201. $html = preg_replace($pattern,$replacement,$html);
  202. #替换句子模版
  203. $pattern = "/\{\{([0-9].+?)\}\}/";
  204. $replacement = '{{sent|$1}}';
  205. $html = preg_replace($pattern,$replacement,$html);
  206. #替换单行注释
  207. #<code>bla</code>
  208. #{{note|bla}}
  209. $pattern = '/<code>(.+?)<\/code>/';
  210. $replacement = '{{note|$1}}';
  211. $html = preg_replace($pattern,$replacement,$html);
  212. #替换多行注释
  213. #<pre><code>bla</code></pre>
  214. #{{note|bla}}
  215. $pattern = '/<pre><code>([\w\W]+?)<\/code><\/pre>/';
  216. $replacement = '{{note|$1}}';
  217. $html = preg_replace($pattern,$replacement,$html);
  218. return $html;
  219. }
  220. /**
  221. *
  222. */
  223. public static function render($markdown,$channelId,$queryId=null,$mode='read',$channelType='translation',$contentType="markdown"){
  224. return MdRender::render2($markdown,$channelId,$queryId,$mode,$channelType,$contentType);
  225. }
  226. }