MdRender.php 12 KB

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