MdRender.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 = "<xml>".$html."</xml>";
  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=""):string{
  77. /**
  78. * 解析xml
  79. * 获取模版参数
  80. * 生成react 组件参数
  81. */
  82. $dom = simplexml_load_string($xml);
  83. $tpl_list = $dom->xpath('//MdTpl');
  84. foreach ($tpl_list as $key => $tpl) {
  85. /**
  86. * 遍历 MdTpl 处理参数
  87. */
  88. $props = [];
  89. $tpl_name = '';
  90. foreach($tpl->attributes() as $a => $a_value){
  91. if($a==="name"){
  92. $tpl_name = $a_value;
  93. }
  94. }
  95. $param_id = 0;
  96. foreach ($tpl->children() as $param) {
  97. # 处理每个参数
  98. if($param->getName() === "param"){
  99. $param_id++;
  100. $props["{$param_id}"] = $param->__toString();
  101. foreach($param->attributes() as $pa => $pa_value){
  102. if($pa === "name"){
  103. $props["{$pa_value}"] = $param->__toString();
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * 生成模版参数
  110. */
  111. $tplRender = new TemplateRender($props,$channelId,'edit');
  112. $tplProps = $tplRender->render($tpl_name);
  113. if($tplProps){
  114. $tpl->addAttribute("props",$tplProps['props']);
  115. $tpl->addAttribute("tpl",$tplProps['tpl']);
  116. $tpl->addChild($tplProps['tag'],$tplProps['html']);
  117. }
  118. }
  119. $html = str_replace('<?xml version="1.0"?>','',$dom->asXML()) ;
  120. $html = str_replace(['<xml>','</xml>'],['<span>','</span>'],$html);
  121. return $html;
  122. }
  123. public static function render2($markdown,$channelId='',$queryId=null){
  124. $wiki = MdRender::markdown2wiki($markdown);
  125. $html = MdRender::wiki2xml($wiki);
  126. if(!is_null($queryId)){
  127. $html = MdRender::xmlQueryId($html, $queryId);
  128. }
  129. $tpl = MdRender::xml2tpl($html,$channelId);
  130. return $tpl;
  131. }
  132. public static function markdown2wiki(string $markdown): string{
  133. /**
  134. * 替换换行符
  135. * react 无法处理 <br> 替换为<div></div>代替换行符作用
  136. */
  137. $markdown = str_replace('<br>','<div></div>',$markdown);
  138. /**
  139. * markdown -> html
  140. */
  141. $html = Str::markdown($markdown);
  142. #替换术语
  143. $pattern = "/\[\[(.+?)\]\]/";
  144. $replacement = '{{term|$1}}';
  145. $html = preg_replace($pattern,$replacement,$html);
  146. #替换句子模版
  147. $pattern = "/\{\{([0-9].+?)\}\}/";
  148. $replacement = '{{sent|$1}}';
  149. $html = preg_replace($pattern,$replacement,$html);
  150. #替换注释
  151. #<code>bla</code>
  152. #{{note:bla}}
  153. $pattern = '/<code>(.+?)<\/code>/';
  154. $replacement = '{{note|$1}}';
  155. $html = preg_replace($pattern,$replacement,$html);
  156. return $html;
  157. }
  158. /**
  159. *
  160. */
  161. public static function render($markdown,$channelId,$queryId=null){
  162. return MdRender::render2($markdown,$channelId,$queryId);
  163. $html = MdRender::markdown2wiki($markdown);
  164. /**
  165. * 转换为Mustache模版
  166. */
  167. $pattern = "/\{\{(.+?)\}\}/";
  168. $replacement = "\n{{#function}}\n$1\n{{/function}}\n";
  169. $html = preg_replace($pattern,$replacement,$html);
  170. /**
  171. * Mustache_Engine 处理Mustache模版
  172. * 把Mustache模版内容转换为react组件
  173. */
  174. $m = new \Mustache_Engine(array('entity_flags' => ENT_QUOTES));
  175. $html = $m->render($html, array(
  176. 'function' => function($text) use($m,$channelId) {
  177. //1: 解析
  178. $param = explode("|",$text);
  179. //3: 处理业务逻辑
  180. $tplName = trim($param[0]);
  181. $innerString = "";
  182. switch($tplName){
  183. case 'term':
  184. //获取实际的参数
  185. $word = trim($param[1]);
  186. $props = Cache::remember("/term/{$channelId}/{$word}",
  187. 60,
  188. function() use($word,$channelId){
  189. $tplParam = DhammaTerm::where("word",$word)->first();
  190. $output = [
  191. "word" => $word,
  192. "channel" => $channelId,
  193. ];
  194. $innerString = $output["word"];
  195. if($tplParam){
  196. $output["id"] = $tplParam->guid;
  197. $output["meaning"] = $tplParam->meaning;
  198. $innerString = $output["meaning"];
  199. if(!empty($tplParam->other_meaning)){
  200. $output["meaning2"] = $tplParam->other_meaning;
  201. }
  202. }
  203. return $output;
  204. });
  205. break;
  206. case 'note':
  207. if(isset($param[1])){
  208. $props = ["note"=>trim($param[1])];
  209. }
  210. if(isset($param[2])){
  211. $props["trigger"] = trim($param[2]);
  212. $innerString = $props["trigger"];
  213. }
  214. break;
  215. case 'sent':
  216. $tplName = "sentedit";
  217. $innerString = "";
  218. $sentInfo = explode('@',trim($param[1]));
  219. $sentId = $sentInfo[0];
  220. $Sent = new CorpusController();
  221. if(empty($channelId)){
  222. $channels = [];
  223. }else{
  224. $channels = [$channelId];
  225. }
  226. if(isset($sentInfo[1])){
  227. $channels = [$sentInfo[1]];
  228. }
  229. $html = $Sent->getSentTpl($param[1],$channels);
  230. return $html;
  231. break;
  232. case 'quote':
  233. $paraId = trim($param[1]);
  234. $props = Cache::remember("/quote/{$channelId}/{$paraId}",
  235. 60,
  236. function() use($paraId,$channelId){
  237. $para = \explode('-',$paraId);
  238. $output = [
  239. "paraId" => $paraId,
  240. "channel" => $channelId,
  241. "innerString" => $paraId,
  242. ];
  243. if(count($para)<2){
  244. return $output;
  245. }
  246. $PaliText = PaliText::where("book",$para[0])
  247. ->where("paragraph",$para[1])
  248. ->select(['toc','path'])
  249. ->first();
  250. if($PaliText){
  251. $output["pali"] = $PaliText->toc;
  252. $output["paliPath"] = \json_decode($PaliText->path);
  253. $innerString = $PaliText->toc;
  254. }
  255. return $output;
  256. });
  257. break;
  258. case 'exercise':
  259. $exeId = trim($param[1]);
  260. $exeContent = trim($param[2]);
  261. $props = Cache::remember("/quote/{$channelId}/{$exeId}",
  262. 60,
  263. function() use($exeId,$channelId){
  264. $output = [
  265. "id" => $exeId,
  266. "channel" => $channelId,
  267. ];
  268. return $output;
  269. });
  270. #替换句子
  271. $pattern = "/\(\((.+?)\)\)/";
  272. $replacement = '{{sent|$1}}';
  273. Log::info("content{$exeContent}");
  274. $exeContent = preg_replace($pattern,$replacement,$exeContent);
  275. Log::info("content{$exeContent}");
  276. $innerString = MdRender::render($exeContent,$channelId);
  277. break;
  278. default:
  279. break;
  280. }
  281. //4: 返回拼好的字符串
  282. $props = base64_encode(\json_encode($props));
  283. $html = "<MdTpl tpl='{$tplName}' props='{$props}' >{$innerString}</MdTpl>";
  284. return $html;
  285. }
  286. ));
  287. if(substr_count($html,"<p>") === 1){
  288. $html = \str_replace(['<p>','</p>'],'',$html);
  289. }
  290. //LOG::info($html);
  291. return "<xml>{$html}</xml>";
  292. }
  293. }