TemplateRender.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  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. default:
  153. if (mb_substr($tpl_name, 0, 4, "UTF-8") === 'Tpl:') {
  154. $result = $this->render_tpl($tpl_name);
  155. } else {
  156. $result = [
  157. 'props' => base64_encode(\json_encode([])),
  158. 'html' => '',
  159. 'tag' => 'span',
  160. 'tpl' => 'unknown',
  161. ];
  162. }
  163. break;
  164. }
  165. return $result;
  166. }
  167. public function render_tpl($name)
  168. {
  169. $article = app(ArticleService::class)->getRawByTitle($name);
  170. $content = $article->content;
  171. if (count($this->param) > 0) {
  172. $m = new \Mustache_Engine(array(
  173. 'entity_flags' => ENT_QUOTES,
  174. 'escape' => function ($value) {
  175. return $value;
  176. }
  177. ));
  178. $content = $m->render($content, $this->param);
  179. }
  180. return [
  181. 'props' => base64_encode(\json_encode(['content' => $content])),
  182. 'html' => $content,
  183. 'tag' => 'span',
  184. 'tpl' => 'tpl',
  185. ];
  186. }
  187. public function getTermProps($word, $tag = null, $channel = null)
  188. {
  189. if ($channel && !empty($channel)) {
  190. $channelId = $channel;
  191. } else {
  192. if (count($this->channel_id) > 0) {
  193. $channelId = $this->channel_id[0];
  194. } else {
  195. $channelId = null;
  196. }
  197. }
  198. if (count($this->channelInfo) === 0) {
  199. if (!empty($channel)) {
  200. $channelInfo = Channel::where('uid', $channel)->first();
  201. if (!$channelInfo) {
  202. unset($channelInfo);
  203. }
  204. }
  205. if (!isset($channelInfo)) {
  206. Log::warning('channel is null');
  207. $output = [
  208. "word" => $word,
  209. 'innerHtml' => '',
  210. ];
  211. return $output;
  212. }
  213. } else {
  214. $channelInfo = $this->channelInfo[0];
  215. }
  216. if (Str::isUuid($channelId)) {
  217. $lang = Channel::where('uid', $channelId)->value('lang');
  218. if (!empty($lang)) {
  219. $langFamily = explode('-', $lang)[0];
  220. } else {
  221. $langFamily = 'zh';
  222. }
  223. $this->info("term:{$word} 先查属于这个channel 的", 'term');
  224. $this->info('channel id' . $channelId, 'term');
  225. $table = DhammaTerm::where("word", $word)
  226. ->where('channal', $channelId);
  227. if ($tag && !empty($tag)) {
  228. $table = $table->where('tag', $tag);
  229. }
  230. $tplParam = $table->orderBy('updated_at', 'desc')
  231. ->first();
  232. $studioId = $channelInfo->owner_uid;
  233. } else {
  234. $tplParam = false;
  235. $lang = '';
  236. $langFamily = '';
  237. $studioId = $this->studioId;
  238. }
  239. if (!$tplParam) {
  240. if (Str::isUuid($studioId)) {
  241. /**
  242. * 没有,再查这个studio的
  243. * 按照语言过滤
  244. * 完全匹配的优先
  245. * 语族匹配也行
  246. */
  247. $this->info("没有-再查这个studio的", 'term');
  248. $table = DhammaTerm::where("word", $word);
  249. if (!empty($tag)) {
  250. $table = $table->where('tag', $tag);
  251. }
  252. $termsInStudio = $table->where('owner', $channelInfo->owner_uid)
  253. ->orderBy('updated_at', 'desc')
  254. ->get();
  255. if (count($termsInStudio) > 0) {
  256. $list = array();
  257. foreach ($termsInStudio as $key => $term) {
  258. if (empty($term->channal)) {
  259. if ($term->language === $lang) {
  260. $list[$term->guid] = 2;
  261. } else if (strpos($term->language, $langFamily) !== false) {
  262. $list[$term->guid] = 1;
  263. }
  264. }
  265. }
  266. if (count($list) > 0) {
  267. arsort($list);
  268. foreach ($list as $key => $one) {
  269. foreach ($termsInStudio as $term) {
  270. if ($term->guid === $key) {
  271. $tplParam = $term;
  272. break;
  273. }
  274. }
  275. break;
  276. }
  277. }
  278. }
  279. }
  280. }
  281. if (!$tplParam) {
  282. $this->info("没有,再查社区", 'term');
  283. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  284. $table = DhammaTerm::where("word", $word);
  285. if (!empty($tag)) {
  286. $table = $table->where('tag', $tag);
  287. }
  288. $tplParam = $table->where('channal', $community_channel)
  289. ->first();
  290. if ($tplParam) {
  291. $isCommunity = true;
  292. } else {
  293. $this->info("查社区没有", 'term');
  294. }
  295. }
  296. $output = [
  297. "word" => $word,
  298. "parentChannelId" => $channelId,
  299. "parentStudioId" => $channelInfo->owner_uid,
  300. ];
  301. $innerString = $output["word"];
  302. if ($tplParam) {
  303. $output["id"] = $tplParam->guid;
  304. $output["meaning"] = $tplParam->meaning;
  305. $output["channel"] = $tplParam->channal;
  306. if (!empty($tplParam->note)) {
  307. $mdRender = new MdRender(['format' => $this->format]);
  308. $output['note'] = $mdRender->convert($tplParam->note, $this->channel_id);
  309. }
  310. if (isset($isCommunity)) {
  311. $output["isCommunity"] = true;
  312. }
  313. $innerString = "{$output["meaning"]}({$output["word"]})";
  314. if (!empty($tplParam->other_meaning)) {
  315. $output["meaning2"] = $tplParam->other_meaning;
  316. }
  317. }
  318. $output['innerHtml'] = $innerString;
  319. return $output;
  320. }
  321. private function render_term()
  322. {
  323. $word = $this->get_param($this->param, "word", 1);
  324. $props = $this->getTermProps($word, '');
  325. $output = $props['word'];
  326. switch ($this->format) {
  327. case 'react':
  328. $output = [
  329. 'props' => base64_encode(\json_encode($props)),
  330. 'html' => $props['innerHtml'],
  331. 'tag' => 'span',
  332. 'tpl' => 'term',
  333. ];
  334. break;
  335. case 'unity':
  336. $output = [
  337. 'props' => base64_encode(\json_encode($props)),
  338. 'tpl' => 'term',
  339. ];
  340. break;
  341. case 'html':
  342. if (isset($props["meaning"])) {
  343. $GLOBALS[$this->glossaryKey][$props["word"]] = $props['meaning'];
  344. $key = 'term-' . $props["word"];
  345. $termHead = "<a href='#'>" . $props['meaning'] . "</a>";
  346. if (isset($GLOBALS[$key])) {
  347. $output = $termHead;
  348. } else {
  349. $GLOBALS[$key] = 1;
  350. $output = $termHead . '(<em>' . $props["word"] . '</em>)';
  351. }
  352. } else {
  353. $output = $props["word"];
  354. }
  355. break;
  356. case 'text':
  357. if (isset($props["meaning"])) {
  358. $key = 'term-' . $props["word"];
  359. if (isset($GLOBALS[$key])) {
  360. $output = $props["meaning"];
  361. } else {
  362. $GLOBALS[$key] = 1;
  363. $output = $props["meaning"] . '(' . $props["word"] . ')';
  364. }
  365. } else {
  366. $output = $props["word"];
  367. }
  368. break;
  369. case 'tex':
  370. if (isset($props["meaning"])) {
  371. $key = 'term-' . $props["word"];
  372. if (isset($GLOBALS[$key])) {
  373. $output = $props["meaning"];
  374. } else {
  375. $GLOBALS[$key] = 1;
  376. $output = $props["meaning"] . '(' . $props["word"] . ')';
  377. }
  378. } else {
  379. $output = $props["word"];
  380. }
  381. break;
  382. case 'simple':
  383. if (isset($props["meaning"])) {
  384. $output = $props["meaning"];
  385. } else {
  386. $output = $props["word"];
  387. }
  388. break;
  389. case 'markdown':
  390. if (isset($props["meaning"])) {
  391. $key = 'term-' . $props["word"];
  392. if (isset($GLOBALS[$key]) && $GLOBALS[$key] === 1) {
  393. $GLOBALS[$key]++;
  394. $output = $props["meaning"];
  395. } else {
  396. $GLOBALS[$key] = 1;
  397. $output = $props["meaning"] . '(' . $props["word"] . ')';
  398. }
  399. } else {
  400. $output = $props["word"];
  401. }
  402. //如果有内容且第一次出现,显示为脚注
  403. if (!empty($props["note"]) && $GLOBALS[$key] === 1) {
  404. if (isset($GLOBALS['note_sn'])) {
  405. $GLOBALS['note_sn']++;
  406. } else {
  407. $GLOBALS['note_sn'] = 1;
  408. $GLOBALS['note'] = array();
  409. }
  410. $content = $props["note"];
  411. $output .= '[^' . $GLOBALS['note_sn'] . ']';
  412. $GLOBALS['note'][] = [
  413. 'sn' => $GLOBALS['note_sn'],
  414. 'trigger' => '',
  415. 'content' => $content,
  416. ];
  417. }
  418. break;
  419. default:
  420. if (isset($props["meaning"])) {
  421. $output = $props["meaning"];
  422. } else {
  423. $output = $props["word"];
  424. }
  425. break;
  426. }
  427. return $output;
  428. }
  429. private function render_note()
  430. {
  431. $note = $this->get_param($this->param, "text", 1);
  432. $trigger = $this->get_param($this->param, "trigger", 2);
  433. $props = ["note" => $note];
  434. $innerString = "";
  435. if (!empty($trigger)) {
  436. $props["trigger"] = $trigger;
  437. $innerString = $props["trigger"];
  438. }
  439. if ($this->format === 'unity') {
  440. $props["note"] = MdRender::render(
  441. $props["note"],
  442. $this->channel_id,
  443. null,
  444. 'read',
  445. 'translation',
  446. 'markdown',
  447. 'unity'
  448. );
  449. }
  450. $output = $note;
  451. switch ($this->format) {
  452. case 'react':
  453. $output = [
  454. 'props' => base64_encode(\json_encode($props)),
  455. 'html' => $innerString,
  456. 'tag' => 'span',
  457. 'tpl' => 'note',
  458. ];
  459. break;
  460. case 'unity':
  461. $output = [
  462. 'props' => base64_encode(\json_encode($props)),
  463. 'tpl' => 'note',
  464. ];
  465. break;
  466. case 'html':
  467. if (isset($GLOBALS['note_sn'])) {
  468. $GLOBALS['note_sn']++;
  469. } else {
  470. $GLOBALS['note_sn'] = 1;
  471. $GLOBALS['note'] = array();
  472. }
  473. $GLOBALS['note'][] = [
  474. 'sn' => $GLOBALS['note_sn'],
  475. 'trigger' => $trigger,
  476. 'content' => MdRender::render(
  477. $props["note"],
  478. $this->channel_id,
  479. null,
  480. 'read',
  481. 'translation',
  482. 'markdown',
  483. 'html'
  484. ),
  485. ];
  486. $link = "<a href='#footnote-" . $GLOBALS['note_sn'] . "' name='note-" . $GLOBALS['note_sn'] . "'>";
  487. if (empty($trigger)) {
  488. $output = $link . "<sup>[" . $GLOBALS['note_sn'] . "]</sup></a>";
  489. } else {
  490. $output = $link . $trigger . "</a>";
  491. }
  492. break;
  493. case 'text':
  494. $output = $trigger;
  495. break;
  496. case 'tex':
  497. $output = $trigger;
  498. break;
  499. case 'simple':
  500. $output = '';
  501. break;
  502. case 'markdown':
  503. if (isset($GLOBALS['note_sn'])) {
  504. $GLOBALS['note_sn']++;
  505. } else {
  506. $GLOBALS['note_sn'] = 1;
  507. $GLOBALS['note'] = array();
  508. }
  509. $content = MdRender::render(
  510. $props["note"],
  511. $this->channel_id,
  512. null,
  513. 'read',
  514. 'translation',
  515. 'markdown',
  516. 'markdown'
  517. );
  518. $output = '[^' . $GLOBALS['note_sn'] . ']';
  519. $GLOBALS['note'][] = [
  520. 'sn' => $GLOBALS['note_sn'],
  521. 'trigger' => $trigger,
  522. 'content' => $content,
  523. ];
  524. //$output = '<footnote id="'.$GLOBALS['note_sn'].'">'.$content.'</footnote>';
  525. break;
  526. default:
  527. $output = '';
  528. break;
  529. }
  530. return $output;
  531. }
  532. private function render_nissaya()
  533. {
  534. $pali = $this->get_param($this->param, "pali", 1);
  535. $meaning = $this->get_param($this->param, "meaning", 2);
  536. $innerString = "";
  537. $props = [
  538. "pali" => $pali,
  539. "meaning" => explode('=', $meaning),
  540. "lang" => $this->lang,
  541. ];
  542. switch ($this->format) {
  543. case 'react':
  544. $output = [
  545. 'props' => base64_encode(\json_encode($props)),
  546. 'html' => $innerString,
  547. 'tag' => 'span',
  548. 'tpl' => 'nissaya',
  549. ];
  550. break;
  551. case 'unity':
  552. $output = [
  553. 'props' => base64_encode(\json_encode($props)),
  554. 'tpl' => 'nissaya',
  555. ];
  556. break;
  557. case 'prompt':
  558. $output = Tools::MyToRm($pali) . ':' . end($props["meaning"]);
  559. break;
  560. default:
  561. $output = $pali . '၊' . $meaning;
  562. break;
  563. }
  564. return $output;
  565. }
  566. private function render_exercise()
  567. {
  568. $id = $this->get_param($this->param, "id", 1);
  569. $title = $this->get_param($this->param, "title", 1);
  570. $props = [
  571. "id" => $id,
  572. "title" => $title,
  573. "channel" => $this->channel_id[0],
  574. ];
  575. switch ($this->format) {
  576. case 'react':
  577. $output = [
  578. 'props' => base64_encode(\json_encode($props)),
  579. 'html' => "",
  580. 'tag' => 'span',
  581. 'tpl' => 'exercise',
  582. ];
  583. break;
  584. case 'unity':
  585. $output = [
  586. 'props' => base64_encode(\json_encode($props)),
  587. 'tpl' => 'exercise',
  588. ];
  589. break;
  590. case 'text':
  591. $output = $title;
  592. break;
  593. case 'tex':
  594. $output = $title;
  595. break;
  596. case 'simple':
  597. $output = $title;
  598. break;
  599. default:
  600. $output = '';
  601. break;
  602. }
  603. return $output;
  604. }
  605. private function render_article()
  606. {
  607. $type = $this->get_param($this->param, "type", 1);
  608. $id = $this->get_param($this->param, "id", 2);
  609. $title = $this->get_param($this->param, "title", 3);
  610. $channel = $this->get_param($this->param, "channel", 4);
  611. $style = $this->get_param($this->param, "style", 5);
  612. $book = $this->get_param($this->param, "book", 6);
  613. $paragraphs = $this->get_param($this->param, "paragraphs", 7);
  614. $anthology = $this->get_param($this->param, "anthology", 8);
  615. if ($type === 'chapter' && empty($id)) {
  616. $book = (int)$book;
  617. $paragraphs = (int)$paragraphs;
  618. $id = "{$book}-{$paragraphs}";
  619. }
  620. $props = [
  621. "type" => $type,
  622. "id" => $id,
  623. 'style' => $style,
  624. ];
  625. if (!empty($channel)) {
  626. $props['channel'] = $channel;
  627. }
  628. if (!empty($title)) {
  629. $props['title'] = $title;
  630. }
  631. if (!empty($book)) {
  632. $props['book'] = $book;
  633. }
  634. if (!empty($paragraphs)) {
  635. $props['paragraphs'] = $paragraphs;
  636. }
  637. if (!empty($anthology)) {
  638. $props['anthology'] = $anthology;
  639. }
  640. if (is_array($this->channel_id)) {
  641. $props['parentChannels'] = $this->channel_id;
  642. }
  643. switch ($this->format) {
  644. case 'react':
  645. $output = [
  646. 'props' => base64_encode(\json_encode($props)),
  647. 'html' => "",
  648. 'text' => $title,
  649. 'tag' => 'span',
  650. 'tpl' => 'article',
  651. ];
  652. break;
  653. case 'unity':
  654. $output = [
  655. 'props' => base64_encode(\json_encode($props)),
  656. 'tpl' => 'article',
  657. ];
  658. break;
  659. case 'text':
  660. $output = $title;
  661. break;
  662. case 'tex':
  663. $output = $title;
  664. break;
  665. case 'simple':
  666. $output = $title;
  667. break;
  668. default:
  669. $output = '';
  670. break;
  671. }
  672. return $output;
  673. }
  674. private function render_quote()
  675. {
  676. $paraId = $this->get_param($this->param, "para", 1);
  677. $channelId = $this->channel_id[0];
  678. $props = Cache::remember(
  679. "/quote/{$channelId}/{$paraId}",
  680. config('mint.cache.expire'),
  681. function () use ($paraId, $channelId) {
  682. $para = \explode('-', $paraId);
  683. $output = [
  684. "paraId" => $paraId,
  685. "channel" => $channelId,
  686. "innerString" => $paraId,
  687. ];
  688. if (count($para) < 2) {
  689. return $output;
  690. }
  691. $PaliText = PaliText::where("book", $para[0])
  692. ->where("paragraph", $para[1])
  693. ->select(['toc', 'path'])
  694. ->first();
  695. if ($PaliText) {
  696. $output["pali"] = $PaliText->toc;
  697. $output["paliPath"] = \json_decode($PaliText->path);
  698. $output["innerString"] = $PaliText->toc;
  699. }
  700. return $output;
  701. }
  702. );
  703. switch ($this->format) {
  704. case 'react':
  705. $output = [
  706. 'props' => base64_encode(\json_encode($props)),
  707. 'html' => $props["innerString"],
  708. 'tag' => 'span',
  709. 'tpl' => 'quote',
  710. ];
  711. break;
  712. case 'unity':
  713. $output = [
  714. 'props' => base64_encode(\json_encode($props)),
  715. 'tpl' => 'quote',
  716. ];
  717. break;
  718. case 'text':
  719. $output = $props["innerString"];
  720. break;
  721. case 'tex':
  722. $output = $props["innerString"];
  723. break;
  724. case 'simple':
  725. $output = $props["innerString"];
  726. break;
  727. default:
  728. $output = $props["innerString"];
  729. break;
  730. }
  731. return $output;
  732. }
  733. private function render_quote_link()
  734. {
  735. $type = $this->get_param($this->param, "type", 1);
  736. $title = $this->get_param($this->param, "title", 6, '');
  737. $bookName = $this->get_param($this->param, "bookname", 2, '');
  738. $volume = $this->get_param($this->param, "volume", 3);
  739. $page = $this->get_param($this->param, "page", 4, '');
  740. $style = $this->get_param($this->param, "style", 5, 'modal');
  741. $book = $this->get_param($this->param, "book", 7, false);
  742. $para = $this->get_param($this->param, "para", 8, false);
  743. $props = [
  744. 'type' => $type,
  745. 'style' => $style,
  746. 'found' => true,
  747. ];
  748. if (!empty($bookName) && $volume !== '' && !empty($page)) {
  749. $props['bookName'] = $bookName;
  750. $props['volume'] = (int)$volume;
  751. $props['page'] = $page;
  752. $props['found'] = true;
  753. } else if ($book && $para) {
  754. /**
  755. * 没有指定书名,根据book para 查询
  756. */
  757. if ($type === 'c') {
  758. //按照章节名称显示
  759. $path = PaliTextApi::getChapterPath($book, $para);
  760. if ($path) {
  761. $path = json_decode($path, true);
  762. }
  763. if ($path && is_array($path) && count($path) > 2) {
  764. $props['bookName'] = strtolower($path[0]['title']);
  765. $props['chapter'] = strtolower(end($path)['title']);
  766. $props['found'] = true;
  767. } else {
  768. $props['found'] = false;
  769. }
  770. } else {
  771. $pageInfo = $this->pageInfoByPara($type, $book, $para);
  772. if ($pageInfo['found']) {
  773. $props['bookName'] = $pageInfo['bookName'];
  774. $props['volume'] = $pageInfo['volume'];
  775. $props['page'] = $pageInfo['page'];
  776. $props['found'] = true;
  777. } else {
  778. $props['found'] = false;
  779. }
  780. }
  781. } else if ($title) {
  782. //没有书号用title查询
  783. //$tmpTitle = explode('။',$title);
  784. for ($i = mb_strlen($title, 'UTF-8'); $i > 0; $i--) {
  785. $mTitle = mb_substr($title, 0, $i);
  786. $has = array_search($mTitle, array_column(BookTitle::my(), 'title2'));
  787. Log::debug('run', ['title' => $mTitle, 'has' => $has]);
  788. if ($has !== false) {
  789. $tmpBookTitle = $mTitle;
  790. $tmpBookPage = mb_substr($title, $i);
  791. $tmpBookPage = $this->mb_trim($tmpBookPage, '၊။');
  792. break;
  793. }
  794. }
  795. if (isset($tmpBookTitle)) {
  796. Log::debug('book title found', ['title' => $tmpBookTitle, 'page' => $tmpBookPage]);
  797. //$tmpBookTitle = $tmpTitle[0];
  798. //$tmpBookPage = $tmpTitle[1];
  799. $tmpBookPage = (int)str_replace(
  800. ['၁', '၂', '၃', '၄', '၅', '၆', '၇', '၈', '၉', '၀'],
  801. ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
  802. $tmpBookPage
  803. );
  804. $found_key = array_search($tmpBookTitle, array_column(BookTitle::my(), 'title2'));
  805. if ($found_key !== false) {
  806. $props['bookName'] = BookTitle::my()[$found_key]['bookname'];
  807. $props['volume'] = BookTitle::my()[$found_key]['volume'];
  808. $props['page'] = $tmpBookPage;
  809. if (!empty($props['bookName'])) {
  810. $found_title = array_search($props['bookName'], array_column(BookTitle::my(), 'bookname'));
  811. if ($found_title === false) {
  812. $props['found'] = false;
  813. }
  814. }
  815. } else {
  816. //没找到,返回术语和页码
  817. $props['found'] = false;
  818. $props['bookName'] = $tmpBookTitle;
  819. $props['page'] = $tmpBookPage;
  820. $props['volume'] = 0;
  821. }
  822. }
  823. } else {
  824. Log::debug('book title not found');
  825. $props['found'] = false;
  826. }
  827. if ($book && $para) {
  828. $props['book'] = $book;
  829. $props['para'] = $para;
  830. }
  831. if ($title) {
  832. $props['title'] = $title;
  833. }
  834. $text = '';
  835. if (isset($props['bookName'])) {
  836. $searchField = '';
  837. switch ($type) {
  838. case 'm':
  839. $searchField = 'm_title';
  840. break;
  841. case 'p':
  842. $searchField = 'p_title';
  843. break;
  844. }
  845. $found_title = array_search($props['bookName'], array_column(BookTitle::get(), $searchField));
  846. if ($found_title === false) {
  847. $props['found'] = false;
  848. }
  849. $term = $this->getTermProps($props['bookName'], ':quote:');
  850. $props['term'] = $term;
  851. if (isset($term['id'])) {
  852. $props['bookNameLocal'] = $term['meaning'];
  853. $text .= $term['meaning'];
  854. } else {
  855. $text .= $bookName;
  856. }
  857. }
  858. if (isset($props['volume']) && isset($props['page'])) {
  859. $text .= " {$volume}.{$page}";
  860. }
  861. switch ($this->format) {
  862. case 'react':
  863. $output = [
  864. 'props' => base64_encode(\json_encode($props)),
  865. 'html' => '',
  866. 'tag' => 'span',
  867. 'tpl' => 'quote-link',
  868. ];
  869. break;
  870. case 'unity':
  871. $output = [
  872. 'props' => base64_encode(\json_encode($props)),
  873. 'tpl' => 'quote-link',
  874. ];
  875. break;
  876. default:
  877. $output = $text;
  878. break;
  879. }
  880. return $output;
  881. }
  882. private function pageInfoByPara($type, $book, $para)
  883. {
  884. $output = array();
  885. $pageInfo = PageNumber::where('type', strtoupper($type))
  886. ->where('book', $book)
  887. ->where('paragraph', '<=', $para)
  888. ->orderBy('paragraph', 'desc')
  889. ->first();
  890. if ($pageInfo) {
  891. foreach (BookTitle::get() as $value) {
  892. if ($value['id'] === $pageInfo->pcd_book_id) {
  893. switch (strtoupper($type)) {
  894. case 'M':
  895. $key = 'm_title';
  896. break;
  897. case 'P':
  898. $key = 'p_title';
  899. break;
  900. case 'V':
  901. $key = 'v_title';
  902. break;
  903. default:
  904. $key = 'term';
  905. break;
  906. }
  907. $output['bookName'] = $value[$key];
  908. break;
  909. }
  910. }
  911. $output['volume'] = $pageInfo->volume;
  912. $output['page'] = $pageInfo->page;
  913. $output['found'] = true;
  914. } else {
  915. $output['found'] = false;
  916. }
  917. return $output;
  918. }
  919. private function render_sent()
  920. {
  921. $sid = $this->get_param($this->param, "id", 1);
  922. $channel = $this->get_param($this->param, "channel", 2);
  923. $text = $this->get_param($this->param, "text", 2, 'both');
  924. if (!empty($channel)) {
  925. $channels = explode(',', $channel);
  926. } else {
  927. $channels = $this->channel_id;
  928. }
  929. $sentInfo = explode('@', trim($sid));
  930. $sentId = $sentInfo[0];
  931. if (isset($sentInfo[1])) {
  932. $channels = [$sentInfo[1]];
  933. }
  934. $Sent = new CorpusController();
  935. $props = $Sent->getSentTpl(
  936. $sentId,
  937. $channels,
  938. $this->mode,
  939. true,
  940. $this->format
  941. );
  942. if ($props === false) {
  943. $props['error'] = "句子模版渲染错误。句子参数个数不符。应该是四个。";
  944. }
  945. if ($this->mode === 'read') {
  946. $tpl = "sentread";
  947. } else {
  948. $tpl = "sentedit";
  949. }
  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 ($text === 'both' || $text === '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 ($text === 'both' || $text === '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 ($text === 'both' || $text === '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 ($text === 'both' || $text === '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 ($text === 'both' || $text === '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 ($text === 'both' || $text === '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 ($text === 'both' || $text === '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 ($text === 'both' || $text === '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 ($text === '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. }