TemplateRender.php 49 KB

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