TemplateRender.php 29 KB

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