TemplateRender.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. case 'simple':
  247. if(isset($props["meaning"])){
  248. $output = $props["meaning"];
  249. }else{
  250. $output = $props["word"];
  251. }
  252. break;
  253. default:
  254. if(isset($props["meaning"])){
  255. $output = $props["meaning"];
  256. }else{
  257. $output = $props["word"];
  258. }
  259. break;
  260. }
  261. return $output;
  262. }
  263. private function render_note(){
  264. $note = $this->get_param($this->param,"text",1);
  265. $trigger = $this->get_param($this->param,"trigger",2);
  266. $props = ["note" => $note ];
  267. $innerString = "";
  268. if(!empty($trigger)){
  269. $props["trigger"] = $trigger;
  270. $innerString = $props["trigger"];
  271. }
  272. if($this->format==='unity'){
  273. $props["note"] = MdRender::render($props["note"],
  274. $this->channel_id,
  275. null,
  276. 'read',
  277. 'translation',
  278. 'markdown',
  279. 'unity'
  280. );
  281. }
  282. $output = $note;
  283. switch ($this->format) {
  284. case 'react':
  285. $output=[
  286. 'props'=>base64_encode(\json_encode($props)),
  287. 'html'=>$innerString,
  288. 'tag'=>'span',
  289. 'tpl'=>'note',
  290. ];
  291. break;
  292. case 'unity':
  293. $output=[
  294. 'props'=>base64_encode(\json_encode($props)),
  295. 'tpl'=>'note',
  296. ];
  297. break;
  298. case 'html':
  299. if(isset($GLOBALS['note_sn'])){
  300. $GLOBALS['note_sn']++;
  301. }else{
  302. $GLOBALS['note_sn'] = 1;
  303. $GLOBALS['note'] = array();
  304. }
  305. $GLOBALS['note'][] = [
  306. 'sn' => 1,
  307. 'trigger' => $trigger,
  308. 'content' => MdRender::render($props["note"],
  309. $this->channel_id,
  310. null,
  311. 'read',
  312. 'translation',
  313. 'markdown',
  314. 'html'
  315. ),
  316. ];
  317. $link="<a href='#footnote-".$GLOBALS['note_sn']."' name='note-".$GLOBALS['note_sn']."'>";
  318. if(empty($trigger)){
  319. $output = $link. "<sup>[" . $GLOBALS['note_sn'] . "]</sup></a>";
  320. }else{
  321. $output = $link . $trigger . "</a>";
  322. }
  323. break;
  324. case 'text':
  325. $output = $trigger;
  326. break;
  327. case 'tex':
  328. $output = $trigger;
  329. break;
  330. case 'simple':
  331. $output = '';
  332. break;
  333. default:
  334. $output = '';
  335. break;
  336. }
  337. return $output;
  338. }
  339. private function render_nissaya(){
  340. $pali = $this->get_param($this->param,"pali",1);
  341. $meaning = $this->get_param($this->param,"meaning",2);
  342. $innerString = "";
  343. $props = [
  344. "pali" => $pali,
  345. "meaning" => $meaning,
  346. ];
  347. switch ($this->format) {
  348. case 'react':
  349. $output = [
  350. 'props'=>base64_encode(\json_encode($props)),
  351. 'html'=>$innerString,
  352. 'tag'=>'span',
  353. 'tpl'=>'nissaya',
  354. ];
  355. break;
  356. case 'unity':
  357. $output = [
  358. 'props'=>base64_encode(\json_encode($props)),
  359. 'tpl'=>'nissaya',
  360. ];
  361. break;
  362. case 'text':
  363. $output = $pali.'၊'.$meaning;
  364. break;
  365. case 'tex':
  366. $output = $pali.'၊'.$meaning;
  367. break;
  368. case 'simple':
  369. $output = $pali.'၊'.$meaning;
  370. break;
  371. default:
  372. $output = $pali.'၊'.$meaning;
  373. break;
  374. }
  375. return $output;
  376. }
  377. private function render_exercise(){
  378. $id = $this->get_param($this->param,"id",1);
  379. $title = $this->get_param($this->param,"title",1);
  380. $props = [
  381. "id" => $id,
  382. "title" => $title,
  383. "channel" => $this->channel_id[0],
  384. ];
  385. switch ($this->format) {
  386. case 'react':
  387. $output = [
  388. 'props'=>base64_encode(\json_encode($props)),
  389. 'html'=>"",
  390. 'tag'=>'span',
  391. 'tpl'=>'exercise',
  392. ];
  393. break;
  394. case 'unity':
  395. $output = [
  396. 'props'=>base64_encode(\json_encode($props)),
  397. 'tpl'=>'exercise',
  398. ];
  399. break;
  400. case 'text':
  401. $output = $title;
  402. break;
  403. case 'tex':
  404. $output = $title;
  405. break;
  406. case 'simple':
  407. $output = $title;
  408. break;
  409. default:
  410. $output = '';
  411. break;
  412. }
  413. return $output;
  414. }
  415. private function render_article(){
  416. $type = $this->get_param($this->param,"type",1);
  417. $id = $this->get_param($this->param,"id",2);
  418. $title = $this->get_param($this->param,"title",3);
  419. $channel = $this->get_param($this->param,"channel",4,$this->channel_id[0]);
  420. $style = $this->get_param($this->param,"style",5);
  421. $props = [
  422. "type" => $type,
  423. "id" => $id,
  424. "channel" => $channel,
  425. 'style' => $style,
  426. ];
  427. if(!empty($title)){
  428. $props['title'] = $title;
  429. }
  430. switch ($this->format) {
  431. case 'react':
  432. $output = [
  433. 'props'=>base64_encode(\json_encode($props)),
  434. 'html'=>"",
  435. 'text'=>$title,
  436. 'tag'=>'span',
  437. 'tpl'=>'article',
  438. ];
  439. break;
  440. case 'unity':
  441. $output = [
  442. 'props'=>base64_encode(\json_encode($props)),
  443. 'tpl'=>'article',
  444. ];
  445. break;
  446. case 'text':
  447. $output = $title;
  448. break;
  449. case 'tex':
  450. $output = $title;
  451. break;
  452. case 'simple':
  453. $output = $title;
  454. break;
  455. default:
  456. $output = '';
  457. break;
  458. }
  459. return $output;
  460. }
  461. private function render_quote(){
  462. $paraId = $this->get_param($this->param,"para",1);
  463. $channelId = $this->channel_id[0];
  464. $props = RedisClusters::remember("/quote/{$channelId}/{$paraId}",
  465. config('mint.cache.expire'),
  466. function() use($paraId,$channelId){
  467. $para = \explode('-',$paraId);
  468. $output = [
  469. "paraId" => $paraId,
  470. "channel" => $channelId,
  471. "innerString" => $paraId,
  472. ];
  473. if(count($para)<2){
  474. return $output;
  475. }
  476. $PaliText = PaliText::where("book",$para[0])
  477. ->where("paragraph",$para[1])
  478. ->select(['toc','path'])
  479. ->first();
  480. if($PaliText){
  481. $output["pali"] = $PaliText->toc;
  482. $output["paliPath"] = \json_decode($PaliText->path);
  483. $output["innerString"]= $PaliText->toc;
  484. }
  485. return $output;
  486. });
  487. switch ($this->format) {
  488. case 'react':
  489. $output = [
  490. 'props'=>base64_encode(\json_encode($props)),
  491. 'html'=>$props["innerString"],
  492. 'tag'=>'span',
  493. 'tpl'=>'quote',
  494. ];
  495. break;
  496. case 'unity':
  497. $output = [
  498. 'props'=>base64_encode(\json_encode($props)),
  499. 'tpl'=>'quote',
  500. ];
  501. break;
  502. case 'text':
  503. $output = $props["innerString"];
  504. break;
  505. case 'tex':
  506. $output = $props["innerString"];
  507. break;
  508. case 'simple':
  509. $output = $props["innerString"];
  510. break;
  511. default:
  512. $output = $props["innerString"];
  513. break;
  514. }
  515. return $output;
  516. }
  517. private function render_sent(){
  518. $sid = $this->get_param($this->param,"sid",1);
  519. $channel = $this->get_param($this->param,"channel",2);
  520. if(!empty($channel)){
  521. $channels = explode(',',$channel);
  522. }else{
  523. $channels = $this->channel_id;
  524. }
  525. $sentInfo = explode('@',trim($sid));
  526. $sentId = $sentInfo[0];
  527. if(isset($sentInfo[1])){
  528. $channels = [$sentInfo[1]];
  529. }
  530. $Sent = new CorpusController();
  531. $props = $Sent->getSentTpl($sentId,$channels,
  532. $this->mode,true,
  533. $this->format);
  534. if($props === false){
  535. $props['error']="句子模版渲染错误。句子参数个数不符。应该是四个。";
  536. }
  537. if($this->mode==='read'){
  538. $tpl = "sentread";
  539. }else{
  540. $tpl = "sentedit";
  541. }
  542. switch ($this->format) {
  543. case 'react':
  544. $output = [
  545. 'props'=>base64_encode(\json_encode($props)),
  546. 'html'=>"",
  547. 'tag'=>'span',
  548. 'tpl'=>$tpl,
  549. ];
  550. break;
  551. case 'unity':
  552. $output = [
  553. 'props'=>base64_encode(\json_encode($props)),
  554. 'tpl'=>$tpl,
  555. ];
  556. break;
  557. case 'text':
  558. $output = '';
  559. if(isset($props['translation']) && is_array($props['translation'])){
  560. foreach ($props['translation'] as $key => $value) {
  561. $output .= $value['html'];
  562. }
  563. }
  564. break;
  565. case 'tex':
  566. $output = '';
  567. if(isset($props['translation']) && is_array($props['translation'])){
  568. foreach ($props['translation'] as $key => $value) {
  569. $output .= $value['html'];
  570. }
  571. }
  572. break;
  573. case 'simple':
  574. $output = '';
  575. if(isset($props['translation']) &&
  576. is_array($props['translation']) &&
  577. count($props['translation']) > 0
  578. ){
  579. $sentences = $props['translation'];
  580. foreach ($sentences as $key => $value) {
  581. $output .= $value['html'];
  582. }
  583. }
  584. if(empty($output)){
  585. if(isset($props['origin']) &&
  586. is_array($props['origin']) &&
  587. count($props['origin']) > 0
  588. ){
  589. $sentences = $props['origin'];
  590. foreach ($sentences as $key => $value) {
  591. $output .= $value['html'];
  592. }
  593. }
  594. }
  595. break;
  596. default:
  597. $output = '';
  598. break;
  599. }
  600. return $output;
  601. }
  602. private function render_mermaid(){
  603. $text = json_decode(base64_decode($this->get_param($this->param,"text",1)));
  604. $props = ["text" => implode("\n",$text)];
  605. switch ($this->format) {
  606. case 'react':
  607. $output = [
  608. 'props'=>base64_encode(\json_encode($props)),
  609. 'html'=>"mermaid",
  610. 'tag'=>'div',
  611. 'tpl'=>'mermaid',
  612. ];
  613. break;
  614. case 'unity':
  615. $output = [
  616. 'props'=>base64_encode(\json_encode($props)),
  617. 'tpl'=>'mermaid',
  618. ];
  619. break;
  620. case 'text':
  621. $output = 'mermaid';
  622. break;
  623. case 'tex':
  624. $output = 'mermaid';
  625. break;
  626. case 'simple':
  627. $output = 'mermaid';
  628. break;
  629. default:
  630. $output = 'mermaid';
  631. break;
  632. }
  633. return $output;
  634. }
  635. private function get_param(array $param,string $name,int $id,string $default=''){
  636. if(isset($param[$name])){
  637. return trim($param[$name]);
  638. }else if(isset($param["{$id}"])){
  639. return trim($param["{$id}"]);
  640. }else{
  641. return $default;
  642. }
  643. }
  644. }