TemplateRender.php 49 KB

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