TemplateRender.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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 === false) {
  942. $props['error'] = "句子模版渲染错误。句子参数个数不符。应该是四个。";
  943. }
  944. if ($this->mode === 'read') {
  945. $tpl = "sentread";
  946. } else {
  947. $tpl = "sentedit";
  948. }
  949. $props['show'] = $show;
  950. //输出引用
  951. $arrSid = explode('-', $sid);
  952. $bookPara = array_slice($arrSid, 0, 2);
  953. if (!isset($GLOBALS['ref_sent'])) {
  954. $GLOBALS['ref_sent'] = array();
  955. }
  956. $GLOBALS['ref_sent'][] = $bookPara;
  957. switch ($this->format) {
  958. case 'react':
  959. $output = [
  960. 'props' => base64_encode(\json_encode($props)),
  961. 'html' => "",
  962. 'tag' => 'span',
  963. 'tpl' => $tpl,
  964. ];
  965. break;
  966. case 'unity':
  967. $output = [
  968. 'props' => base64_encode(\json_encode($props)),
  969. 'tpl' => $tpl,
  970. ];
  971. break;
  972. case 'text':
  973. $output = '';
  974. if (isset($props['origin']) && is_array($props['origin'])) {
  975. foreach ($props['origin'] as $key => $value) {
  976. $output .= $value['html'];
  977. }
  978. }
  979. if (isset($props['translation']) && is_array($props['translation'])) {
  980. foreach ($props['translation'] as $key => $value) {
  981. $output .= $value['html'];
  982. }
  983. }
  984. break;
  985. case 'prompt':
  986. $output = '';
  987. if ($show === 'both' || $show === 'origin') {
  988. if (isset($props['origin']) && is_array($props['origin'])) {
  989. foreach ($props['origin'] as $key => $value) {
  990. $output .= $value['html'];
  991. }
  992. }
  993. }
  994. if ($show === 'both' || $show === 'translation') {
  995. if (isset($props['translation']) && is_array($props['translation'])) {
  996. foreach ($props['translation'] as $key => $value) {
  997. $output .= $value['html'];
  998. }
  999. }
  1000. }
  1001. break;
  1002. case 'html':
  1003. $output = '';
  1004. $output .= '<span class="sentence">';
  1005. if ($show === 'both' || $show === 'origin') {
  1006. if (isset($props['origin']) && is_array($props['origin'])) {
  1007. foreach ($props['origin'] as $key => $value) {
  1008. $output .= '<span class="origin">' . $value['html'] . '</span>';
  1009. }
  1010. }
  1011. }
  1012. if ($show === 'both' || $show === 'translation') {
  1013. if (isset($props['translation']) && is_array($props['translation'])) {
  1014. foreach ($props['translation'] as $key => $value) {
  1015. $output .= '<span class="translation">' . $value['html'] . '</span>';
  1016. }
  1017. }
  1018. }
  1019. $output .= '</span>';
  1020. break;
  1021. case 'tex':
  1022. $output = '';
  1023. if (isset($props['translation']) && is_array($props['translation'])) {
  1024. foreach ($props['translation'] as $key => $value) {
  1025. $output .= $value['html'];
  1026. }
  1027. }
  1028. break;
  1029. case 'simple':
  1030. $output = '';
  1031. if ($show === 'both' || $show === 'origin') {
  1032. if (empty($output)) {
  1033. if (
  1034. isset($props['origin']) &&
  1035. is_array($props['origin']) &&
  1036. count($props['origin']) > 0
  1037. ) {
  1038. foreach ($props['origin'] as $key => $value) {
  1039. $output .= trim($value['html']);
  1040. }
  1041. }
  1042. }
  1043. }
  1044. if ($show === 'both' || $show === 'translation') {
  1045. if (
  1046. isset($props['translation']) &&
  1047. is_array($props['translation']) &&
  1048. count($props['translation']) > 0
  1049. ) {
  1050. foreach ($props['translation'] as $key => $value) {
  1051. $output .= trim($value['html']);
  1052. }
  1053. }
  1054. }
  1055. break;
  1056. case 'markdown':
  1057. $output = '';
  1058. if ($show === 'both' || $show === 'origin') {
  1059. if (
  1060. $this->options['origin'] === true ||
  1061. $this->options['origin'] === 'true'
  1062. ) {
  1063. if (isset($props['origin']) && is_array($props['origin'])) {
  1064. foreach ($props['origin'] as $key => $value) {
  1065. $output .= trim($value['html']);
  1066. }
  1067. }
  1068. }
  1069. }
  1070. if ($show === 'both' || $show === 'translation') {
  1071. if (
  1072. $this->options['translation'] === true ||
  1073. $this->options['translation'] === 'true'
  1074. ) {
  1075. if (
  1076. isset($props['translation']) &&
  1077. is_array($props['translation']) &&
  1078. count($props['translation']) > 0
  1079. ) {
  1080. foreach ($props['translation'] as $key => $value) {
  1081. $output .= trim($value['html']);
  1082. }
  1083. } else {
  1084. if ($show === 'translation') {
  1085. //无译文用原文代替
  1086. if (isset($props['origin']) && is_array($props['origin'])) {
  1087. foreach ($props['origin'] as $key => $value) {
  1088. $output .= trim($value['html']);
  1089. }
  1090. }
  1091. }
  1092. }
  1093. }
  1094. }
  1095. break;
  1096. default:
  1097. $output = '';
  1098. break;
  1099. }
  1100. return $output;
  1101. }
  1102. private function render_mermaid()
  1103. {
  1104. $text = json_decode(base64_decode($this->get_param($this->param, "text", 1)));
  1105. $props = ["text" => implode("\n", $text)];
  1106. switch ($this->format) {
  1107. case 'react':
  1108. $output = [
  1109. 'props' => base64_encode(\json_encode($props)),
  1110. 'html' => "mermaid",
  1111. 'tag' => 'div',
  1112. 'tpl' => 'mermaid',
  1113. ];
  1114. break;
  1115. case 'unity':
  1116. $output = [
  1117. 'props' => base64_encode(\json_encode($props)),
  1118. 'tpl' => 'mermaid',
  1119. ];
  1120. break;
  1121. case 'text':
  1122. $output = 'mermaid';
  1123. break;
  1124. case 'tex':
  1125. $output = 'mermaid';
  1126. break;
  1127. case 'simple':
  1128. $output = 'mermaid';
  1129. break;
  1130. default:
  1131. $output = 'mermaid';
  1132. break;
  1133. }
  1134. return $output;
  1135. }
  1136. private function render_qa()
  1137. {
  1138. $id = $this->get_param($this->param, "id", 1);
  1139. $style = $this->get_param($this->param, "style", 2);
  1140. $props = [
  1141. "type" => 'qa',
  1142. "id" => $id,
  1143. 'title' => '',
  1144. 'style' => $style,
  1145. ];
  1146. $qa = Discussion::where('id', $id)->first();
  1147. if ($qa) {
  1148. $props['title'] = $qa->title;
  1149. $props['resId'] = $qa->res_id;
  1150. $props['resType'] = $qa->res_type;
  1151. }
  1152. switch ($this->format) {
  1153. case 'react':
  1154. $output = [
  1155. 'props' => base64_encode(\json_encode($props)),
  1156. 'html' => "",
  1157. 'text' => $props['title'],
  1158. 'tag' => 'div',
  1159. 'tpl' => 'qa',
  1160. ];
  1161. break;
  1162. case 'unity':
  1163. $output = [
  1164. 'props' => base64_encode(\json_encode($props)),
  1165. 'tpl' => 'qa',
  1166. ];
  1167. break;
  1168. default:
  1169. $output = $props['title'];
  1170. break;
  1171. }
  1172. return $output;
  1173. }
  1174. private function render_grammar_lookup()
  1175. {
  1176. $word = $this->get_param($this->param, "word", 1);
  1177. $props = ['word' => $word];
  1178. $localTermChannel = ChannelApi::getSysChannel(
  1179. "_System_Grammar_Term_" . strtolower($this->lang) . "_",
  1180. "_System_Grammar_Term_en_"
  1181. );
  1182. $term = $this->getTermProps($word, null, $localTermChannel);
  1183. $props['term'] = $term;
  1184. switch ($this->format) {
  1185. case 'react':
  1186. $output = [
  1187. 'props' => base64_encode(\json_encode($props)),
  1188. 'html' => "",
  1189. 'text' => $props['word'],
  1190. 'tag' => 'span',
  1191. 'tpl' => 'grammar',
  1192. ];
  1193. break;
  1194. case 'unity':
  1195. $output = [
  1196. 'props' => base64_encode(\json_encode($props)),
  1197. 'tpl' => 'grammar',
  1198. ];
  1199. break;
  1200. default:
  1201. $output = $props['word'];
  1202. break;
  1203. }
  1204. return $output;
  1205. }
  1206. private function render_video()
  1207. {
  1208. $url = $this->get_param($this->param, "url", 1);
  1209. $style = $this->get_param($this->param, "style", 2, 'modal');
  1210. $title = $this->get_param($this->param, "title", 3);
  1211. $props = [
  1212. "url" => $url,
  1213. 'title' => $title,
  1214. 'style' => $style,
  1215. ];
  1216. switch ($this->format) {
  1217. case 'react':
  1218. $output = [
  1219. 'props' => base64_encode(\json_encode($props)),
  1220. 'html' => "",
  1221. 'text' => $props['title'],
  1222. 'tag' => 'span',
  1223. 'tpl' => 'video',
  1224. ];
  1225. break;
  1226. case 'unity':
  1227. $output = [
  1228. 'props' => base64_encode(\json_encode($props)),
  1229. 'tpl' => 'video',
  1230. ];
  1231. break;
  1232. default:
  1233. $output = $props['title'];
  1234. break;
  1235. }
  1236. return $output;
  1237. }
  1238. //论文后面的参考资料
  1239. private function render_ref()
  1240. {
  1241. $references = array();
  1242. $counter = 0;
  1243. if (isset($GLOBALS['ref_sent'])) {
  1244. $hasBooks = array();
  1245. $book_titles = BookSeries::select(['book', 'paragraph', 'title', 'sn'])
  1246. ->orderBy('sn', 'DESC')->get();
  1247. $bTitles = array();
  1248. foreach ($book_titles as $key => $book) {
  1249. $bTitles[] = [
  1250. 'book' => $book->book,
  1251. 'paragraph' => $book->paragraph,
  1252. 'title' => $book->title
  1253. ];
  1254. }
  1255. foreach ($GLOBALS['ref_sent'] as $key => $ref) {
  1256. $books = array_filter($bTitles, function ($value) use ($ref) {
  1257. return $value['book'] === (int)$ref[0];
  1258. });
  1259. if (count($books) > 0) {
  1260. foreach ($books as $key => $book) {
  1261. if ($book['paragraph'] < (int)$ref[1]) {
  1262. if (!isset($hasBooks[$book['title']])) {
  1263. $hasBooks[$book['title']] = 1;
  1264. $counter++;
  1265. $references[] = [
  1266. 'sn' => $counter,
  1267. 'title' => $book['title'],
  1268. 'copyright' => 'CSCD V4 VRI 2008'
  1269. ];
  1270. }
  1271. }
  1272. }
  1273. }
  1274. }
  1275. }
  1276. $props = [
  1277. "pali" => $references,
  1278. ];
  1279. switch ($this->format) {
  1280. case 'react':
  1281. $output = [
  1282. 'props' => base64_encode(\json_encode($props)),
  1283. 'html' => '',
  1284. 'tag' => 'div',
  1285. 'tpl' => 'reference',
  1286. ];
  1287. break;
  1288. case 'unity':
  1289. $output = [
  1290. 'props' => base64_encode(\json_encode($props)),
  1291. 'tpl' => 'reference',
  1292. ];
  1293. break;
  1294. case 'markdown':
  1295. $output = '';
  1296. foreach ($references as $key => $reference) {
  1297. $output .= '[' . $reference['sn'] . '] **' . ucfirst($reference['title']) . '** ';
  1298. $output .= $reference['copyright'] . "\n\n";
  1299. }
  1300. break;
  1301. default:
  1302. $output = '';
  1303. foreach ($references as $key => $reference) {
  1304. $output .= '[' . $reference['sn'] . '] ' . ucfirst($reference['title']) . ' ';
  1305. $output .= $reference['copyright'] . "\n";
  1306. }
  1307. break;
  1308. }
  1309. return $output;
  1310. }
  1311. private function render_dict_pref()
  1312. {
  1313. $currPage = $this->get_param($this->param, "page", 1, 1);
  1314. $pageSize = $this->get_param($this->param, "size", 2, 100);
  1315. $props = [
  1316. "currPage" => $currPage,
  1317. 'pageSize' => $pageSize,
  1318. ];
  1319. switch ($this->format) {
  1320. case 'react':
  1321. $output = [
  1322. 'props' => base64_encode(\json_encode($props)),
  1323. 'html' => "",
  1324. 'text' => '',
  1325. 'tag' => 'div',
  1326. 'tpl' => 'dict-pref',
  1327. ];
  1328. break;
  1329. case 'unity':
  1330. $output = [
  1331. 'props' => base64_encode(\json_encode($props)),
  1332. 'tpl' => 'dict-pref',
  1333. ];
  1334. break;
  1335. default:
  1336. $output = 'dict-pref';
  1337. break;
  1338. }
  1339. return $output;
  1340. }
  1341. private function render_ai()
  1342. {
  1343. $model = $this->get_param($this->param, "model", 1, 1);
  1344. $props = [
  1345. "model" => $model,
  1346. ];
  1347. switch ($this->format) {
  1348. case 'react':
  1349. $output = [
  1350. 'props' => base64_encode(\json_encode($props)),
  1351. 'html' => "",
  1352. 'text' => '',
  1353. 'tag' => 'div',
  1354. 'tpl' => 'ai',
  1355. ];
  1356. break;
  1357. case 'unity':
  1358. $output = [
  1359. 'props' => base64_encode(\json_encode($props)),
  1360. 'tpl' => 'ai',
  1361. ];
  1362. break;
  1363. case 'text':
  1364. $output = 'ai';
  1365. break;
  1366. case 'prompt':
  1367. $output = '';
  1368. break;
  1369. default:
  1370. $output = 'ai';
  1371. break;
  1372. }
  1373. return $output;
  1374. }
  1375. private function get_param(array $param, string $name, int $id, string $default = '')
  1376. {
  1377. if (isset($param[$name])) {
  1378. return trim($param[$name]);
  1379. } else if (isset($param["{$id}"])) {
  1380. return trim($param["{$id}"]);
  1381. } else {
  1382. return $default;
  1383. }
  1384. }
  1385. private function mb_trim($str, string $character_mask = ' ', $charset = "UTF-8")
  1386. {
  1387. $start = 0;
  1388. $end = mb_strlen($str, $charset) - 1;
  1389. $chars = preg_split('//u', $character_mask, -1, PREG_SPLIT_NO_EMPTY);
  1390. while ($start <= $end && in_array(mb_substr($str, $start, 1, $charset), $chars)) {
  1391. $start++;
  1392. }
  1393. while ($end >= $start && in_array(mb_substr($str, $end, 1, $charset), $chars)) {
  1394. $end--;
  1395. }
  1396. return mb_substr($str, $start, $end - $start + 1, $charset);
  1397. }
  1398. }