TemplateRender.php 30 KB

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