TemplateRender.php 28 KB

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