TemplateRender.php 45 KB

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