TemplateRender.php 25 KB

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