TemplateRender.php 49 KB

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