TemplateRender.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. use Illuminate\Support\Str;
  13. class TemplateRender{
  14. protected $param = [];
  15. protected $mode = "read";
  16. protected $channel_id = [];
  17. protected $debug = [];
  18. protected $format = 'react';
  19. protected $studioId = null;
  20. /**
  21. * Create a new command instance.
  22. * string $mode 'read' | 'edit'
  23. * string $format 'react' | 'text' | 'tex' | 'unity'
  24. * @return void
  25. */
  26. public function __construct($param, $channelInfo, $mode,$format='react',$studioId,$debug=[])
  27. {
  28. $this->param = $param;
  29. foreach ($channelInfo as $value) {
  30. $this->channel_id[] = $value->uid;
  31. }
  32. $this->channelInfo = $channelInfo;
  33. $this->mode = $mode;
  34. $this->format = $format;
  35. $this->studioId = $studioId;
  36. $this->debug = $debug;
  37. }
  38. private function info($message,$debug){
  39. if(in_array($debug,$this->debug)){
  40. Log::info($message);
  41. }
  42. }
  43. private function error($message,$debug){
  44. if(in_array($debug,$this->debug)){
  45. Log::error($message);
  46. }
  47. }
  48. public function render($tpl_name){
  49. switch ($tpl_name) {
  50. case 'term':
  51. # 术语
  52. $result = $this->render_term();
  53. break;
  54. case 'note':
  55. $result = $this->render_note();
  56. break;
  57. case 'sent':
  58. $result = $this->render_sent();
  59. break;
  60. case 'quote':
  61. $result = $this->render_quote();
  62. break;
  63. case 'exercise':
  64. $result = $this->render_exercise();
  65. break;
  66. case 'article':
  67. $result = $this->render_article();
  68. break;
  69. case 'nissaya':
  70. $result = $this->render_nissaya();
  71. break;
  72. case 'mermaid':
  73. $result = $this->render_mermaid();
  74. break;
  75. default:
  76. # code...
  77. $result = [
  78. 'props'=>base64_encode(\json_encode([])),
  79. 'html'=>'',
  80. 'tag'=>'span',
  81. 'tpl'=>'unknown',
  82. ];
  83. break;
  84. }
  85. return $result;
  86. }
  87. public function getTermProps($word,$channelId,$channelInfo){
  88. if(Str::isUuid($channelId)){
  89. $lang = Channel::where('uid',$channelId)->value('lang');
  90. if(!empty($lang)){
  91. $langFamily = explode('-',$lang)[0];
  92. }else{
  93. $langFamily = 'zh';
  94. }
  95. $this->info("term:{$word} 先查属于这个channel 的",'term');
  96. $this->info('channel id'.$channelId,'term');
  97. $tplParam = DhammaTerm::where("word",$word)
  98. ->where('channal',$channelId)
  99. ->orderBy('updated_at','desc')
  100. ->first();
  101. $studioId = $channelInfo->owner_uid;
  102. }else{
  103. $tplParam = false;
  104. $lang = '';
  105. $langFamily = '';
  106. $studioId = $this->studioId;
  107. }
  108. if(!$tplParam){
  109. if(Str::isUuid($studioId)){
  110. /**
  111. * 没有,再查这个studio的
  112. * 按照语言过滤
  113. * 完全匹配的优先
  114. * 语族匹配也行
  115. */
  116. $this->info("没有-再查这个studio的",'term');
  117. $termsInStudio = DhammaTerm::where("word",$word)
  118. ->where('owner',$channelInfo->owner_uid)
  119. ->orderBy('updated_at','desc')
  120. ->get();
  121. if(count($termsInStudio)>0){
  122. $list = array();
  123. foreach ($termsInStudio as $key=>$term) {
  124. if(empty($term->channal)){
  125. if($term->language===$lang){
  126. $list[$term->guid]=2;
  127. }else if(strpos($term->language,$langFamily) !== false){
  128. $list[$term->guid]=1;
  129. }
  130. }
  131. }
  132. if(count($list)>0){
  133. arsort($list);
  134. foreach ($list as $key => $one) {
  135. foreach ($termsInStudio as $term) {
  136. if($term->guid===$key){
  137. $tplParam = $term;
  138. break;
  139. }
  140. }
  141. break;
  142. }
  143. }
  144. }
  145. }
  146. }
  147. if(!$tplParam){
  148. $this->info("没有,再查社区",'term');
  149. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  150. $tplParam = DhammaTerm::where("word",$word)
  151. ->where('channal',$community_channel)
  152. ->first();
  153. if($tplParam){
  154. $isCommunity = true;
  155. }else{
  156. $this->info("查社区没有",'term');
  157. }
  158. }
  159. $output = [
  160. "word" => $word,
  161. "parentChannelId" => $channelId,
  162. "parentStudioId" => $channelInfo->owner_uid,
  163. ];
  164. $innerString = $output["word"];
  165. if($tplParam){
  166. $output["id"] = $tplParam->guid;
  167. $output["meaning"] = $tplParam->meaning;
  168. $output["channel"] = $tplParam->channal;
  169. if(isset($isCommunity)){
  170. $output["isCommunity"] = true;
  171. }
  172. $innerString = "{$output["meaning"]}({$output["word"]})";
  173. if(!empty($tplParam->other_meaning)){
  174. $output["meaning2"] = $tplParam->other_meaning;
  175. }
  176. }
  177. $output['innerHtml'] = $innerString;
  178. return $output;
  179. }
  180. private function render_term(){
  181. $word = $this->get_param($this->param,"word",1);
  182. if(count($this->channel_id)>0){
  183. $channelId = $this->channel_id[0];
  184. }else{
  185. $channelId = null;
  186. }
  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 'html':
  566. $output = '';
  567. if(isset($props['translation']) && is_array($props['translation'])){
  568. foreach ($props['translation'] as $key => $value) {
  569. $output .= '<span class="sentence">'.$value['html'].'</span>';
  570. }
  571. }
  572. break;
  573. case 'tex':
  574. $output = '';
  575. if(isset($props['translation']) && is_array($props['translation'])){
  576. foreach ($props['translation'] as $key => $value) {
  577. $output .= $value['html'];
  578. }
  579. }
  580. break;
  581. case 'simple':
  582. $output = '';
  583. if(isset($props['translation']) &&
  584. is_array($props['translation']) &&
  585. count($props['translation']) > 0
  586. ){
  587. $sentences = $props['translation'];
  588. foreach ($sentences as $key => $value) {
  589. $output .= $value['html'];
  590. }
  591. }
  592. if(empty($output)){
  593. if(isset($props['origin']) &&
  594. is_array($props['origin']) &&
  595. count($props['origin']) > 0
  596. ){
  597. $sentences = $props['origin'];
  598. foreach ($sentences as $key => $value) {
  599. $output .= $value['html'];
  600. }
  601. }
  602. }
  603. break;
  604. default:
  605. $output = '';
  606. break;
  607. }
  608. return $output;
  609. }
  610. private function render_mermaid(){
  611. $text = json_decode(base64_decode($this->get_param($this->param,"text",1)));
  612. $props = ["text" => implode("\n",$text)];
  613. switch ($this->format) {
  614. case 'react':
  615. $output = [
  616. 'props'=>base64_encode(\json_encode($props)),
  617. 'html'=>"mermaid",
  618. 'tag'=>'div',
  619. 'tpl'=>'mermaid',
  620. ];
  621. break;
  622. case 'unity':
  623. $output = [
  624. 'props'=>base64_encode(\json_encode($props)),
  625. 'tpl'=>'mermaid',
  626. ];
  627. break;
  628. case 'text':
  629. $output = 'mermaid';
  630. break;
  631. case 'tex':
  632. $output = 'mermaid';
  633. break;
  634. case 'simple':
  635. $output = 'mermaid';
  636. break;
  637. default:
  638. $output = 'mermaid';
  639. break;
  640. }
  641. return $output;
  642. }
  643. private function get_param(array $param,string $name,int $id,string $default=''){
  644. if(isset($param[$name])){
  645. return trim($param[$name]);
  646. }else if(isset($param["{$id}"])){
  647. return trim($param["{$id}"]);
  648. }else{
  649. return $default;
  650. }
  651. }
  652. }