TemplateRender.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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]) && $GLOBALS[$key]===1){
  352. $GLOBALS[$key]++;
  353. $output = $props["meaning"];
  354. }else{
  355. $GLOBALS[$key] = 1;
  356. $output = $props["meaning"].'('.$props["word"].')';
  357. }
  358. }else{
  359. $output = $props["word"];
  360. }
  361. //如果有内容且第一次出现,显示为脚注
  362. if(!empty($props["note"]) && $GLOBALS[$key]===1){
  363. if(isset($GLOBALS['note_sn'])){
  364. $GLOBALS['note_sn']++;
  365. }else{
  366. $GLOBALS['note_sn'] = 1;
  367. $GLOBALS['note'] = array();
  368. }
  369. $content = $props["note"];
  370. $output .= '[^'.$GLOBALS['note_sn'].']';
  371. $GLOBALS['note'][] = [
  372. 'sn' => $GLOBALS['note_sn'],
  373. 'trigger' => '',
  374. 'content' => $content,
  375. ];
  376. }
  377. break;
  378. default:
  379. if(isset($props["meaning"])){
  380. $output = $props["meaning"];
  381. }else{
  382. $output = $props["word"];
  383. }
  384. break;
  385. }
  386. return $output;
  387. }
  388. private function render_note(){
  389. $note = $this->get_param($this->param,"text",1);
  390. $trigger = $this->get_param($this->param,"trigger",2);
  391. $props = ["note" => $note ];
  392. $innerString = "";
  393. if(!empty($trigger)){
  394. $props["trigger"] = $trigger;
  395. $innerString = $props["trigger"];
  396. }
  397. if($this->format==='unity'){
  398. $props["note"] = MdRender::render(
  399. $props["note"],
  400. $this->channel_id,
  401. null,
  402. 'read',
  403. 'translation',
  404. 'markdown',
  405. 'unity'
  406. );
  407. }
  408. $output = $note;
  409. switch ($this->format) {
  410. case 'react':
  411. $output=[
  412. 'props'=>base64_encode(\json_encode($props)),
  413. 'html'=>$innerString,
  414. 'tag'=>'span',
  415. 'tpl'=>'note',
  416. ];
  417. break;
  418. case 'unity':
  419. $output=[
  420. 'props'=>base64_encode(\json_encode($props)),
  421. 'tpl'=>'note',
  422. ];
  423. break;
  424. case 'html':
  425. if(isset($GLOBALS['note_sn'])){
  426. $GLOBALS['note_sn']++;
  427. }else{
  428. $GLOBALS['note_sn'] = 1;
  429. $GLOBALS['note'] = array();
  430. }
  431. $GLOBALS['note'][] = [
  432. 'sn' => $GLOBALS['note_sn'],
  433. 'trigger' => $trigger,
  434. 'content' => MdRender::render($props["note"],
  435. $this->channel_id,
  436. null,
  437. 'read',
  438. 'translation',
  439. 'markdown',
  440. 'html'
  441. ),
  442. ];
  443. $link="<a href='#footnote-".$GLOBALS['note_sn']."' name='note-".$GLOBALS['note_sn']."'>";
  444. if(empty($trigger)){
  445. $output = $link. "<sup>[" . $GLOBALS['note_sn'] . "]</sup></a>";
  446. }else{
  447. $output = $link . $trigger . "</a>";
  448. }
  449. break;
  450. case 'text':
  451. $output = $trigger;
  452. break;
  453. case 'tex':
  454. $output = $trigger;
  455. break;
  456. case 'simple':
  457. $output = '';
  458. break;
  459. case 'markdown':
  460. if(isset($GLOBALS['note_sn'])){
  461. $GLOBALS['note_sn']++;
  462. }else{
  463. $GLOBALS['note_sn'] = 1;
  464. $GLOBALS['note'] = array();
  465. }
  466. $content = MdRender::render(
  467. $props["note"],
  468. $this->channel_id,
  469. null,
  470. 'read',
  471. 'translation',
  472. 'markdown',
  473. 'markdown'
  474. );
  475. $output = '[^'.$GLOBALS['note_sn'].']';
  476. $GLOBALS['note'][] = [
  477. 'sn' => $GLOBALS['note_sn'],
  478. 'trigger' => $trigger,
  479. 'content' => $content,
  480. ];
  481. //$output = '<footnote id="'.$GLOBALS['note_sn'].'">'.$content.'</footnote>';
  482. break;
  483. default:
  484. $output = '';
  485. break;
  486. }
  487. return $output;
  488. }
  489. private function render_nissaya(){
  490. $pali = $this->get_param($this->param,"pali",1);
  491. $meaning = $this->get_param($this->param,"meaning",2);
  492. $innerString = "";
  493. $props = [
  494. "pali" => $pali,
  495. "meaning" => $meaning,
  496. "lang" => $this->lang,
  497. ];
  498. switch ($this->format) {
  499. case 'react':
  500. $output = [
  501. 'props'=>base64_encode(\json_encode($props)),
  502. 'html'=>$innerString,
  503. 'tag'=>'span',
  504. 'tpl'=>'nissaya',
  505. ];
  506. break;
  507. case 'unity':
  508. $output = [
  509. 'props'=>base64_encode(\json_encode($props)),
  510. 'tpl'=>'nissaya',
  511. ];
  512. break;
  513. case 'text':
  514. $output = $pali.'၊'.$meaning;
  515. break;
  516. case 'tex':
  517. $output = $pali.'၊'.$meaning;
  518. break;
  519. case 'simple':
  520. $output = $pali.'၊'.$meaning;
  521. break;
  522. default:
  523. $output = $pali.'၊'.$meaning;
  524. break;
  525. }
  526. return $output;
  527. }
  528. private function render_exercise(){
  529. $id = $this->get_param($this->param,"id",1);
  530. $title = $this->get_param($this->param,"title",1);
  531. $props = [
  532. "id" => $id,
  533. "title" => $title,
  534. "channel" => $this->channel_id[0],
  535. ];
  536. switch ($this->format) {
  537. case 'react':
  538. $output = [
  539. 'props'=>base64_encode(\json_encode($props)),
  540. 'html'=>"",
  541. 'tag'=>'span',
  542. 'tpl'=>'exercise',
  543. ];
  544. break;
  545. case 'unity':
  546. $output = [
  547. 'props'=>base64_encode(\json_encode($props)),
  548. 'tpl'=>'exercise',
  549. ];
  550. break;
  551. case 'text':
  552. $output = $title;
  553. break;
  554. case 'tex':
  555. $output = $title;
  556. break;
  557. case 'simple':
  558. $output = $title;
  559. break;
  560. default:
  561. $output = '';
  562. break;
  563. }
  564. return $output;
  565. }
  566. private function render_article(){
  567. $type = $this->get_param($this->param,"type",1);
  568. $id = $this->get_param($this->param,"id",2);
  569. $title = $this->get_param($this->param,"title",3);
  570. $channel = $this->get_param($this->param,"channel",4);
  571. $style = $this->get_param($this->param,"style",5);
  572. $book = $this->get_param($this->param,"book",6);
  573. $paragraphs = $this->get_param($this->param,"paragraphs",7);
  574. $anthology = $this->get_param($this->param,"anthology",8);
  575. $props = [
  576. "type" => $type,
  577. "id" => $id,
  578. 'style' => $style,
  579. ];
  580. if(!empty($channel)){
  581. $props['channel'] = $channel;
  582. }
  583. if(!empty($title)){
  584. $props['title'] = $title;
  585. }
  586. if(!empty($book)){
  587. $props['book'] = $book;
  588. }
  589. if(!empty($paragraphs)){
  590. $props['paragraphs'] = $paragraphs;
  591. }
  592. if(!empty($anthology)){
  593. $props['anthology'] = $anthology;
  594. }
  595. if(is_array($this->channel_id)){
  596. $props['parentChannels'] = $this->channel_id;
  597. }
  598. switch ($this->format) {
  599. case 'react':
  600. $output = [
  601. 'props'=>base64_encode(\json_encode($props)),
  602. 'html'=>"",
  603. 'text'=>$title,
  604. 'tag'=>'span',
  605. 'tpl'=>'article',
  606. ];
  607. break;
  608. case 'unity':
  609. $output = [
  610. 'props'=>base64_encode(\json_encode($props)),
  611. 'tpl'=>'article',
  612. ];
  613. break;
  614. case 'text':
  615. $output = $title;
  616. break;
  617. case 'tex':
  618. $output = $title;
  619. break;
  620. case 'simple':
  621. $output = $title;
  622. break;
  623. default:
  624. $output = '';
  625. break;
  626. }
  627. return $output;
  628. }
  629. private function render_quote(){
  630. $paraId = $this->get_param($this->param,"para",1);
  631. $channelId = $this->channel_id[0];
  632. $props = RedisClusters::remember("/quote/{$channelId}/{$paraId}",
  633. config('mint.cache.expire'),
  634. function() use($paraId,$channelId){
  635. $para = \explode('-',$paraId);
  636. $output = [
  637. "paraId" => $paraId,
  638. "channel" => $channelId,
  639. "innerString" => $paraId,
  640. ];
  641. if(count($para)<2){
  642. return $output;
  643. }
  644. $PaliText = PaliText::where("book",$para[0])
  645. ->where("paragraph",$para[1])
  646. ->select(['toc','path'])
  647. ->first();
  648. if($PaliText){
  649. $output["pali"] = $PaliText->toc;
  650. $output["paliPath"] = \json_decode($PaliText->path);
  651. $output["innerString"]= $PaliText->toc;
  652. }
  653. return $output;
  654. });
  655. switch ($this->format) {
  656. case 'react':
  657. $output = [
  658. 'props'=>base64_encode(\json_encode($props)),
  659. 'html'=>$props["innerString"],
  660. 'tag'=>'span',
  661. 'tpl'=>'quote',
  662. ];
  663. break;
  664. case 'unity':
  665. $output = [
  666. 'props'=>base64_encode(\json_encode($props)),
  667. 'tpl'=>'quote',
  668. ];
  669. break;
  670. case 'text':
  671. $output = $props["innerString"];
  672. break;
  673. case 'tex':
  674. $output = $props["innerString"];
  675. break;
  676. case 'simple':
  677. $output = $props["innerString"];
  678. break;
  679. default:
  680. $output = $props["innerString"];
  681. break;
  682. }
  683. return $output;
  684. }
  685. private function render_quote_link(){
  686. $type = $this->get_param($this->param,"type",1);
  687. $title = $this->get_param($this->param,"title",6,'');
  688. $bookName = $this->get_param($this->param,"bookname",2,'');
  689. $volume = $this->get_param($this->param,"volume",3);
  690. $page = $this->get_param($this->param,"page",4,'');
  691. $style = $this->get_param($this->param,"style",5,'modal');
  692. $book = $this->get_param($this->param,"book",7,false);
  693. $para = $this->get_param($this->param,"para",8,false);
  694. $props = [
  695. 'type' => $type,
  696. 'style' => $style,
  697. 'found' => true,
  698. ];
  699. if(!empty($bookName) && $volume !== '' && !empty($page)){
  700. $props['bookName'] = $bookName;
  701. $props['volume'] = (int)$volume;
  702. $props['page'] = $page;
  703. $props['found'] = true;
  704. }else if($book && $para){
  705. /**
  706. * 没有指定书名,根据book para 查询
  707. */
  708. if($type==='c'){
  709. //按照章节名称显示
  710. $path = PaliTextApi::getChapterPath($book,$para);
  711. if($path){
  712. $path = json_decode($path,true);
  713. }
  714. if($path && is_array($path) && count($path)>2){
  715. $props['bookName'] = strtolower($path[0]['title']) ;
  716. $props['chapter'] = strtolower(end($path)['title']);
  717. $props['found'] = true;
  718. }else{
  719. $props['found'] = false;
  720. }
  721. }else{
  722. $pageInfo = $this->pageInfoByPara($type,$book,$para);
  723. if($pageInfo['found']){
  724. $props['bookName'] = $pageInfo['bookName'];
  725. $props['volume'] = $pageInfo['volume'];
  726. $props['page'] = $pageInfo['page'];
  727. $props['found'] = true;
  728. }else{
  729. $props['found'] = false;
  730. }
  731. }
  732. }else if($title){
  733. //没有书号用title查询
  734. //$tmpTitle = explode('။',$title);
  735. for ($i=mb_strlen($title,'UTF-8'); $i > 0 ; $i--) {
  736. $mTitle = mb_substr($title,0,$i);
  737. $has = array_search($mTitle, array_column(BookTitle::my(), 'title2'));
  738. Log::debug('run',['title'=>$mTitle,'has'=>$has]);
  739. if($has !== false){
  740. $tmpBookTitle = $mTitle;
  741. $tmpBookPage = mb_substr($title,$i);
  742. $tmpBookPage = $this->mb_trim($tmpBookPage,'၊။');
  743. break;
  744. }
  745. }
  746. if(isset($tmpBookTitle)){
  747. Log::debug('book title found',['title'=>$tmpBookTitle,'page'=>$tmpBookPage]);
  748. //$tmpBookTitle = $tmpTitle[0];
  749. //$tmpBookPage = $tmpTitle[1];
  750. $tmpBookPage = (int)str_replace(
  751. ['၁','၂','၃','၄','၅','၆','၇','၈','၉','၀'],
  752. ['1','2','3','4','5','6','7','8','9','0'],
  753. $tmpBookPage);
  754. $found_key = array_search($tmpBookTitle, array_column(BookTitle::my(), 'title2'));
  755. if($found_key !== false){
  756. $props['bookName'] = BookTitle::my()[$found_key]['bookname'];
  757. $props['volume'] = BookTitle::my()[$found_key]['volume'];
  758. $props['page'] = $tmpBookPage;
  759. if(!empty($props['bookName'])){
  760. $found_title = array_search($props['bookName'], array_column(BookTitle::my(), 'bookname'));
  761. if($found_title === false){
  762. $props['found'] = false;
  763. }
  764. }
  765. }else{
  766. //没找到,返回术语和页码
  767. $props['found'] = false;
  768. $props['bookName'] = $tmpBookTitle;
  769. $props['page'] = $tmpBookPage;
  770. $props['volume'] = 0;
  771. }
  772. }
  773. }else{
  774. Log::debug('book title not found');
  775. $props['found'] = false;
  776. }
  777. if($book && $para){
  778. $props['book'] = $book;
  779. $props['para'] = $para;
  780. }
  781. if($title){
  782. $props['title'] = $title;
  783. }
  784. $text = '';
  785. if(isset($props['bookName'])){
  786. $searchField = '';
  787. switch ($type) {
  788. case 'm':
  789. $searchField = 'm_title';
  790. break;
  791. case 'p':
  792. $searchField = 'p_title';
  793. break;
  794. }
  795. $found_title = array_search($props['bookName'], array_column(BookTitle::get(), $searchField));
  796. if($found_title === false){
  797. $props['found'] = false;
  798. }
  799. $term = $this->getTermProps($props['bookName'],':quote:');
  800. $props['term'] = $term;
  801. if(isset($term['id'])){
  802. $props['bookNameLocal'] = $term['meaning'];
  803. $text .= $term['meaning'];
  804. }else{
  805. $text .= $bookName;
  806. }
  807. }
  808. if(isset($props['volume']) && isset($props['page'])){
  809. $text .= " {$volume}.{$page}";
  810. }
  811. switch ($this->format) {
  812. case 'react':
  813. $output = [
  814. 'props'=>base64_encode(\json_encode($props)),
  815. 'html'=>'',
  816. 'tag'=>'span',
  817. 'tpl'=>'quote-link',
  818. ];
  819. break;
  820. case 'unity':
  821. $output = [
  822. 'props'=>base64_encode(\json_encode($props)),
  823. 'tpl'=>'quote-link',
  824. ];
  825. break;
  826. default:
  827. $output = $text;
  828. break;
  829. }
  830. return $output;
  831. }
  832. private function pageInfoByPara($type,$book,$para){
  833. $output = array();
  834. $pageInfo = PageNumber::where('type',strtoupper($type))
  835. ->where('book',$book)
  836. ->where('paragraph','<=',$para)
  837. ->orderBy('paragraph','desc')
  838. ->first();
  839. if($pageInfo){
  840. foreach (BookTitle::get() as $value) {
  841. if($value['id']===$pageInfo->pcd_book_id){
  842. switch (strtoupper($type)) {
  843. case 'M':
  844. $key = 'm_title';
  845. break;
  846. case 'P':
  847. $key = 'p_title';
  848. break;
  849. case 'V':
  850. $key = 'v_title';
  851. break;
  852. default:
  853. $key = 'term';
  854. break;
  855. }
  856. $output['bookName'] = $value[$key];
  857. break;
  858. }
  859. }
  860. $output['volume'] = $pageInfo->volume;
  861. $output['page'] = $pageInfo->page;
  862. $output['found'] = true;
  863. }else{
  864. $output['found'] = false;
  865. }
  866. return $output;
  867. }
  868. private function render_sent(){
  869. $sid = $this->get_param($this->param,"id",1);
  870. $channel = $this->get_param($this->param,"channel",2);
  871. $text = $this->get_param($this->param,"text",2,'both');
  872. if(!empty($channel)){
  873. $channels = explode(',',$channel);
  874. }else{
  875. $channels = $this->channel_id;
  876. }
  877. $sentInfo = explode('@',trim($sid));
  878. $sentId = $sentInfo[0];
  879. if(isset($sentInfo[1])){
  880. $channels = [$sentInfo[1]];
  881. }
  882. $Sent = new CorpusController();
  883. $props = $Sent->getSentTpl($sentId,$channels,
  884. $this->mode,true,
  885. $this->format);
  886. if($props === false){
  887. $props['error']="句子模版渲染错误。句子参数个数不符。应该是四个。";
  888. }
  889. if($this->mode==='read'){
  890. $tpl = "sentread";
  891. }else{
  892. $tpl = "sentedit";
  893. }
  894. //输出引用
  895. $arrSid = explode('-',$sid);
  896. $bookPara = array_slice($arrSid,0,2);
  897. if(!isset($GLOBALS['ref_sent'])){
  898. $GLOBALS['ref_sent'] = array();
  899. }
  900. $GLOBALS['ref_sent'][] = $bookPara;
  901. switch ($this->format) {
  902. case 'react':
  903. $output = [
  904. 'props'=>base64_encode(\json_encode($props)),
  905. 'html'=>"",
  906. 'tag'=>'span',
  907. 'tpl'=>$tpl,
  908. ];
  909. break;
  910. case 'unity':
  911. $output = [
  912. 'props'=>base64_encode(\json_encode($props)),
  913. 'tpl'=>$tpl,
  914. ];
  915. break;
  916. case 'text':
  917. $output = '';
  918. if(isset($props['origin']) && is_array($props['origin'])){
  919. foreach ($props['origin'] as $key => $value) {
  920. $output .= $value['html'];
  921. }
  922. }
  923. if(isset($props['translation']) && is_array($props['translation'])){
  924. foreach ($props['translation'] as $key => $value) {
  925. $output .= $value['html'];
  926. }
  927. }
  928. break;
  929. case 'html':
  930. $output = '';
  931. $output .= '<span class="sentence">';
  932. if($text === 'both' || $text === 'origin'){
  933. if(isset($props['origin']) && is_array($props['origin'])){
  934. foreach ($props['origin'] as $key => $value) {
  935. $output .= '<span class="origin">'.$value['html'].'</span>';
  936. }
  937. }
  938. }
  939. if($text === 'both' || $text === 'translation'){
  940. if(isset($props['translation']) && is_array($props['translation'])){
  941. foreach ($props['translation'] as $key => $value) {
  942. $output .= '<span class="translation">'.$value['html'].'</span>';
  943. }
  944. }
  945. }
  946. $output .= '</span>';
  947. break;
  948. case 'tex':
  949. $output = '';
  950. if(isset($props['translation']) && is_array($props['translation'])){
  951. foreach ($props['translation'] as $key => $value) {
  952. $output .= $value['html'];
  953. }
  954. }
  955. break;
  956. case 'simple':
  957. $output = '';
  958. if($text === 'both' || $text === 'origin'){
  959. if(empty($output)){
  960. if(isset($props['origin']) &&
  961. is_array($props['origin']) &&
  962. count($props['origin']) > 0
  963. ){
  964. foreach ($props['origin'] as $key => $value) {
  965. $output .= trim($value['html']);
  966. }
  967. }
  968. }
  969. }
  970. if($text === 'both' || $text === 'translation'){
  971. if(isset($props['translation']) &&
  972. is_array($props['translation']) &&
  973. count($props['translation']) > 0
  974. ){
  975. foreach ($props['translation'] as $key => $value) {
  976. $output .= trim($value['html']);
  977. }
  978. }
  979. }
  980. break;
  981. case 'markdown':
  982. $output = '';
  983. if($text === 'both' || $text === 'origin'){
  984. if($this->options['origin'] === true ||
  985. $this->options['origin'] === 'true'){
  986. if(isset($props['origin']) && is_array($props['origin'])){
  987. foreach ($props['origin'] as $key => $value) {
  988. $output .= trim($value['html']);
  989. }
  990. }
  991. }
  992. }
  993. if($text === 'both' || $text === 'translation'){
  994. if($this->options['translation'] === true ||
  995. $this->options['translation'] === 'true'){
  996. if(isset($props['translation']) &&
  997. is_array($props['translation']) &&
  998. count($props['translation'])>0){
  999. foreach ($props['translation'] as $key => $value) {
  1000. $output .= trim($value['html']);
  1001. }
  1002. }else{
  1003. if($text === 'translation'){
  1004. //无译文用原文代替
  1005. if(isset($props['origin']) && is_array($props['origin'])){
  1006. foreach ($props['origin'] as $key => $value) {
  1007. $output .= trim($value['html']);
  1008. }
  1009. }
  1010. }
  1011. }
  1012. }
  1013. }
  1014. break;
  1015. default:
  1016. $output = '';
  1017. break;
  1018. }
  1019. return $output;
  1020. }
  1021. private function render_mermaid(){
  1022. $text = json_decode(base64_decode($this->get_param($this->param,"text",1)));
  1023. $props = ["text" => implode("\n",$text)];
  1024. switch ($this->format) {
  1025. case 'react':
  1026. $output = [
  1027. 'props'=>base64_encode(\json_encode($props)),
  1028. 'html'=>"mermaid",
  1029. 'tag'=>'div',
  1030. 'tpl'=>'mermaid',
  1031. ];
  1032. break;
  1033. case 'unity':
  1034. $output = [
  1035. 'props'=>base64_encode(\json_encode($props)),
  1036. 'tpl'=>'mermaid',
  1037. ];
  1038. break;
  1039. case 'text':
  1040. $output = 'mermaid';
  1041. break;
  1042. case 'tex':
  1043. $output = 'mermaid';
  1044. break;
  1045. case 'simple':
  1046. $output = 'mermaid';
  1047. break;
  1048. default:
  1049. $output = 'mermaid';
  1050. break;
  1051. }
  1052. return $output;
  1053. }
  1054. private function render_qa(){
  1055. $id = $this->get_param($this->param,"id",1);
  1056. $style = $this->get_param($this->param,"style",2);
  1057. $props = [
  1058. "type" => 'qa',
  1059. "id" => $id,
  1060. 'title' => '',
  1061. 'style' => $style,
  1062. ];
  1063. $qa = Discussion::where('id',$id)->first();
  1064. if($qa){
  1065. $props['title'] = $qa->title;
  1066. $props['resId'] = $qa->res_id;
  1067. $props['resType'] = $qa->res_type;
  1068. }
  1069. switch ($this->format) {
  1070. case 'react':
  1071. $output = [
  1072. 'props'=>base64_encode(\json_encode($props)),
  1073. 'html'=>"",
  1074. 'text'=>$props['title'],
  1075. 'tag'=>'div',
  1076. 'tpl'=>'qa',
  1077. ];
  1078. break;
  1079. case 'unity':
  1080. $output = [
  1081. 'props'=>base64_encode(\json_encode($props)),
  1082. 'tpl'=>'qa',
  1083. ];
  1084. break;
  1085. default:
  1086. $output = $props['title'];
  1087. break;
  1088. }
  1089. return $output;
  1090. }
  1091. private function render_grammar_lookup(){
  1092. $word = $this->get_param($this->param,"word",1);
  1093. $props = ['word' => $word];
  1094. $localTermChannel = ChannelApi::getSysChannel(
  1095. "_System_Grammar_Term_".strtolower($this->lang)."_",
  1096. "_System_Grammar_Term_en_"
  1097. );
  1098. $term = $this->getTermProps($word,null,$localTermChannel);
  1099. $props['term'] = $term;
  1100. switch ($this->format) {
  1101. case 'react':
  1102. $output = [
  1103. 'props'=>base64_encode(\json_encode($props)),
  1104. 'html'=>"",
  1105. 'text'=>$props['word'],
  1106. 'tag'=>'span',
  1107. 'tpl'=>'grammar',
  1108. ];
  1109. break;
  1110. case 'unity':
  1111. $output = [
  1112. 'props'=>base64_encode(\json_encode($props)),
  1113. 'tpl'=>'grammar',
  1114. ];
  1115. break;
  1116. default:
  1117. $output = $props['word'];
  1118. break;
  1119. }
  1120. return $output;
  1121. }
  1122. private function render_video(){
  1123. $url = $this->get_param($this->param,"url",1);
  1124. $style = $this->get_param($this->param,"style",2,'modal');
  1125. $title = $this->get_param($this->param,"title",3);
  1126. $props = [
  1127. "url" => $url,
  1128. 'title' => $title,
  1129. 'style' => $style,
  1130. ];
  1131. switch ($this->format) {
  1132. case 'react':
  1133. $output = [
  1134. 'props'=>base64_encode(\json_encode($props)),
  1135. 'html'=>"",
  1136. 'text'=>$props['title'],
  1137. 'tag'=>'span',
  1138. 'tpl'=>'video',
  1139. ];
  1140. break;
  1141. case 'unity':
  1142. $output = [
  1143. 'props'=>base64_encode(\json_encode($props)),
  1144. 'tpl'=>'video',
  1145. ];
  1146. break;
  1147. default:
  1148. $output = $props['title'];
  1149. break;
  1150. }
  1151. return $output;
  1152. }
  1153. //论文后面的参考资料
  1154. private function render_ref(){
  1155. $references = array();
  1156. $counter = 0;
  1157. if(isset($GLOBALS['ref_sent'])){
  1158. $hasBooks = array();
  1159. $book_titles = BookSeries::select(['book','paragraph','title','sn'])
  1160. ->orderBy('sn','DESC')->get();
  1161. $bTitles = array();
  1162. foreach ($book_titles as $key => $book) {
  1163. $bTitles[] = [
  1164. 'book'=>$book->book,
  1165. 'paragraph'=>$book->paragraph,
  1166. 'title'=>$book->title
  1167. ];
  1168. }
  1169. foreach ($GLOBALS['ref_sent'] as $key => $ref) {
  1170. $books = array_filter($bTitles,function($value) use($ref){
  1171. return $value['book'] === (int)$ref[0];
  1172. });
  1173. if(count($books)>0){
  1174. foreach ($books as $key => $book) {
  1175. if($book['paragraph'] < (int)$ref[1]){
  1176. if(!isset($hasBooks[$book['title']])){
  1177. $hasBooks[$book['title']] = 1;
  1178. $counter++;
  1179. $references[] = [
  1180. 'sn'=>$counter,
  1181. 'title'=>$book['title'],
  1182. 'copyright'=>'CSCD V4 VRI 2008'
  1183. ];
  1184. }
  1185. }
  1186. }
  1187. }
  1188. }
  1189. }
  1190. $props = [
  1191. "pali" => $references,
  1192. ];
  1193. switch ($this->format) {
  1194. case 'react':
  1195. $output = [
  1196. 'props'=>base64_encode(\json_encode($props)),
  1197. 'html'=>'',
  1198. 'tag'=>'div',
  1199. 'tpl'=>'reference',
  1200. ];
  1201. break;
  1202. case 'unity':
  1203. $output = [
  1204. 'props'=>base64_encode(\json_encode($props)),
  1205. 'tpl'=>'reference',
  1206. ];
  1207. break;
  1208. case 'markdown':
  1209. $output = '';
  1210. foreach ($references as $key => $reference) {
  1211. $output .= '[' . $reference['sn'] . '] **' . ucfirst($reference['title']) . '** ';
  1212. $output .= $reference['copyright']. "\n\n";
  1213. }
  1214. break;
  1215. default:
  1216. $output = '';
  1217. foreach ($references as $key => $reference) {
  1218. $output .= '[' . $reference['sn'] . '] ' . ucfirst($reference['title']) . ' ';
  1219. $output .= $reference['copyright']. "\n";
  1220. }
  1221. break;
  1222. }
  1223. return $output;
  1224. }
  1225. private function get_param(array $param,string $name,int $id,string $default=''){
  1226. if(isset($param[$name])){
  1227. return trim($param[$name]);
  1228. }else if(isset($param["{$id}"])){
  1229. return trim($param["{$id}"]);
  1230. }else{
  1231. return $default;
  1232. }
  1233. }
  1234. private function mb_trim($str,string $character_mask = ' ', $charset = "UTF-8") {
  1235. $start = 0;
  1236. $end = mb_strlen($str, $charset) - 1;
  1237. $chars = preg_split('//u', $character_mask, -1, PREG_SPLIT_NO_EMPTY);
  1238. while ($start <= $end && in_array(mb_substr($str, $start, 1, $charset),$chars)) {
  1239. $start++;
  1240. }
  1241. while ($end >= $start && in_array(mb_substr($str, $end, 1, $charset),$chars) ) {
  1242. $end--;
  1243. }
  1244. return mb_substr($str, $start, $end - $start + 1, $charset);
  1245. }
  1246. }