TemplateRender.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. <?php
  2. namespace App\Http\Api;
  3. use App\Models\DhammaTerm;
  4. use App\Models\PaliText;
  5. use App\Models\Channel;
  6. use App\Http\Controllers\CorpusController;
  7. use Illuminate\Support\Facades\Cache;
  8. use App\Tools\RedisClusters;
  9. use Illuminate\Support\Facades\Log;
  10. use App\Http\Api\ChannelApi;
  11. use App\Http\Api\MdRender;
  12. class TemplateRender{
  13. protected $param = [];
  14. protected $mode = "read";
  15. protected $channel_id = [];
  16. protected $debug = [];
  17. protected $format = 'react';
  18. /**
  19. * Create a new command instance.
  20. * string $mode 'read' | 'edit'
  21. * string $format 'react' | 'text' | 'tex' | 'unity'
  22. * @return void
  23. */
  24. public function __construct($param, $channelInfo, $mode,$format='react',$debug=[])
  25. {
  26. $this->param = $param;
  27. foreach ($channelInfo as $value) {
  28. $this->channel_id[] = $value->uid;
  29. }
  30. $this->channelInfo = $channelInfo;
  31. $this->mode = $mode;
  32. $this->format = $format;
  33. $this->debug = $debug;
  34. }
  35. private function info($message,$debug){
  36. if(in_array($debug,$this->debug)){
  37. Log::info($message);
  38. }
  39. }
  40. private function error($message,$debug){
  41. if(in_array($debug,$this->debug)){
  42. Log::error($message);
  43. }
  44. }
  45. public function render($tpl_name){
  46. switch ($tpl_name) {
  47. case 'term':
  48. # 术语
  49. $result = $this->render_term();
  50. break;
  51. case 'note':
  52. $result = $this->render_note();
  53. break;
  54. case 'sent':
  55. $result = $this->render_sent();
  56. break;
  57. case 'quote':
  58. $result = $this->render_quote();
  59. break;
  60. case 'exercise':
  61. $result = $this->render_exercise();
  62. break;
  63. case 'article':
  64. $result = $this->render_article();
  65. break;
  66. case 'nissaya':
  67. $result = $this->render_nissaya();
  68. break;
  69. case 'mermaid':
  70. $result = $this->render_mermaid();
  71. break;
  72. default:
  73. # code...
  74. $result = [
  75. 'props'=>base64_encode(\json_encode([])),
  76. 'html'=>'',
  77. 'tag'=>'span',
  78. 'tpl'=>'unknown',
  79. ];
  80. break;
  81. }
  82. return $result;
  83. }
  84. public function getTermProps($word,$channelId,$channelInfo){
  85. $lang = Channel::where('uid',$channelId)->value('lang');
  86. if(!empty($lang)){
  87. $langFamily = explode('-',$lang)[0];
  88. }else{
  89. $langFamily = 'zh';
  90. }
  91. $this->info("term:{$word} 先查属于这个channel 的",'term');
  92. $this->info('channel id'.$channelId,'term');
  93. $tplParam = DhammaTerm::where("word",$word)
  94. ->where('channal',$channelId)
  95. ->orderBy('updated_at','desc')
  96. ->first();
  97. if(!$tplParam){
  98. /**
  99. * 没有,再查这个studio的
  100. * 按照语言过滤
  101. * 完全匹配的优先
  102. * 语族匹配也行
  103. */
  104. $this->info("没有-再查这个studio的",'term');
  105. $termsInStudio = DhammaTerm::where("word",$word)
  106. ->where('owner',$channelInfo->owner_uid)
  107. ->orderBy('updated_at','desc')
  108. ->get();
  109. if(count($termsInStudio)>0){
  110. $list = array();
  111. foreach ($termsInStudio as $key=>$term) {
  112. if(empty($term->channal)){
  113. if($term->language===$lang){
  114. $list[$term->guid]=2;
  115. }else if(strpos($term->language,$langFamily)!==false){
  116. $list[$term->guid]=1;
  117. }
  118. }
  119. }
  120. if(count($list)>0){
  121. arsort($list);
  122. foreach ($list as $key => $one) {
  123. foreach ($termsInStudio as $term) {
  124. if($term->guid===$key){
  125. $tplParam = $term;
  126. break;
  127. }
  128. }
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. if(!$tplParam){
  135. $this->info("没有,再查社区",'term');
  136. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  137. $tplParam = DhammaTerm::where("word",$word)
  138. ->where('channal',$community_channel)
  139. ->first();
  140. if($tplParam){
  141. $isCommunity = true;
  142. }else{
  143. $this->info("查社区没有",'term');
  144. }
  145. }
  146. $output = [
  147. "word" => $word,
  148. "parentChannelId" => $channelId,
  149. "parentStudioId" => $channelInfo->owner_uid,
  150. ];
  151. $innerString = $output["word"];
  152. if($tplParam){
  153. $output["id"] = $tplParam->guid;
  154. $output["meaning"] = $tplParam->meaning;
  155. $output["channel"] = $tplParam->channal;
  156. if(isset($isCommunity)){
  157. $output["isCommunity"] = true;
  158. }
  159. $innerString = "{$output["meaning"]}({$output["word"]})";
  160. if(!empty($tplParam->other_meaning)){
  161. $output["meaning2"] = $tplParam->other_meaning;
  162. }
  163. /*
  164. if($tplParam->note){
  165. $output["summary"] = $tplParam->note;
  166. }else{
  167. //本人没有解释内容的。用社区数据。
  168. //TODO 由作者(读者)设置是否使用社区数据
  169. //获取channel 语言
  170. //使用社区note
  171. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  172. //查找社区解释
  173. $community_note = DhammaTerm::where("word",$word)
  174. ->where('channal',$community_channel)
  175. ->value('note');
  176. $output["summary"] = $tplParam->note;
  177. $output["community"] = true;
  178. }
  179. */
  180. }
  181. $output['innerHtml'] = $innerString;
  182. return $output;
  183. }
  184. private function render_term(){
  185. $word = $this->get_param($this->param,"word",1);
  186. $channelId = $this->channel_id[0];
  187. $channelInfo = $this->channelInfo[0];
  188. $key = "/term/{$channelId}/{$word}";
  189. $props = $this->getTermProps($word,$channelId,$channelInfo);
  190. $output = $props['word'];
  191. switch ($this->format) {
  192. case 'react':
  193. $output=[
  194. 'props'=>base64_encode(\json_encode($props)),
  195. 'html'=>$props['innerHtml'],
  196. 'tag'=>'span',
  197. 'tpl'=>'term',
  198. ];
  199. break;
  200. case 'unity':
  201. $output=[
  202. 'props'=>base64_encode(\json_encode($props)),
  203. 'tpl'=>'term',
  204. ];
  205. break;
  206. case 'html':
  207. if(isset($props["meaning"])){
  208. $key = 'term-'.$props["word"];
  209. $termHead = "<a href='#'>".$props['meaning']."</a>";
  210. if(isset($GLOBALS[$key])){
  211. $output = $termHead;
  212. }else{
  213. $GLOBALS[$key] = 1;
  214. $output = $termHead.'(<em>'.$props["word"].'</em>)';
  215. }
  216. }else{
  217. $output = $props["word"];
  218. }
  219. break;
  220. case 'text':
  221. if(isset($props["meaning"])){
  222. $key = 'term-'.$props["word"];
  223. if(isset($GLOBALS[$key])){
  224. $output = $props["meaning"];
  225. }else{
  226. $GLOBALS[$key] = 1;
  227. $output = $props["meaning"].'('.$props["word"].')';
  228. }
  229. }else{
  230. $output = $props["word"];
  231. }
  232. break;
  233. case 'tex':
  234. if(isset($props["meaning"])){
  235. $key = 'term-'.$props["word"];
  236. if(isset($GLOBALS[$key])){
  237. $output = $props["meaning"];
  238. }else{
  239. $GLOBALS[$key] = 1;
  240. $output = $props["meaning"].'('.$props["word"].')';
  241. }
  242. }else{
  243. $output = $props["word"];
  244. }
  245. break;
  246. }
  247. return $output;
  248. }
  249. private function render_note(){
  250. $note = $this->get_param($this->param,"text",1);
  251. $trigger = $this->get_param($this->param,"trigger",2);
  252. $props = ["note" => $note ];
  253. $innerString = "";
  254. if(!empty($trigger)){
  255. $props["trigger"] = $trigger;
  256. $innerString = $props["trigger"];
  257. }
  258. if($this->format==='unity'){
  259. $props["note"] = MdRender::render($props["note"],
  260. $this->channel_id,
  261. null,
  262. 'read',
  263. 'translation',
  264. 'markdown',
  265. 'unity'
  266. );
  267. }
  268. $output = $note;
  269. switch ($this->format) {
  270. case 'react':
  271. $output=[
  272. 'props'=>base64_encode(\json_encode($props)),
  273. 'html'=>$innerString,
  274. 'tag'=>'span',
  275. 'tpl'=>'note',
  276. ];
  277. break;
  278. case 'unity':
  279. $output=[
  280. 'props'=>base64_encode(\json_encode($props)),
  281. 'tpl'=>'note',
  282. ];
  283. break;
  284. case 'html':
  285. if(isset($GLOBALS['note_sn'])){
  286. $GLOBALS['note_sn']++;
  287. }else{
  288. $GLOBALS['note_sn'] = 1;
  289. $GLOBALS['note'] = array();
  290. }
  291. $GLOBALS['note'][] = [
  292. 'sn' => 1,
  293. 'trigger' => $trigger,
  294. 'content' => MdRender::render($props["note"],
  295. $this->channel_id,
  296. null,
  297. 'read',
  298. 'translation',
  299. 'markdown',
  300. 'html'
  301. ),
  302. ];
  303. $link="<a href='#footnote-".$GLOBALS['note_sn']."' name='note-".$GLOBALS['note_sn']."'>";
  304. if(empty($trigger)){
  305. $output = $link. "<sup>[" . $GLOBALS['note_sn'] . "]</sup></a>";
  306. }else{
  307. $output = $link . $trigger . "</a>";
  308. }
  309. break;
  310. case 'text':
  311. $output = $trigger;
  312. break;
  313. case 'tex':
  314. $output = $trigger;
  315. break;
  316. }
  317. return $output;
  318. }
  319. private function render_nissaya(){
  320. $pali = $this->get_param($this->param,"pali",1);
  321. $meaning = $this->get_param($this->param,"meaning",2);
  322. $innerString = "";
  323. $props = [
  324. "pali" => $pali,
  325. "meaning" => $meaning,
  326. ];
  327. switch ($this->format) {
  328. case 'react':
  329. $output = [
  330. 'props'=>base64_encode(\json_encode($props)),
  331. 'html'=>$innerString,
  332. 'tag'=>'span',
  333. 'tpl'=>'nissaya',
  334. ];
  335. break;
  336. case 'unity':
  337. $output = [
  338. 'props'=>base64_encode(\json_encode($props)),
  339. 'tpl'=>'nissaya',
  340. ];
  341. break;
  342. case 'text':
  343. $output = $pali.'၊'.$meaning;
  344. break;
  345. case 'tex':
  346. $output = $pali.'၊'.$meaning;
  347. break;
  348. default:
  349. $output = $pali.'၊'.$meaning;
  350. break;
  351. }
  352. return $output;
  353. }
  354. private function render_exercise(){
  355. $id = $this->get_param($this->param,"id",1);
  356. $title = $this->get_param($this->param,"title",1);
  357. $props = [
  358. "id" => $id,
  359. "title" => $title,
  360. "channel" => $this->channel_id[0],
  361. ];
  362. switch ($this->format) {
  363. case 'react':
  364. $output = [
  365. 'props'=>base64_encode(\json_encode($props)),
  366. 'html'=>"",
  367. 'tag'=>'span',
  368. 'tpl'=>'exercise',
  369. ];
  370. break;
  371. case 'unity':
  372. $output = [
  373. 'props'=>base64_encode(\json_encode($props)),
  374. 'tpl'=>'exercise',
  375. ];
  376. break;
  377. case 'text':
  378. $output = $title;
  379. break;
  380. case 'tex':
  381. $output = $title;
  382. break;
  383. default:
  384. $output = '';
  385. break;
  386. }
  387. return $output;
  388. }
  389. private function render_article(){
  390. $type = $this->get_param($this->param,"type",1);
  391. $id = $this->get_param($this->param,"id",2);
  392. $title = $this->get_param($this->param,"title",3);
  393. $channel = $this->get_param($this->param,"channel",4,$this->channel_id[0]);
  394. $style = $this->get_param($this->param,"style",5);
  395. $props = [
  396. "type" => $type,
  397. "id" => $id,
  398. "channel" => $channel,
  399. 'style' => $style,
  400. ];
  401. if(!empty($title)){
  402. $props['title'] = $title;
  403. }
  404. switch ($this->format) {
  405. case 'react':
  406. $output = [
  407. 'props'=>base64_encode(\json_encode($props)),
  408. 'html'=>"",
  409. 'text'=>$title,
  410. 'tag'=>'span',
  411. 'tpl'=>'article',
  412. ];
  413. break;
  414. case 'unity':
  415. $output = [
  416. 'props'=>base64_encode(\json_encode($props)),
  417. 'tpl'=>'article',
  418. ];
  419. break;
  420. case 'text':
  421. $output = $title;
  422. break;
  423. case 'tex':
  424. $output = $title;
  425. break;
  426. default:
  427. $output = '';
  428. break;
  429. }
  430. return $output;
  431. }
  432. private function render_quote(){
  433. $paraId = $this->get_param($this->param,"para",1);
  434. $channelId = $this->channel_id[0];
  435. $props = RedisClusters::remember("/quote/{$channelId}/{$paraId}",
  436. config('mint.cache.expire'),
  437. function() use($paraId,$channelId){
  438. $para = \explode('-',$paraId);
  439. $output = [
  440. "paraId" => $paraId,
  441. "channel" => $channelId,
  442. "innerString" => $paraId,
  443. ];
  444. if(count($para)<2){
  445. return $output;
  446. }
  447. $PaliText = PaliText::where("book",$para[0])
  448. ->where("paragraph",$para[1])
  449. ->select(['toc','path'])
  450. ->first();
  451. if($PaliText){
  452. $output["pali"] = $PaliText->toc;
  453. $output["paliPath"] = \json_decode($PaliText->path);
  454. $output["innerString"]= $PaliText->toc;
  455. }
  456. return $output;
  457. });
  458. switch ($this->format) {
  459. case 'react':
  460. $output = [
  461. 'props'=>base64_encode(\json_encode($props)),
  462. 'html'=>$props["innerString"],
  463. 'tag'=>'span',
  464. 'tpl'=>'quote',
  465. ];
  466. break;
  467. case 'unity':
  468. $output = [
  469. 'props'=>base64_encode(\json_encode($props)),
  470. 'tpl'=>'quote',
  471. ];
  472. break;
  473. case 'text':
  474. $output = $props["innerString"];
  475. break;
  476. case 'tex':
  477. $output = $props["innerString"];
  478. break;
  479. default:
  480. $output = '';
  481. break;
  482. }
  483. return $output;
  484. }
  485. private function render_sent(){
  486. $sid = $this->get_param($this->param,"sid",1);
  487. $channel = $this->get_param($this->param,"channel",2);
  488. if(!empty($channel)){
  489. $channels = explode(',',$channel);
  490. }else{
  491. $channels = $this->channel_id;
  492. }
  493. $sentInfo = explode('@',trim($sid));
  494. $sentId = $sentInfo[0];
  495. if(isset($sentInfo[1])){
  496. $channels = [$sentInfo[1]];
  497. }
  498. $Sent = new CorpusController();
  499. $props = $Sent->getSentTpl($sentId,$channels,$this->mode,true);
  500. if($props === false){
  501. $props['error']="句子模版渲染错误。句子参数个数不符。应该是四个。";
  502. }
  503. if($this->mode==='read'){
  504. $tpl = "sentread";
  505. }else{
  506. $tpl = "sentedit";
  507. }
  508. switch ($this->format) {
  509. case 'react':
  510. $output = [
  511. 'props'=>base64_encode(\json_encode($props)),
  512. 'html'=>"",
  513. 'tag'=>'span',
  514. 'tpl'=>$tpl,
  515. ];
  516. break;
  517. case 'unity':
  518. $output = [
  519. 'props'=>base64_encode(\json_encode($props)),
  520. 'tpl'=>$tpl,
  521. ];
  522. break;
  523. case 'text':
  524. $output = '';
  525. if(isset($props['translation']) && is_array($props['translation'])){
  526. foreach ($props['translation'] as $key => $value) {
  527. $output .= MdRender::render($value['content'],
  528. [$value['channel']['id']],
  529. null,
  530. 'read',
  531. $value['channel']['type'],
  532. 'translation',
  533. 'text'
  534. );
  535. }
  536. }
  537. break;
  538. case 'tex':
  539. $output = '';
  540. if(isset($props['translation']) && is_array($props['translation'])){
  541. foreach ($props['translation'] as $key => $value) {
  542. $output .= MdRender::render($value['content'],
  543. [$value['channel']['id']],
  544. null,
  545. 'read',
  546. $value['channel']['type'],
  547. 'translation',
  548. 'tex'
  549. );
  550. }
  551. }
  552. break;
  553. default:
  554. $output = '';
  555. break;
  556. }
  557. return $output;
  558. }
  559. private function render_mermaid(){
  560. $text = json_decode(base64_decode($this->get_param($this->param,"text",1)));
  561. $props = ["text" => implode("\n",$text)];
  562. switch ($this->format) {
  563. case 'react':
  564. $output = [
  565. 'props'=>base64_encode(\json_encode($props)),
  566. 'html'=>"mermaid",
  567. 'tag'=>'div',
  568. 'tpl'=>'mermaid',
  569. ];
  570. break;
  571. case 'unity':
  572. $output = [
  573. 'props'=>base64_encode(\json_encode($props)),
  574. 'tpl'=>'mermaid',
  575. ];
  576. break;
  577. case 'text':
  578. $output = 'mermaid';
  579. break;
  580. case 'tex':
  581. $output = 'mermaid';
  582. break;
  583. default:
  584. $output = 'mermaid';
  585. break;
  586. }
  587. return $output;
  588. }
  589. private function get_param(array $param,string $name,int $id,string $default=''){
  590. if(isset($param[$name])){
  591. return trim($param[$name]);
  592. }else if(isset($param["{$id}"])){
  593. return trim($param["{$id}"]);
  594. }else{
  595. return $default;
  596. }
  597. }
  598. }