MdRender.php 12 KB

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