TemplateRender.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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\Models\Discussion;
  12. use App\Http\Controllers\CorpusController;
  13. use App\Tools\RedisClusters;
  14. use App\Http\Api\ChannelApi;
  15. use App\Http\Api\MdRender;
  16. use App\Http\Api\PaliTextApi;
  17. class TemplateRender{
  18. protected $param = [];
  19. protected $mode = "read";
  20. protected $channel_id = [];
  21. protected $debug = [];
  22. protected $format = 'react';
  23. protected $studioId = null;
  24. protected $lang = 'en';
  25. protected $langFamily = 'en';
  26. /**
  27. * Create a new command instance.
  28. * string $mode 'read' | 'edit'
  29. * string $format 'react' | 'text' | 'tex' | 'unity'
  30. * @return void
  31. */
  32. public function __construct($param, $channelInfo, $mode,$format='react',$studioId='',$debug=[],$lang='zh-Hans')
  33. {
  34. $this->param = $param;
  35. foreach ($channelInfo as $value) {
  36. $this->channel_id[] = $value->uid;
  37. }
  38. $this->channelInfo = $channelInfo;
  39. $this->mode = $mode;
  40. $this->format = $format;
  41. $this->studioId = $studioId;
  42. $this->debug = $debug;
  43. $this->glossaryKey = 'glossary';
  44. if(count($this->channel_id)>0){
  45. $channelId = $this->channel_id[0];
  46. if(Str::isUuid($channelId)){
  47. $lang = Channel::where('uid',$channelId)->value('lang');
  48. }
  49. }
  50. if(!empty($lang)){
  51. $this->lang = $lang;
  52. $this->langFamily = explode('-',$lang)[0];
  53. }
  54. }
  55. public function glossaryKey(){
  56. return $this->glossaryKey;
  57. }
  58. /**
  59. * TODO 设置默认语言。在渲染某些内容的时候需要语言信息
  60. */
  61. public function setLang($lang){
  62. $this->lang = $lang;
  63. $this->langFamily = explode('-',$lang)[0];
  64. }
  65. private function info($message,$debug){
  66. if(in_array($debug,$this->debug)){
  67. Log::info($message);
  68. }
  69. }
  70. private function error($message,$debug){
  71. if(in_array($debug,$this->debug)){
  72. Log::error($message);
  73. }
  74. }
  75. public function render($tpl_name){
  76. switch ($tpl_name) {
  77. case 'term':
  78. # 术语
  79. $result = $this->render_term();
  80. break;
  81. case 'note':
  82. $result = $this->render_note();
  83. break;
  84. case 'sent':
  85. $result = $this->render_sent();
  86. break;
  87. case 'quote':
  88. $result = $this->render_quote();
  89. break;
  90. case 'ql':
  91. $result = $this->render_quote_link();
  92. break;
  93. case 'exercise':
  94. $result = $this->render_exercise();
  95. break;
  96. case 'article':
  97. $result = $this->render_article();
  98. break;
  99. case 'nissaya':
  100. $result = $this->render_nissaya();
  101. break;
  102. case 'mermaid':
  103. $result = $this->render_mermaid();
  104. break;
  105. case 'qa':
  106. $result = $this->render_qa();
  107. break;
  108. case 'v':
  109. $result = $this->render_video();
  110. break;
  111. case 'g':
  112. $result = $this->render_grammar_lookup();
  113. break;
  114. default:
  115. # code...
  116. $result = [
  117. 'props'=>base64_encode(\json_encode([])),
  118. 'html'=>'',
  119. 'tag'=>'span',
  120. 'tpl'=>'unknown',
  121. ];
  122. break;
  123. }
  124. return $result;
  125. }
  126. public function getTermProps($word,$tag=null,$channel=null){
  127. if($channel && !empty($channel)){
  128. $channelId = $channel;
  129. }else{
  130. if(count($this->channel_id)>0){
  131. $channelId = $this->channel_id[0];
  132. }else{
  133. $channelId = null;
  134. }
  135. }
  136. if(count($this->channelInfo)===0){
  137. if(!empty($channel)){
  138. $channelInfo = Channel::where('uid',$channel)->first();
  139. if(!$channelInfo){
  140. unset($channelInfo);
  141. }
  142. }
  143. if(!isset($channelInfo)){
  144. Log::error('channel is null');
  145. $output = [
  146. "word" => $word,
  147. 'innerHtml' => '',
  148. ];
  149. return $output;
  150. }
  151. }else{
  152. $channelInfo = $this->channelInfo[0];
  153. }
  154. if(Str::isUuid($channelId)){
  155. $lang = Channel::where('uid',$channelId)->value('lang');
  156. if(!empty($lang)){
  157. $langFamily = explode('-',$lang)[0];
  158. }else{
  159. $langFamily = 'zh';
  160. }
  161. $this->info("term:{$word} 先查属于这个channel 的",'term');
  162. $this->info('channel id'.$channelId,'term');
  163. $table = DhammaTerm::where("word",$word)
  164. ->where('channal',$channelId);
  165. if($tag && !empty($tag)){
  166. $table = $table->where('tag',$tag);
  167. }
  168. $tplParam = $table->orderBy('updated_at','desc')
  169. ->first();
  170. $studioId = $channelInfo->owner_uid;
  171. }else{
  172. $tplParam = false;
  173. $lang = '';
  174. $langFamily = '';
  175. $studioId = $this->studioId;
  176. }
  177. if(!$tplParam){
  178. if(Str::isUuid($studioId)){
  179. /**
  180. * 没有,再查这个studio的
  181. * 按照语言过滤
  182. * 完全匹配的优先
  183. * 语族匹配也行
  184. */
  185. $this->info("没有-再查这个studio的",'term');
  186. $table = DhammaTerm::where("word",$word);
  187. if(!empty($tag)){
  188. $table = $table->where('tag',$tag);
  189. }
  190. $termsInStudio = $table->where('owner',$channelInfo->owner_uid)
  191. ->orderBy('updated_at','desc')
  192. ->get();
  193. if(count($termsInStudio)>0){
  194. $list = array();
  195. foreach ($termsInStudio as $key=>$term) {
  196. if(empty($term->channal)){
  197. if($term->language===$lang){
  198. $list[$term->guid]=2;
  199. }else if(strpos($term->language,$langFamily) !== false){
  200. $list[$term->guid]=1;
  201. }
  202. }
  203. }
  204. if(count($list)>0){
  205. arsort($list);
  206. foreach ($list as $key => $one) {
  207. foreach ($termsInStudio as $term) {
  208. if($term->guid===$key){
  209. $tplParam = $term;
  210. break;
  211. }
  212. }
  213. break;
  214. }
  215. }
  216. }
  217. }
  218. }
  219. if(!$tplParam){
  220. $this->info("没有,再查社区",'term');
  221. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  222. $table = DhammaTerm::where("word",$word);
  223. if(!empty($tag)){
  224. $table = $table->where('tag',$tag);
  225. }
  226. $tplParam = $table->where('channal',$community_channel)
  227. ->first();
  228. if($tplParam){
  229. $isCommunity = true;
  230. }else{
  231. $this->info("查社区没有",'term');
  232. }
  233. }
  234. $output = [
  235. "word" => $word,
  236. "parentChannelId" => $channelId,
  237. "parentStudioId" => $channelInfo->owner_uid,
  238. ];
  239. $innerString = $output["word"];
  240. if($tplParam){
  241. $output["id"] = $tplParam->guid;
  242. $output["meaning"] = $tplParam->meaning;
  243. $output["channel"] = $tplParam->channal;
  244. if(!empty($tplParam->note)){
  245. $mdRender = new MdRender(['format'=>$this->format]);
  246. $output['note'] = $mdRender->convert($tplParam->note,$this->channel_id);
  247. }
  248. if(isset($isCommunity)){
  249. $output["isCommunity"] = true;
  250. }
  251. $innerString = "{$output["meaning"]}({$output["word"]})";
  252. if(!empty($tplParam->other_meaning)){
  253. $output["meaning2"] = $tplParam->other_meaning;
  254. }
  255. }
  256. $output['innerHtml'] = $innerString;
  257. return $output;
  258. }
  259. private function render_term(){
  260. $word = $this->get_param($this->param,"word",1);
  261. $props = $this->getTermProps($word,'');
  262. //$key = "/term/{$channelId}/{$word}";
  263. $output = $props['word'];
  264. switch ($this->format) {
  265. case 'react':
  266. $output=[
  267. 'props'=>base64_encode(\json_encode($props)),
  268. 'html'=>$props['innerHtml'],
  269. 'tag'=>'span',
  270. 'tpl'=>'term',
  271. ];
  272. break;
  273. case 'unity':
  274. $output=[
  275. 'props'=>base64_encode(\json_encode($props)),
  276. 'tpl'=>'term',
  277. ];
  278. break;
  279. case 'html':
  280. if(isset($props["meaning"])){
  281. $GLOBALS[$this->glossaryKey][$props["word"]] = $props['meaning'];
  282. $key = 'term-'.$props["word"];
  283. $termHead = "<a href='#'>".$props['meaning']."</a>";
  284. if(isset($GLOBALS[$key])){
  285. $output = $termHead;
  286. }else{
  287. $GLOBALS[$key] = 1;
  288. $output = $termHead.'(<em>'.$props["word"].'</em>)';
  289. }
  290. }else{
  291. $output = $props["word"];
  292. }
  293. break;
  294. case 'text':
  295. if(isset($props["meaning"])){
  296. $key = 'term-'.$props["word"];
  297. if(isset($GLOBALS[$key])){
  298. $output = $props["meaning"];
  299. }else{
  300. $GLOBALS[$key] = 1;
  301. $output = $props["meaning"].'('.$props["word"].')';
  302. }
  303. }else{
  304. $output = $props["word"];
  305. }
  306. break;
  307. case 'tex':
  308. if(isset($props["meaning"])){
  309. $key = 'term-'.$props["word"];
  310. if(isset($GLOBALS[$key])){
  311. $output = $props["meaning"];
  312. }else{
  313. $GLOBALS[$key] = 1;
  314. $output = $props["meaning"].'('.$props["word"].')';
  315. }
  316. }else{
  317. $output = $props["word"];
  318. }
  319. break;
  320. case 'simple':
  321. if(isset($props["meaning"])){
  322. $output = $props["meaning"];
  323. }else{
  324. $output = $props["word"];
  325. }
  326. break;
  327. case 'markdown':
  328. if(isset($props["meaning"])){
  329. $key = 'term-'.$props["word"];
  330. if(isset($GLOBALS[$key])){
  331. $output = $props["meaning"];
  332. }else{
  333. $GLOBALS[$key] = 1;
  334. $output = $props["meaning"].'('.$props["word"].')';
  335. }
  336. }else{
  337. $output = $props["word"];
  338. }
  339. //如果有内容,显示为脚注
  340. if(!empty($props["note"])){
  341. if(isset($GLOBALS['note_sn'])){
  342. $GLOBALS['note_sn']++;
  343. }else{
  344. $GLOBALS['note_sn'] = 1;
  345. $GLOBALS['note'] = array();
  346. }
  347. $content = $props["note"];
  348. $output .= '[^'.$GLOBALS['note_sn'].']';
  349. $GLOBALS['note'][] = [
  350. 'sn' => $GLOBALS['note_sn'],
  351. 'trigger' => '',
  352. 'content' => $content,
  353. ];
  354. }
  355. break;
  356. default:
  357. if(isset($props["meaning"])){
  358. $output = $props["meaning"];
  359. }else{
  360. $output = $props["word"];
  361. }
  362. break;
  363. }
  364. return $output;
  365. }
  366. private function render_note(){
  367. $note = $this->get_param($this->param,"text",1);
  368. $trigger = $this->get_param($this->param,"trigger",2);
  369. $props = ["note" => $note ];
  370. $innerString = "";
  371. if(!empty($trigger)){
  372. $props["trigger"] = $trigger;
  373. $innerString = $props["trigger"];
  374. }
  375. if($this->format==='unity'){
  376. $props["note"] = MdRender::render($props["note"],
  377. $this->channel_id,
  378. null,
  379. 'read',
  380. 'translation',
  381. 'markdown',
  382. 'unity'
  383. );
  384. }
  385. $output = $note;
  386. switch ($this->format) {
  387. case 'react':
  388. $output=[
  389. 'props'=>base64_encode(\json_encode($props)),
  390. 'html'=>$innerString,
  391. 'tag'=>'span',
  392. 'tpl'=>'note',
  393. ];
  394. break;
  395. case 'unity':
  396. $output=[
  397. 'props'=>base64_encode(\json_encode($props)),
  398. 'tpl'=>'note',
  399. ];
  400. break;
  401. case 'html':
  402. if(isset($GLOBALS['note_sn'])){
  403. $GLOBALS['note_sn']++;
  404. }else{
  405. $GLOBALS['note_sn'] = 1;
  406. $GLOBALS['note'] = array();
  407. }
  408. $GLOBALS['note'][] = [
  409. 'sn' => $GLOBALS['note_sn'],
  410. 'trigger' => $trigger,
  411. 'content' => MdRender::render($props["note"],
  412. $this->channel_id,
  413. null,
  414. 'read',
  415. 'translation',
  416. 'markdown',
  417. 'html'
  418. ),
  419. ];
  420. $link="<a href='#footnote-".$GLOBALS['note_sn']."' name='note-".$GLOBALS['note_sn']."'>";
  421. if(empty($trigger)){
  422. $output = $link. "<sup>[" . $GLOBALS['note_sn'] . "]</sup></a>";
  423. }else{
  424. $output = $link . $trigger . "</a>";
  425. }
  426. break;
  427. case 'text':
  428. $output = $trigger;
  429. break;
  430. case 'tex':
  431. $output = $trigger;
  432. break;
  433. case 'simple':
  434. $output = '';
  435. break;
  436. case 'markdown':
  437. if(isset($GLOBALS['note_sn'])){
  438. $GLOBALS['note_sn']++;
  439. }else{
  440. $GLOBALS['note_sn'] = 1;
  441. $GLOBALS['note'] = array();
  442. }
  443. $content = MdRender::render(
  444. $props["note"],
  445. $this->channel_id,
  446. null,
  447. 'read',
  448. 'translation',
  449. 'markdown',
  450. 'markdown'
  451. );
  452. $output = '[^'.$GLOBALS['note_sn'].']';
  453. $GLOBALS['note'][] = [
  454. 'sn' => $GLOBALS['note_sn'],
  455. 'trigger' => $trigger,
  456. 'content' => $content,
  457. ];
  458. //$output = '<footnote id="'.$GLOBALS['note_sn'].'">'.$content.'</footnote>';
  459. break;
  460. default:
  461. $output = '';
  462. break;
  463. }
  464. return $output;
  465. }
  466. private function render_nissaya(){
  467. $pali = $this->get_param($this->param,"pali",1);
  468. $meaning = $this->get_param($this->param,"meaning",2);
  469. $innerString = "";
  470. $props = [
  471. "pali" => $pali,
  472. "meaning" => $meaning,
  473. "lang" => $this->lang,
  474. ];
  475. switch ($this->format) {
  476. case 'react':
  477. $output = [
  478. 'props'=>base64_encode(\json_encode($props)),
  479. 'html'=>$innerString,
  480. 'tag'=>'span',
  481. 'tpl'=>'nissaya',
  482. ];
  483. break;
  484. case 'unity':
  485. $output = [
  486. 'props'=>base64_encode(\json_encode($props)),
  487. 'tpl'=>'nissaya',
  488. ];
  489. break;
  490. case 'text':
  491. $output = $pali.'၊'.$meaning;
  492. break;
  493. case 'tex':
  494. $output = $pali.'၊'.$meaning;
  495. break;
  496. case 'simple':
  497. $output = $pali.'၊'.$meaning;
  498. break;
  499. default:
  500. $output = $pali.'၊'.$meaning;
  501. break;
  502. }
  503. return $output;
  504. }
  505. private function render_exercise(){
  506. $id = $this->get_param($this->param,"id",1);
  507. $title = $this->get_param($this->param,"title",1);
  508. $props = [
  509. "id" => $id,
  510. "title" => $title,
  511. "channel" => $this->channel_id[0],
  512. ];
  513. switch ($this->format) {
  514. case 'react':
  515. $output = [
  516. 'props'=>base64_encode(\json_encode($props)),
  517. 'html'=>"",
  518. 'tag'=>'span',
  519. 'tpl'=>'exercise',
  520. ];
  521. break;
  522. case 'unity':
  523. $output = [
  524. 'props'=>base64_encode(\json_encode($props)),
  525. 'tpl'=>'exercise',
  526. ];
  527. break;
  528. case 'text':
  529. $output = $title;
  530. break;
  531. case 'tex':
  532. $output = $title;
  533. break;
  534. case 'simple':
  535. $output = $title;
  536. break;
  537. default:
  538. $output = '';
  539. break;
  540. }
  541. return $output;
  542. }
  543. private function render_article(){
  544. $type = $this->get_param($this->param,"type",1);
  545. $id = $this->get_param($this->param,"id",2);
  546. $title = $this->get_param($this->param,"title",3);
  547. $channel = $this->get_param($this->param,"channel",4);
  548. $style = $this->get_param($this->param,"style",5);
  549. $book = $this->get_param($this->param,"book",6);
  550. $paragraphs = $this->get_param($this->param,"paragraphs",7);
  551. $anthology = $this->get_param($this->param,"anthology",8);
  552. $props = [
  553. "type" => $type,
  554. "id" => $id,
  555. 'style' => $style,
  556. ];
  557. if(!empty($channel)){
  558. $props['channel'] = $channel;
  559. }
  560. if(!empty($title)){
  561. $props['title'] = $title;
  562. }
  563. if(!empty($book)){
  564. $props['book'] = $book;
  565. }
  566. if(!empty($paragraphs)){
  567. $props['paragraphs'] = $paragraphs;
  568. }
  569. if(!empty($anthology)){
  570. $props['anthology'] = $anthology;
  571. }
  572. if(is_array($this->channel_id)){
  573. $props['parentChannels'] = $this->channel_id;
  574. }
  575. switch ($this->format) {
  576. case 'react':
  577. $output = [
  578. 'props'=>base64_encode(\json_encode($props)),
  579. 'html'=>"",
  580. 'text'=>$title,
  581. 'tag'=>'span',
  582. 'tpl'=>'article',
  583. ];
  584. break;
  585. case 'unity':
  586. $output = [
  587. 'props'=>base64_encode(\json_encode($props)),
  588. 'tpl'=>'article',
  589. ];
  590. break;
  591. case 'text':
  592. $output = $title;
  593. break;
  594. case 'tex':
  595. $output = $title;
  596. break;
  597. case 'simple':
  598. $output = $title;
  599. break;
  600. default:
  601. $output = '';
  602. break;
  603. }
  604. return $output;
  605. }
  606. private function render_quote(){
  607. $paraId = $this->get_param($this->param,"para",1);
  608. $channelId = $this->channel_id[0];
  609. $props = RedisClusters::remember("/quote/{$channelId}/{$paraId}",
  610. config('mint.cache.expire'),
  611. function() use($paraId,$channelId){
  612. $para = \explode('-',$paraId);
  613. $output = [
  614. "paraId" => $paraId,
  615. "channel" => $channelId,
  616. "innerString" => $paraId,
  617. ];
  618. if(count($para)<2){
  619. return $output;
  620. }
  621. $PaliText = PaliText::where("book",$para[0])
  622. ->where("paragraph",$para[1])
  623. ->select(['toc','path'])
  624. ->first();
  625. if($PaliText){
  626. $output["pali"] = $PaliText->toc;
  627. $output["paliPath"] = \json_decode($PaliText->path);
  628. $output["innerString"]= $PaliText->toc;
  629. }
  630. return $output;
  631. });
  632. switch ($this->format) {
  633. case 'react':
  634. $output = [
  635. 'props'=>base64_encode(\json_encode($props)),
  636. 'html'=>$props["innerString"],
  637. 'tag'=>'span',
  638. 'tpl'=>'quote',
  639. ];
  640. break;
  641. case 'unity':
  642. $output = [
  643. 'props'=>base64_encode(\json_encode($props)),
  644. 'tpl'=>'quote',
  645. ];
  646. break;
  647. case 'text':
  648. $output = $props["innerString"];
  649. break;
  650. case 'tex':
  651. $output = $props["innerString"];
  652. break;
  653. case 'simple':
  654. $output = $props["innerString"];
  655. break;
  656. default:
  657. $output = $props["innerString"];
  658. break;
  659. }
  660. return $output;
  661. }
  662. private function render_quote_link(){
  663. $type = $this->get_param($this->param,"type",1);
  664. $title = $this->get_param($this->param,"title",6,'');
  665. $bookName = $this->get_param($this->param,"bookname",2,'');
  666. $volume = $this->get_param($this->param,"volume",3);
  667. $page = $this->get_param($this->param,"page",4,'');
  668. $style = $this->get_param($this->param,"style",5,'modal');
  669. $book = $this->get_param($this->param,"book",7,false);
  670. $para = $this->get_param($this->param,"para",8,false);
  671. $props = [
  672. 'type' => $type,
  673. 'style' => $style,
  674. 'found' => true,
  675. ];
  676. if(!empty($bookName) && $volume !== '' && !empty($page)){
  677. $props['bookName'] = $bookName;
  678. $props['volume'] = (int)$volume;
  679. $props['page'] = $page;
  680. $props['found'] = true;
  681. }else if($book && $para){
  682. /**
  683. * 没有指定书名,根据book para 查询
  684. */
  685. if($type==='c'){
  686. //按照章节名称显示
  687. $path = PaliTextApi::getChapterPath($book,$para);
  688. if($path){
  689. $path = json_decode($path,true);
  690. }
  691. if($path && is_array($path) && count($path)>2){
  692. $props['bookName'] = strtolower($path[0]['title']) ;
  693. $props['chapter'] = strtolower(end($path)['title']);
  694. $props['found'] = true;
  695. }else{
  696. $props['found'] = false;
  697. }
  698. }else{
  699. $pageInfo = $this->pageInfoByPara($type,$book,$para);
  700. if($pageInfo['found']){
  701. $props['bookName'] = $pageInfo['bookName'];
  702. $props['volume'] = $pageInfo['volume'];
  703. $props['page'] = $pageInfo['page'];
  704. $props['found'] = true;
  705. }else{
  706. $props['found'] = false;
  707. }
  708. }
  709. }else if($title){
  710. //没有书号用title查询
  711. //$tmpTitle = explode('။',$title);
  712. for ($i=mb_strlen($title,'UTF-8'); $i > 0 ; $i--) {
  713. $mTitle = mb_substr($title,0,$i);
  714. $has = array_search($mTitle, array_column(BookTitle::my(), 'title2'));
  715. Log::debug('run',['title'=>$mTitle,'has'=>$has]);
  716. if($has !== false){
  717. $tmpBookTitle = $mTitle;
  718. $tmpBookPage = mb_substr($title,$i);
  719. $tmpBookPage = $this->mb_trim($tmpBookPage,'၊။');
  720. break;
  721. }
  722. }
  723. if(isset($tmpBookTitle)){
  724. Log::debug('book title found',['title'=>$tmpBookTitle,'page'=>$tmpBookPage]);
  725. //$tmpBookTitle = $tmpTitle[0];
  726. //$tmpBookPage = $tmpTitle[1];
  727. $tmpBookPage = (int)str_replace(
  728. ['၁','၂','၃','၄','၅','၆','၇','၈','၉','၀'],
  729. ['1','2','3','4','5','6','7','8','9','0'],
  730. $tmpBookPage);
  731. $found_key = array_search($tmpBookTitle, array_column(BookTitle::my(), 'title2'));
  732. if($found_key !== false){
  733. $props['bookName'] = BookTitle::my()[$found_key]['bookname'];
  734. $props['volume'] = BookTitle::my()[$found_key]['volume'];
  735. $props['page'] = $tmpBookPage;
  736. if(!empty($props['bookName'])){
  737. $found_title = array_search($props['bookName'], array_column(BookTitle::my(), 'bookname'));
  738. if($found_title === false){
  739. $props['found'] = false;
  740. }
  741. }
  742. }else{
  743. //没找到,返回术语和页码
  744. $props['found'] = false;
  745. $props['bookName'] = $tmpBookTitle;
  746. $props['page'] = $tmpBookPage;
  747. $props['volume'] = 0;
  748. }
  749. }
  750. }else{
  751. Log::debug('book title not found');
  752. $props['found'] = false;
  753. }
  754. if($book && $para){
  755. $props['book'] = $book;
  756. $props['para'] = $para;
  757. }
  758. if($title){
  759. $props['title'] = $title;
  760. }
  761. $text = '';
  762. if(isset($props['bookName'])){
  763. $searchField = '';
  764. switch ($type) {
  765. case 'm':
  766. $searchField = 'm_title';
  767. break;
  768. case 'p':
  769. $searchField = 'p_title';
  770. break;
  771. }
  772. $found_title = array_search($props['bookName'], array_column(BookTitle::get(), $searchField));
  773. if($found_title === false){
  774. $props['found'] = false;
  775. }
  776. $term = $this->getTermProps($props['bookName'],':quote:');
  777. $props['term'] = $term;
  778. if(isset($term['id'])){
  779. $props['bookNameLocal'] = $term['meaning'];
  780. $text .= $term['meaning'];
  781. }else{
  782. $text .= $bookName;
  783. }
  784. }
  785. if(isset($props['volume']) && isset($props['page'])){
  786. $text .= " {$volume}.{$page}";
  787. }
  788. switch ($this->format) {
  789. case 'react':
  790. $output = [
  791. 'props'=>base64_encode(\json_encode($props)),
  792. 'html'=>'',
  793. 'tag'=>'span',
  794. 'tpl'=>'quote-link',
  795. ];
  796. break;
  797. case 'unity':
  798. $output = [
  799. 'props'=>base64_encode(\json_encode($props)),
  800. 'tpl'=>'quote-link',
  801. ];
  802. break;
  803. default:
  804. $output = $text;
  805. break;
  806. }
  807. return $output;
  808. }
  809. private function pageInfoByPara($type,$book,$para){
  810. $output = array();
  811. $pageInfo = PageNumber::where('type',strtoupper($type))
  812. ->where('book',$book)
  813. ->where('paragraph','<=',$para)
  814. ->orderBy('paragraph','desc')
  815. ->first();
  816. if($pageInfo){
  817. foreach (BookTitle::get() as $value) {
  818. if($value['id']===$pageInfo->pcd_book_id){
  819. switch (strtoupper($type)) {
  820. case 'M':
  821. $key = 'm_title';
  822. break;
  823. case 'P':
  824. $key = 'p_title';
  825. break;
  826. case 'V':
  827. $key = 'v_title';
  828. break;
  829. default:
  830. $key = 'term';
  831. break;
  832. }
  833. $output['bookName'] = $value[$key];
  834. break;
  835. }
  836. }
  837. $output['volume'] = $pageInfo->volume;
  838. $output['page'] = $pageInfo->page;
  839. $output['found'] = true;
  840. }else{
  841. $output['found'] = false;
  842. }
  843. return $output;
  844. }
  845. private function render_sent(){
  846. $sid = $this->get_param($this->param,"id",1);
  847. $channel = $this->get_param($this->param,"channel",2);
  848. $text = $this->get_param($this->param,"text",2,'both');
  849. if(!empty($channel)){
  850. $channels = explode(',',$channel);
  851. }else{
  852. $channels = $this->channel_id;
  853. }
  854. $sentInfo = explode('@',trim($sid));
  855. $sentId = $sentInfo[0];
  856. if(isset($sentInfo[1])){
  857. $channels = [$sentInfo[1]];
  858. }
  859. $Sent = new CorpusController();
  860. $props = $Sent->getSentTpl($sentId,$channels,
  861. $this->mode,true,
  862. $this->format);
  863. if($props === false){
  864. $props['error']="句子模版渲染错误。句子参数个数不符。应该是四个。";
  865. }
  866. if($this->mode==='read'){
  867. $tpl = "sentread";
  868. }else{
  869. $tpl = "sentedit";
  870. }
  871. switch ($this->format) {
  872. case 'react':
  873. $output = [
  874. 'props'=>base64_encode(\json_encode($props)),
  875. 'html'=>"",
  876. 'tag'=>'span',
  877. 'tpl'=>$tpl,
  878. ];
  879. break;
  880. case 'unity':
  881. $output = [
  882. 'props'=>base64_encode(\json_encode($props)),
  883. 'tpl'=>$tpl,
  884. ];
  885. break;
  886. case 'text':
  887. $output = '';
  888. if(isset($props['origin']) && is_array($props['origin'])){
  889. foreach ($props['origin'] as $key => $value) {
  890. $output .= $value['html'];
  891. }
  892. }
  893. if(isset($props['translation']) && is_array($props['translation'])){
  894. foreach ($props['translation'] as $key => $value) {
  895. $output .= $value['html'];
  896. }
  897. }
  898. break;
  899. case 'html':
  900. $output = '';
  901. $output .= '<span class="sentence">';
  902. if($text === 'both' || $text === 'origin'){
  903. if(isset($props['origin']) && is_array($props['origin'])){
  904. foreach ($props['origin'] as $key => $value) {
  905. $output .= '<span class="origin">'.$value['html'].'</span>';
  906. }
  907. }
  908. }
  909. if($text === 'both' || $text === 'translation'){
  910. if(isset($props['translation']) && is_array($props['translation'])){
  911. foreach ($props['translation'] as $key => $value) {
  912. $output .= '<span class="translation">'.$value['html'].'</span>';
  913. }
  914. }
  915. }
  916. $output .= '</span>';
  917. break;
  918. case 'tex':
  919. $output = '';
  920. if(isset($props['translation']) && is_array($props['translation'])){
  921. foreach ($props['translation'] as $key => $value) {
  922. $output .= $value['html'];
  923. }
  924. }
  925. break;
  926. case 'simple':
  927. $output = '';
  928. if($text === 'both' || $text === 'origin'){
  929. if(empty($output)){
  930. if(isset($props['origin']) &&
  931. is_array($props['origin']) &&
  932. count($props['origin']) > 0
  933. ){
  934. foreach ($props['origin'] as $key => $value) {
  935. $output .= $value['html'];
  936. }
  937. }
  938. }
  939. }
  940. if($text === 'both' || $text === 'translation'){
  941. if(isset($props['translation']) &&
  942. is_array($props['translation']) &&
  943. count($props['translation']) > 0
  944. ){
  945. foreach ($props['translation'] as $key => $value) {
  946. $output .= $value['html'];
  947. }
  948. }
  949. }
  950. break;
  951. case 'markdown':
  952. $output = '';
  953. if($text === 'both' || $text === 'origin'){
  954. if(isset($props['origin']) && is_array($props['origin'])){
  955. foreach ($props['origin'] as $key => $value) {
  956. $output .= "\n\n". $value['html'];
  957. }
  958. }
  959. }
  960. if($text === 'both' || $text === 'translation'){
  961. if(isset($props['translation']) && is_array($props['translation'])){
  962. foreach ($props['translation'] as $key => $value) {
  963. $output .= "\n\n". $value['html'];
  964. }
  965. }
  966. }
  967. break;
  968. default:
  969. $output = '';
  970. break;
  971. }
  972. return $output;
  973. }
  974. private function render_mermaid(){
  975. $text = json_decode(base64_decode($this->get_param($this->param,"text",1)));
  976. $props = ["text" => implode("\n",$text)];
  977. switch ($this->format) {
  978. case 'react':
  979. $output = [
  980. 'props'=>base64_encode(\json_encode($props)),
  981. 'html'=>"mermaid",
  982. 'tag'=>'div',
  983. 'tpl'=>'mermaid',
  984. ];
  985. break;
  986. case 'unity':
  987. $output = [
  988. 'props'=>base64_encode(\json_encode($props)),
  989. 'tpl'=>'mermaid',
  990. ];
  991. break;
  992. case 'text':
  993. $output = 'mermaid';
  994. break;
  995. case 'tex':
  996. $output = 'mermaid';
  997. break;
  998. case 'simple':
  999. $output = 'mermaid';
  1000. break;
  1001. default:
  1002. $output = 'mermaid';
  1003. break;
  1004. }
  1005. return $output;
  1006. }
  1007. private function render_qa(){
  1008. $id = $this->get_param($this->param,"id",1);
  1009. $style = $this->get_param($this->param,"style",2);
  1010. $props = [
  1011. "type" => 'qa',
  1012. "id" => $id,
  1013. 'title' => '',
  1014. 'style' => $style,
  1015. ];
  1016. $qa = Discussion::where('id',$id)->first();
  1017. if($qa){
  1018. $props['title'] = $qa->title;
  1019. $props['resId'] = $qa->res_id;
  1020. $props['resType'] = $qa->res_type;
  1021. }
  1022. switch ($this->format) {
  1023. case 'react':
  1024. $output = [
  1025. 'props'=>base64_encode(\json_encode($props)),
  1026. 'html'=>"",
  1027. 'text'=>$props['title'],
  1028. 'tag'=>'div',
  1029. 'tpl'=>'qa',
  1030. ];
  1031. break;
  1032. case 'unity':
  1033. $output = [
  1034. 'props'=>base64_encode(\json_encode($props)),
  1035. 'tpl'=>'qa',
  1036. ];
  1037. break;
  1038. default:
  1039. $output = $props['title'];
  1040. break;
  1041. }
  1042. return $output;
  1043. }
  1044. private function render_grammar_lookup(){
  1045. $word = $this->get_param($this->param,"word",1);
  1046. $props = ['word' => $word];
  1047. $localTermChannel = ChannelApi::getSysChannel(
  1048. "_System_Grammar_Term_".strtolower($this->lang)."_",
  1049. "_System_Grammar_Term_en_"
  1050. );
  1051. $term = $this->getTermProps($word,null,$localTermChannel);
  1052. $props['term'] = $term;
  1053. switch ($this->format) {
  1054. case 'react':
  1055. $output = [
  1056. 'props'=>base64_encode(\json_encode($props)),
  1057. 'html'=>"",
  1058. 'text'=>$props['word'],
  1059. 'tag'=>'span',
  1060. 'tpl'=>'grammar',
  1061. ];
  1062. break;
  1063. case 'unity':
  1064. $output = [
  1065. 'props'=>base64_encode(\json_encode($props)),
  1066. 'tpl'=>'grammar',
  1067. ];
  1068. break;
  1069. default:
  1070. $output = $props['word'];
  1071. break;
  1072. }
  1073. return $output;
  1074. }
  1075. private function render_video(){
  1076. $url = $this->get_param($this->param,"url",1);
  1077. $style = $this->get_param($this->param,"style",2,'modal');
  1078. $title = $this->get_param($this->param,"title",3);
  1079. $props = [
  1080. "url" => $url,
  1081. 'title' => $title,
  1082. 'style' => $style,
  1083. ];
  1084. switch ($this->format) {
  1085. case 'react':
  1086. $output = [
  1087. 'props'=>base64_encode(\json_encode($props)),
  1088. 'html'=>"",
  1089. 'text'=>$props['title'],
  1090. 'tag'=>'span',
  1091. 'tpl'=>'video',
  1092. ];
  1093. break;
  1094. case 'unity':
  1095. $output = [
  1096. 'props'=>base64_encode(\json_encode($props)),
  1097. 'tpl'=>'video',
  1098. ];
  1099. break;
  1100. default:
  1101. $output = $props['title'];
  1102. break;
  1103. }
  1104. return $output;
  1105. }
  1106. private function get_param(array $param,string $name,int $id,string $default=''){
  1107. if(isset($param[$name])){
  1108. return trim($param[$name]);
  1109. }else if(isset($param["{$id}"])){
  1110. return trim($param["{$id}"]);
  1111. }else{
  1112. return $default;
  1113. }
  1114. }
  1115. private function mb_trim($str,string $character_mask = ' ', $charset = "UTF-8") {
  1116. $start = 0;
  1117. $end = mb_strlen($str, $charset) - 1;
  1118. $chars = preg_split('//u', $character_mask, -1, PREG_SPLIT_NO_EMPTY);
  1119. while ($start <= $end && in_array(mb_substr($str, $start, 1, $charset),$chars)) {
  1120. $start++;
  1121. }
  1122. while ($end >= $start && in_array(mb_substr($str, $end, 1, $charset),$chars) ) {
  1123. $end--;
  1124. }
  1125. return mb_substr($str, $start, $end - $start + 1, $charset);
  1126. }
  1127. }