2
0

TemplateRender.php 47 KB

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