TemplateRender.php 54 KB

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