2
0

TemplateRender.php 48 KB

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