TemplateRender.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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::warning('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" => explode('=', $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 'prompt':
  535. $output = Tools::MyToRm($pali) . ':' . end($props["meaning"]);
  536. break;
  537. default:
  538. $output = $pali . '၊' . $meaning;
  539. break;
  540. }
  541. return $output;
  542. }
  543. private function render_exercise()
  544. {
  545. $id = $this->get_param($this->param, "id", 1);
  546. $title = $this->get_param($this->param, "title", 1);
  547. $props = [
  548. "id" => $id,
  549. "title" => $title,
  550. "channel" => $this->channel_id[0],
  551. ];
  552. switch ($this->format) {
  553. case 'react':
  554. $output = [
  555. 'props' => base64_encode(\json_encode($props)),
  556. 'html' => "",
  557. 'tag' => 'span',
  558. 'tpl' => 'exercise',
  559. ];
  560. break;
  561. case 'unity':
  562. $output = [
  563. 'props' => base64_encode(\json_encode($props)),
  564. 'tpl' => 'exercise',
  565. ];
  566. break;
  567. case 'text':
  568. $output = $title;
  569. break;
  570. case 'tex':
  571. $output = $title;
  572. break;
  573. case 'simple':
  574. $output = $title;
  575. break;
  576. default:
  577. $output = '';
  578. break;
  579. }
  580. return $output;
  581. }
  582. private function render_article()
  583. {
  584. $type = $this->get_param($this->param, "type", 1);
  585. $id = $this->get_param($this->param, "id", 2);
  586. $title = $this->get_param($this->param, "title", 3);
  587. $channel = $this->get_param($this->param, "channel", 4);
  588. $style = $this->get_param($this->param, "style", 5);
  589. $book = $this->get_param($this->param, "book", 6);
  590. $paragraphs = $this->get_param($this->param, "paragraphs", 7);
  591. $anthology = $this->get_param($this->param, "anthology", 8);
  592. if ($type === 'chapter' && empty($id)) {
  593. $book = (int)$book;
  594. $paragraphs = (int)$paragraphs;
  595. $id = "{$book}-{$paragraphs}";
  596. }
  597. $props = [
  598. "type" => $type,
  599. "id" => $id,
  600. 'style' => $style,
  601. ];
  602. if (!empty($channel)) {
  603. $props['channel'] = $channel;
  604. }
  605. if (!empty($title)) {
  606. $props['title'] = $title;
  607. }
  608. if (!empty($book)) {
  609. $props['book'] = $book;
  610. }
  611. if (!empty($paragraphs)) {
  612. $props['paragraphs'] = $paragraphs;
  613. }
  614. if (!empty($anthology)) {
  615. $props['anthology'] = $anthology;
  616. }
  617. if (is_array($this->channel_id)) {
  618. $props['parentChannels'] = $this->channel_id;
  619. }
  620. switch ($this->format) {
  621. case 'react':
  622. $output = [
  623. 'props' => base64_encode(\json_encode($props)),
  624. 'html' => "",
  625. 'text' => $title,
  626. 'tag' => 'span',
  627. 'tpl' => 'article',
  628. ];
  629. break;
  630. case 'unity':
  631. $output = [
  632. 'props' => base64_encode(\json_encode($props)),
  633. 'tpl' => 'article',
  634. ];
  635. break;
  636. case 'text':
  637. $output = $title;
  638. break;
  639. case 'tex':
  640. $output = $title;
  641. break;
  642. case 'simple':
  643. $output = $title;
  644. break;
  645. default:
  646. $output = '';
  647. break;
  648. }
  649. return $output;
  650. }
  651. private function render_quote()
  652. {
  653. $paraId = $this->get_param($this->param, "para", 1);
  654. $channelId = $this->channel_id[0];
  655. $props = RedisClusters::remember(
  656. "/quote/{$channelId}/{$paraId}",
  657. config('mint.cache.expire'),
  658. function () use ($paraId, $channelId) {
  659. $para = \explode('-', $paraId);
  660. $output = [
  661. "paraId" => $paraId,
  662. "channel" => $channelId,
  663. "innerString" => $paraId,
  664. ];
  665. if (count($para) < 2) {
  666. return $output;
  667. }
  668. $PaliText = PaliText::where("book", $para[0])
  669. ->where("paragraph", $para[1])
  670. ->select(['toc', 'path'])
  671. ->first();
  672. if ($PaliText) {
  673. $output["pali"] = $PaliText->toc;
  674. $output["paliPath"] = \json_decode($PaliText->path);
  675. $output["innerString"] = $PaliText->toc;
  676. }
  677. return $output;
  678. }
  679. );
  680. switch ($this->format) {
  681. case 'react':
  682. $output = [
  683. 'props' => base64_encode(\json_encode($props)),
  684. 'html' => $props["innerString"],
  685. 'tag' => 'span',
  686. 'tpl' => 'quote',
  687. ];
  688. break;
  689. case 'unity':
  690. $output = [
  691. 'props' => base64_encode(\json_encode($props)),
  692. 'tpl' => 'quote',
  693. ];
  694. break;
  695. case 'text':
  696. $output = $props["innerString"];
  697. break;
  698. case 'tex':
  699. $output = $props["innerString"];
  700. break;
  701. case 'simple':
  702. $output = $props["innerString"];
  703. break;
  704. default:
  705. $output = $props["innerString"];
  706. break;
  707. }
  708. return $output;
  709. }
  710. private function render_quote_link()
  711. {
  712. $type = $this->get_param($this->param, "type", 1);
  713. $title = $this->get_param($this->param, "title", 6, '');
  714. $bookName = $this->get_param($this->param, "bookname", 2, '');
  715. $volume = $this->get_param($this->param, "volume", 3);
  716. $page = $this->get_param($this->param, "page", 4, '');
  717. $style = $this->get_param($this->param, "style", 5, 'modal');
  718. $book = $this->get_param($this->param, "book", 7, false);
  719. $para = $this->get_param($this->param, "para", 8, false);
  720. $props = [
  721. 'type' => $type,
  722. 'style' => $style,
  723. 'found' => true,
  724. ];
  725. if (!empty($bookName) && $volume !== '' && !empty($page)) {
  726. $props['bookName'] = $bookName;
  727. $props['volume'] = (int)$volume;
  728. $props['page'] = $page;
  729. $props['found'] = true;
  730. } else if ($book && $para) {
  731. /**
  732. * 没有指定书名,根据book para 查询
  733. */
  734. if ($type === 'c') {
  735. //按照章节名称显示
  736. $path = PaliTextApi::getChapterPath($book, $para);
  737. if ($path) {
  738. $path = json_decode($path, true);
  739. }
  740. if ($path && is_array($path) && count($path) > 2) {
  741. $props['bookName'] = strtolower($path[0]['title']);
  742. $props['chapter'] = strtolower(end($path)['title']);
  743. $props['found'] = true;
  744. } else {
  745. $props['found'] = false;
  746. }
  747. } else {
  748. $pageInfo = $this->pageInfoByPara($type, $book, $para);
  749. if ($pageInfo['found']) {
  750. $props['bookName'] = $pageInfo['bookName'];
  751. $props['volume'] = $pageInfo['volume'];
  752. $props['page'] = $pageInfo['page'];
  753. $props['found'] = true;
  754. } else {
  755. $props['found'] = false;
  756. }
  757. }
  758. } else if ($title) {
  759. //没有书号用title查询
  760. //$tmpTitle = explode('။',$title);
  761. for ($i = mb_strlen($title, 'UTF-8'); $i > 0; $i--) {
  762. $mTitle = mb_substr($title, 0, $i);
  763. $has = array_search($mTitle, array_column(BookTitle::my(), 'title2'));
  764. Log::debug('run', ['title' => $mTitle, 'has' => $has]);
  765. if ($has !== false) {
  766. $tmpBookTitle = $mTitle;
  767. $tmpBookPage = mb_substr($title, $i);
  768. $tmpBookPage = $this->mb_trim($tmpBookPage, '၊။');
  769. break;
  770. }
  771. }
  772. if (isset($tmpBookTitle)) {
  773. Log::debug('book title found', ['title' => $tmpBookTitle, 'page' => $tmpBookPage]);
  774. //$tmpBookTitle = $tmpTitle[0];
  775. //$tmpBookPage = $tmpTitle[1];
  776. $tmpBookPage = (int)str_replace(
  777. ['၁', '၂', '၃', '၄', '၅', '၆', '၇', '၈', '၉', '၀'],
  778. ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
  779. $tmpBookPage
  780. );
  781. $found_key = array_search($tmpBookTitle, array_column(BookTitle::my(), 'title2'));
  782. if ($found_key !== false) {
  783. $props['bookName'] = BookTitle::my()[$found_key]['bookname'];
  784. $props['volume'] = BookTitle::my()[$found_key]['volume'];
  785. $props['page'] = $tmpBookPage;
  786. if (!empty($props['bookName'])) {
  787. $found_title = array_search($props['bookName'], array_column(BookTitle::my(), 'bookname'));
  788. if ($found_title === false) {
  789. $props['found'] = false;
  790. }
  791. }
  792. } else {
  793. //没找到,返回术语和页码
  794. $props['found'] = false;
  795. $props['bookName'] = $tmpBookTitle;
  796. $props['page'] = $tmpBookPage;
  797. $props['volume'] = 0;
  798. }
  799. }
  800. } else {
  801. Log::debug('book title not found');
  802. $props['found'] = false;
  803. }
  804. if ($book && $para) {
  805. $props['book'] = $book;
  806. $props['para'] = $para;
  807. }
  808. if ($title) {
  809. $props['title'] = $title;
  810. }
  811. $text = '';
  812. if (isset($props['bookName'])) {
  813. $searchField = '';
  814. switch ($type) {
  815. case 'm':
  816. $searchField = 'm_title';
  817. break;
  818. case 'p':
  819. $searchField = 'p_title';
  820. break;
  821. }
  822. $found_title = array_search($props['bookName'], array_column(BookTitle::get(), $searchField));
  823. if ($found_title === false) {
  824. $props['found'] = false;
  825. }
  826. $term = $this->getTermProps($props['bookName'], ':quote:');
  827. $props['term'] = $term;
  828. if (isset($term['id'])) {
  829. $props['bookNameLocal'] = $term['meaning'];
  830. $text .= $term['meaning'];
  831. } else {
  832. $text .= $bookName;
  833. }
  834. }
  835. if (isset($props['volume']) && isset($props['page'])) {
  836. $text .= " {$volume}.{$page}";
  837. }
  838. switch ($this->format) {
  839. case 'react':
  840. $output = [
  841. 'props' => base64_encode(\json_encode($props)),
  842. 'html' => '',
  843. 'tag' => 'span',
  844. 'tpl' => 'quote-link',
  845. ];
  846. break;
  847. case 'unity':
  848. $output = [
  849. 'props' => base64_encode(\json_encode($props)),
  850. 'tpl' => 'quote-link',
  851. ];
  852. break;
  853. default:
  854. $output = $text;
  855. break;
  856. }
  857. return $output;
  858. }
  859. private function pageInfoByPara($type, $book, $para)
  860. {
  861. $output = array();
  862. $pageInfo = PageNumber::where('type', strtoupper($type))
  863. ->where('book', $book)
  864. ->where('paragraph', '<=', $para)
  865. ->orderBy('paragraph', 'desc')
  866. ->first();
  867. if ($pageInfo) {
  868. foreach (BookTitle::get() as $value) {
  869. if ($value['id'] === $pageInfo->pcd_book_id) {
  870. switch (strtoupper($type)) {
  871. case 'M':
  872. $key = 'm_title';
  873. break;
  874. case 'P':
  875. $key = 'p_title';
  876. break;
  877. case 'V':
  878. $key = 'v_title';
  879. break;
  880. default:
  881. $key = 'term';
  882. break;
  883. }
  884. $output['bookName'] = $value[$key];
  885. break;
  886. }
  887. }
  888. $output['volume'] = $pageInfo->volume;
  889. $output['page'] = $pageInfo->page;
  890. $output['found'] = true;
  891. } else {
  892. $output['found'] = false;
  893. }
  894. return $output;
  895. }
  896. private function render_sent()
  897. {
  898. $sid = $this->get_param($this->param, "id", 1);
  899. $channel = $this->get_param($this->param, "channel", 2);
  900. $text = $this->get_param($this->param, "text", 2, 'both');
  901. if (!empty($channel)) {
  902. $channels = explode(',', $channel);
  903. } else {
  904. $channels = $this->channel_id;
  905. }
  906. $sentInfo = explode('@', trim($sid));
  907. $sentId = $sentInfo[0];
  908. if (isset($sentInfo[1])) {
  909. $channels = [$sentInfo[1]];
  910. }
  911. $Sent = new CorpusController();
  912. $props = $Sent->getSentTpl(
  913. $sentId,
  914. $channels,
  915. $this->mode,
  916. true,
  917. $this->format
  918. );
  919. if ($props === false) {
  920. $props['error'] = "句子模版渲染错误。句子参数个数不符。应该是四个。";
  921. }
  922. if ($this->mode === 'read') {
  923. $tpl = "sentread";
  924. } else {
  925. $tpl = "sentedit";
  926. }
  927. //输出引用
  928. $arrSid = explode('-', $sid);
  929. $bookPara = array_slice($arrSid, 0, 2);
  930. if (!isset($GLOBALS['ref_sent'])) {
  931. $GLOBALS['ref_sent'] = array();
  932. }
  933. $GLOBALS['ref_sent'][] = $bookPara;
  934. switch ($this->format) {
  935. case 'react':
  936. $output = [
  937. 'props' => base64_encode(\json_encode($props)),
  938. 'html' => "",
  939. 'tag' => 'span',
  940. 'tpl' => $tpl,
  941. ];
  942. break;
  943. case 'unity':
  944. $output = [
  945. 'props' => base64_encode(\json_encode($props)),
  946. 'tpl' => $tpl,
  947. ];
  948. break;
  949. case 'text':
  950. $output = '';
  951. if (isset($props['origin']) && is_array($props['origin'])) {
  952. foreach ($props['origin'] as $key => $value) {
  953. $output .= $value['html'];
  954. }
  955. }
  956. if (isset($props['translation']) && is_array($props['translation'])) {
  957. foreach ($props['translation'] as $key => $value) {
  958. $output .= $value['html'];
  959. }
  960. }
  961. break;
  962. case 'prompt':
  963. $output = '';
  964. if ($text === 'both' || $text === 'origin') {
  965. if (isset($props['origin']) && is_array($props['origin'])) {
  966. foreach ($props['origin'] as $key => $value) {
  967. $output .= $value['html'];
  968. }
  969. }
  970. }
  971. if ($text === 'both' || $text === 'translation') {
  972. if (isset($props['translation']) && is_array($props['translation'])) {
  973. foreach ($props['translation'] as $key => $value) {
  974. $output .= $value['html'];
  975. }
  976. }
  977. }
  978. break;
  979. case 'html':
  980. $output = '';
  981. $output .= '<span class="sentence">';
  982. if ($text === 'both' || $text === 'origin') {
  983. if (isset($props['origin']) && is_array($props['origin'])) {
  984. foreach ($props['origin'] as $key => $value) {
  985. $output .= '<span class="origin">' . $value['html'] . '</span>';
  986. }
  987. }
  988. }
  989. if ($text === 'both' || $text === 'translation') {
  990. if (isset($props['translation']) && is_array($props['translation'])) {
  991. foreach ($props['translation'] as $key => $value) {
  992. $output .= '<span class="translation">' . $value['html'] . '</span>';
  993. }
  994. }
  995. }
  996. $output .= '</span>';
  997. break;
  998. case 'tex':
  999. $output = '';
  1000. if (isset($props['translation']) && is_array($props['translation'])) {
  1001. foreach ($props['translation'] as $key => $value) {
  1002. $output .= $value['html'];
  1003. }
  1004. }
  1005. break;
  1006. case 'simple':
  1007. $output = '';
  1008. if ($text === 'both' || $text === 'origin') {
  1009. if (empty($output)) {
  1010. if (
  1011. isset($props['origin']) &&
  1012. is_array($props['origin']) &&
  1013. count($props['origin']) > 0
  1014. ) {
  1015. foreach ($props['origin'] as $key => $value) {
  1016. $output .= trim($value['html']);
  1017. }
  1018. }
  1019. }
  1020. }
  1021. if ($text === 'both' || $text === 'translation') {
  1022. if (
  1023. isset($props['translation']) &&
  1024. is_array($props['translation']) &&
  1025. count($props['translation']) > 0
  1026. ) {
  1027. foreach ($props['translation'] as $key => $value) {
  1028. $output .= trim($value['html']);
  1029. }
  1030. }
  1031. }
  1032. break;
  1033. case 'markdown':
  1034. $output = '';
  1035. if ($text === 'both' || $text === 'origin') {
  1036. if (
  1037. $this->options['origin'] === true ||
  1038. $this->options['origin'] === 'true'
  1039. ) {
  1040. if (isset($props['origin']) && is_array($props['origin'])) {
  1041. foreach ($props['origin'] as $key => $value) {
  1042. $output .= trim($value['html']);
  1043. }
  1044. }
  1045. }
  1046. }
  1047. if ($text === 'both' || $text === 'translation') {
  1048. if (
  1049. $this->options['translation'] === true ||
  1050. $this->options['translation'] === 'true'
  1051. ) {
  1052. if (
  1053. isset($props['translation']) &&
  1054. is_array($props['translation']) &&
  1055. count($props['translation']) > 0
  1056. ) {
  1057. foreach ($props['translation'] as $key => $value) {
  1058. $output .= trim($value['html']);
  1059. }
  1060. } else {
  1061. if ($text === 'translation') {
  1062. //无译文用原文代替
  1063. if (isset($props['origin']) && is_array($props['origin'])) {
  1064. foreach ($props['origin'] as $key => $value) {
  1065. $output .= trim($value['html']);
  1066. }
  1067. }
  1068. }
  1069. }
  1070. }
  1071. }
  1072. break;
  1073. default:
  1074. $output = '';
  1075. break;
  1076. }
  1077. return $output;
  1078. }
  1079. private function render_mermaid()
  1080. {
  1081. $text = json_decode(base64_decode($this->get_param($this->param, "text", 1)));
  1082. $props = ["text" => implode("\n", $text)];
  1083. switch ($this->format) {
  1084. case 'react':
  1085. $output = [
  1086. 'props' => base64_encode(\json_encode($props)),
  1087. 'html' => "mermaid",
  1088. 'tag' => 'div',
  1089. 'tpl' => 'mermaid',
  1090. ];
  1091. break;
  1092. case 'unity':
  1093. $output = [
  1094. 'props' => base64_encode(\json_encode($props)),
  1095. 'tpl' => 'mermaid',
  1096. ];
  1097. break;
  1098. case 'text':
  1099. $output = 'mermaid';
  1100. break;
  1101. case 'tex':
  1102. $output = 'mermaid';
  1103. break;
  1104. case 'simple':
  1105. $output = 'mermaid';
  1106. break;
  1107. default:
  1108. $output = 'mermaid';
  1109. break;
  1110. }
  1111. return $output;
  1112. }
  1113. private function render_qa()
  1114. {
  1115. $id = $this->get_param($this->param, "id", 1);
  1116. $style = $this->get_param($this->param, "style", 2);
  1117. $props = [
  1118. "type" => 'qa',
  1119. "id" => $id,
  1120. 'title' => '',
  1121. 'style' => $style,
  1122. ];
  1123. $qa = Discussion::where('id', $id)->first();
  1124. if ($qa) {
  1125. $props['title'] = $qa->title;
  1126. $props['resId'] = $qa->res_id;
  1127. $props['resType'] = $qa->res_type;
  1128. }
  1129. switch ($this->format) {
  1130. case 'react':
  1131. $output = [
  1132. 'props' => base64_encode(\json_encode($props)),
  1133. 'html' => "",
  1134. 'text' => $props['title'],
  1135. 'tag' => 'div',
  1136. 'tpl' => 'qa',
  1137. ];
  1138. break;
  1139. case 'unity':
  1140. $output = [
  1141. 'props' => base64_encode(\json_encode($props)),
  1142. 'tpl' => 'qa',
  1143. ];
  1144. break;
  1145. default:
  1146. $output = $props['title'];
  1147. break;
  1148. }
  1149. return $output;
  1150. }
  1151. private function render_grammar_lookup()
  1152. {
  1153. $word = $this->get_param($this->param, "word", 1);
  1154. $props = ['word' => $word];
  1155. $localTermChannel = ChannelApi::getSysChannel(
  1156. "_System_Grammar_Term_" . strtolower($this->lang) . "_",
  1157. "_System_Grammar_Term_en_"
  1158. );
  1159. $term = $this->getTermProps($word, null, $localTermChannel);
  1160. $props['term'] = $term;
  1161. switch ($this->format) {
  1162. case 'react':
  1163. $output = [
  1164. 'props' => base64_encode(\json_encode($props)),
  1165. 'html' => "",
  1166. 'text' => $props['word'],
  1167. 'tag' => 'span',
  1168. 'tpl' => 'grammar',
  1169. ];
  1170. break;
  1171. case 'unity':
  1172. $output = [
  1173. 'props' => base64_encode(\json_encode($props)),
  1174. 'tpl' => 'grammar',
  1175. ];
  1176. break;
  1177. default:
  1178. $output = $props['word'];
  1179. break;
  1180. }
  1181. return $output;
  1182. }
  1183. private function render_video()
  1184. {
  1185. $url = $this->get_param($this->param, "url", 1);
  1186. $style = $this->get_param($this->param, "style", 2, 'modal');
  1187. $title = $this->get_param($this->param, "title", 3);
  1188. $props = [
  1189. "url" => $url,
  1190. 'title' => $title,
  1191. 'style' => $style,
  1192. ];
  1193. switch ($this->format) {
  1194. case 'react':
  1195. $output = [
  1196. 'props' => base64_encode(\json_encode($props)),
  1197. 'html' => "",
  1198. 'text' => $props['title'],
  1199. 'tag' => 'span',
  1200. 'tpl' => 'video',
  1201. ];
  1202. break;
  1203. case 'unity':
  1204. $output = [
  1205. 'props' => base64_encode(\json_encode($props)),
  1206. 'tpl' => 'video',
  1207. ];
  1208. break;
  1209. default:
  1210. $output = $props['title'];
  1211. break;
  1212. }
  1213. return $output;
  1214. }
  1215. //论文后面的参考资料
  1216. private function render_ref()
  1217. {
  1218. $references = array();
  1219. $counter = 0;
  1220. if (isset($GLOBALS['ref_sent'])) {
  1221. $hasBooks = array();
  1222. $book_titles = BookSeries::select(['book', 'paragraph', 'title', 'sn'])
  1223. ->orderBy('sn', 'DESC')->get();
  1224. $bTitles = array();
  1225. foreach ($book_titles as $key => $book) {
  1226. $bTitles[] = [
  1227. 'book' => $book->book,
  1228. 'paragraph' => $book->paragraph,
  1229. 'title' => $book->title
  1230. ];
  1231. }
  1232. foreach ($GLOBALS['ref_sent'] as $key => $ref) {
  1233. $books = array_filter($bTitles, function ($value) use ($ref) {
  1234. return $value['book'] === (int)$ref[0];
  1235. });
  1236. if (count($books) > 0) {
  1237. foreach ($books as $key => $book) {
  1238. if ($book['paragraph'] < (int)$ref[1]) {
  1239. if (!isset($hasBooks[$book['title']])) {
  1240. $hasBooks[$book['title']] = 1;
  1241. $counter++;
  1242. $references[] = [
  1243. 'sn' => $counter,
  1244. 'title' => $book['title'],
  1245. 'copyright' => 'CSCD V4 VRI 2008'
  1246. ];
  1247. }
  1248. }
  1249. }
  1250. }
  1251. }
  1252. }
  1253. $props = [
  1254. "pali" => $references,
  1255. ];
  1256. switch ($this->format) {
  1257. case 'react':
  1258. $output = [
  1259. 'props' => base64_encode(\json_encode($props)),
  1260. 'html' => '',
  1261. 'tag' => 'div',
  1262. 'tpl' => 'reference',
  1263. ];
  1264. break;
  1265. case 'unity':
  1266. $output = [
  1267. 'props' => base64_encode(\json_encode($props)),
  1268. 'tpl' => 'reference',
  1269. ];
  1270. break;
  1271. case 'markdown':
  1272. $output = '';
  1273. foreach ($references as $key => $reference) {
  1274. $output .= '[' . $reference['sn'] . '] **' . ucfirst($reference['title']) . '** ';
  1275. $output .= $reference['copyright'] . "\n\n";
  1276. }
  1277. break;
  1278. default:
  1279. $output = '';
  1280. foreach ($references as $key => $reference) {
  1281. $output .= '[' . $reference['sn'] . '] ' . ucfirst($reference['title']) . ' ';
  1282. $output .= $reference['copyright'] . "\n";
  1283. }
  1284. break;
  1285. }
  1286. return $output;
  1287. }
  1288. private function render_dict_pref()
  1289. {
  1290. $currPage = $this->get_param($this->param, "page", 1, 1);
  1291. $pageSize = $this->get_param($this->param, "size", 2, 100);
  1292. $props = [
  1293. "currPage" => $currPage,
  1294. 'pageSize' => $pageSize,
  1295. ];
  1296. switch ($this->format) {
  1297. case 'react':
  1298. $output = [
  1299. 'props' => base64_encode(\json_encode($props)),
  1300. 'html' => "",
  1301. 'text' => '',
  1302. 'tag' => 'div',
  1303. 'tpl' => 'dict-pref',
  1304. ];
  1305. break;
  1306. case 'unity':
  1307. $output = [
  1308. 'props' => base64_encode(\json_encode($props)),
  1309. 'tpl' => 'dict-pref',
  1310. ];
  1311. break;
  1312. default:
  1313. $output = 'dict-pref';
  1314. break;
  1315. }
  1316. return $output;
  1317. }
  1318. private function render_ai()
  1319. {
  1320. $model = $this->get_param($this->param, "model", 1, 1);
  1321. $props = [
  1322. "model" => $model,
  1323. ];
  1324. switch ($this->format) {
  1325. case 'react':
  1326. $output = [
  1327. 'props' => base64_encode(\json_encode($props)),
  1328. 'html' => "",
  1329. 'text' => '',
  1330. 'tag' => 'div',
  1331. 'tpl' => 'ai',
  1332. ];
  1333. break;
  1334. case 'unity':
  1335. $output = [
  1336. 'props' => base64_encode(\json_encode($props)),
  1337. 'tpl' => 'ai',
  1338. ];
  1339. break;
  1340. case 'text':
  1341. $output = 'ai';
  1342. break;
  1343. case 'prompt':
  1344. $output = '';
  1345. break;
  1346. default:
  1347. $output = 'ai';
  1348. break;
  1349. }
  1350. return $output;
  1351. }
  1352. private function get_param(array $param, string $name, int $id, string $default = '')
  1353. {
  1354. if (isset($param[$name])) {
  1355. return trim($param[$name]);
  1356. } else if (isset($param["{$id}"])) {
  1357. return trim($param["{$id}"]);
  1358. } else {
  1359. return $default;
  1360. }
  1361. }
  1362. private function mb_trim($str, string $character_mask = ' ', $charset = "UTF-8")
  1363. {
  1364. $start = 0;
  1365. $end = mb_strlen($str, $charset) - 1;
  1366. $chars = preg_split('//u', $character_mask, -1, PREG_SPLIT_NO_EMPTY);
  1367. while ($start <= $end && in_array(mb_substr($str, $start, 1, $charset), $chars)) {
  1368. $start++;
  1369. }
  1370. while ($end >= $start && in_array(mb_substr($str, $end, 1, $charset), $chars)) {
  1371. $end--;
  1372. }
  1373. return mb_substr($str, $start, $end - $start + 1, $charset);
  1374. }
  1375. }