MdRender.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. namespace App\Http\Api;
  3. use Illuminate\Support\Str;
  4. use mustache\mustache;
  5. use App\Models\DhammaTerm;
  6. use App\Models\PaliText;
  7. use App\Models\Channel;
  8. use App\Http\Controllers\CorpusController;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Facades\Log;
  11. define("STACK_DEEP",8);
  12. class MdRender{
  13. public static function tplSplit($tpl){
  14. $before = strpos($tpl,'{{');
  15. if($before === FALSE){
  16. //未找到
  17. return ['data'=>[$tpl,'',''],'error'=>0];
  18. }else{
  19. $pointer = $before;
  20. $stack = array();
  21. $stack[] = $pointer;
  22. $after = substr($tpl,$pointer+2) ;
  23. while (!empty($after) && count($stack)>0 && count($stack)<STACK_DEEP) {
  24. $nextBegin = strpos($after,"{{");
  25. $nextEnd = strpos($after,"}}");
  26. if($nextBegin !== FALSE){
  27. if($nextBegin < $nextEnd){
  28. //有嵌套找到最后一个}}
  29. $pointer = $pointer + 2 + $nextBegin;
  30. $stack[] = $pointer;
  31. $after = substr($tpl,$pointer+2);
  32. }else if($nextEnd !== FALSE){
  33. //无嵌套有结束
  34. $pointer = $pointer + 2 + $nextEnd;
  35. array_pop($stack);
  36. $after = substr($tpl,$pointer+2);
  37. }else{
  38. //无结束符 没找到
  39. break;
  40. }
  41. }else if($nextEnd !== FALSE){
  42. $pointer = $pointer + 2 + $nextEnd;
  43. array_pop($stack);
  44. $after = substr($tpl,$pointer+2);
  45. }else{
  46. //没找到
  47. break;
  48. }
  49. }
  50. if(count($stack)>0){
  51. if(count($stack) === STACK_DEEP){
  52. return ['data'=>[$tpl,'',''],'error'=>2];
  53. }else{
  54. //未关闭
  55. return ['data'=>[$tpl,'',''],'error'=>1];
  56. }
  57. }else{
  58. return ['data'=>
  59. [
  60. substr($tpl,0,$before),
  61. substr($tpl,$before,$pointer-$before+2),
  62. substr($tpl,$pointer+2)
  63. ],
  64. 'error'=>0
  65. ];
  66. }
  67. }
  68. }
  69. public static function wiki2xml(string $wiki):string{
  70. /**
  71. * 替换{{}} 到xml之前 要先把换行符号去掉
  72. */
  73. $wiki = str_replace("\n","",$wiki);
  74. /**
  75. * 把模版转换为xml
  76. */
  77. $remain = $wiki;
  78. $buffer = array();
  79. do {
  80. $arrWiki = MdRender::tplSplit($remain);
  81. $buffer[] = $arrWiki['data'][0];
  82. $tpl = $arrWiki['data'][1];
  83. if(!empty($tpl)){
  84. $pattern = "/\{\{(.+?)\|/";
  85. $replacement = '<MdTpl name="$1"><param>';
  86. $tpl = preg_replace($pattern,$replacement,$tpl);
  87. $tpl = str_replace("}}","</param></MdTpl>",$tpl);
  88. $tpl = str_replace("|","</param><param>",$tpl);
  89. /**
  90. * 替换变量名
  91. */
  92. $pattern = "/<param>([a-z]+?)=/";
  93. $replacement = '<param name="$1">';
  94. $tpl = preg_replace($pattern,$replacement,$tpl);
  95. $buffer[] = $tpl;
  96. }
  97. $remain = $arrWiki['data'][2];
  98. } while (!empty($remain));
  99. $html = implode('' , $buffer);
  100. $html = str_replace("<p>","<div>",$html);
  101. $html = str_replace("</p>","</div>",$html);
  102. $html = "<span>".$html."</span>";
  103. return $html;
  104. }
  105. public static function xmlQueryId(string $xml, string $id):string{
  106. try{
  107. $dom = simplexml_load_string($xml);
  108. }catch(\Exception $e){
  109. Log::error($e);
  110. return "<div></div>";
  111. }
  112. $tpl_list = $dom->xpath('//MdTpl');
  113. foreach ($tpl_list as $key => $tpl) {
  114. foreach ($tpl->children() as $param) {
  115. # 处理每个参数
  116. if($param->getName() === "param"){
  117. foreach($param->attributes() as $pa => $pa_value){
  118. $pValue = $pa_value->__toString();
  119. if($pa === "name" && $pValue === "id"){
  120. if($param->__toString() === $id){
  121. return $tpl->asXML();
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. return "<div></div>";
  129. }
  130. public static function take_sentence(string $xml):array{
  131. $output = [];
  132. try{
  133. $dom = simplexml_load_string($xml);
  134. }catch(\Exception $e){
  135. Log::error($e);
  136. return $output;
  137. }
  138. $tpl_list = $dom->xpath('//MdTpl');
  139. foreach ($tpl_list as $key => $tpl) {
  140. foreach($tpl->attributes() as $a => $a_value){
  141. if($a==="name"){
  142. if($a_value->__toString() ==="sent"){
  143. foreach ($tpl->children() as $param) {
  144. # 处理每个参数
  145. if($param->getName() === "param"){
  146. $sent = $param->__toString();
  147. if(!empty($sent)){
  148. $output[] = $sent;
  149. break;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. return $output;
  158. }
  159. public static function xml2tpl(string $xml, $channelId=[],$mode='read'):string{
  160. /**
  161. * 解析xml
  162. * 获取模版参数
  163. * 生成react 组件参数
  164. */
  165. try{
  166. $dom = simplexml_load_string($xml);
  167. }catch(\Exception $e){
  168. Log::error($e);
  169. Log::error($xml);
  170. return "<span>xml解析错误{$e}</span>";
  171. }
  172. if(!$dom){
  173. Log::error($xml);
  174. return "<span>xml解析错误</span>";
  175. }
  176. /*
  177. $doc = new \DOMDocument();
  178. $xml = str_replace('MdTpl','dfn',$xml);
  179. $ok = $doc->loadHTML($xml,LIBXML_HTML_NODEFDTD | LIBXML_DTDVALID);
  180. if(!$ok){
  181. return "<span>xml解析错误</span>";
  182. }
  183. */
  184. $tpl_list = $dom->xpath('//MdTpl');
  185. foreach ($tpl_list as $key => $tpl) {
  186. /**
  187. * 遍历 MdTpl 处理参数
  188. */
  189. $props = [];
  190. $tpl_name = '';
  191. foreach($tpl->attributes() as $a => $a_value){
  192. if($a==="name"){
  193. $tpl_name = $a_value;
  194. }
  195. }
  196. $param_id = 0;
  197. foreach ($tpl->children() as $param) {
  198. # 处理每个参数
  199. if($param->getName() === "param"){
  200. $param_id++;
  201. $paramName = "";
  202. foreach($param->attributes() as $pa => $pa_value){
  203. if($pa === "name"){
  204. $props["{$pa_value}"] = $param->__toString();
  205. $paramName = $pa_value;
  206. }
  207. }
  208. if(empty($paramName)){
  209. $props["{$param_id}"] = $param->__toString();
  210. }
  211. }
  212. }
  213. /**
  214. * 生成模版参数
  215. */
  216. $channelInfo = Channel::whereIn('uid',$channelId)->get();
  217. $tplRender = new TemplateRender($props,$channelInfo,$mode);
  218. $tplProps = $tplRender->render($tpl_name);
  219. if($tplProps){
  220. $tpl->addAttribute("props",$tplProps['props']);
  221. $tpl->addAttribute("tpl",$tplProps['tpl']);
  222. $tpl->addChild($tplProps['tag'],$tplProps['html']);
  223. }
  224. }
  225. $html = str_replace('<?xml version="1.0"?>','',$dom->asXML()) ;
  226. $html = str_replace(['<xml>','</xml>'],['<span>','</span>'],$html);
  227. return $html;
  228. }
  229. public static function render2($markdown,$channelId=[],$queryId=null,$mode='read',$channelType,$contentType="markdown"){
  230. if(empty($markdown)){
  231. return "<span></span>";
  232. }
  233. $wiki = MdRender::markdown2wiki($markdown,$channelType,$contentType);
  234. $html = MdRender::wiki2xml($wiki);
  235. if(!is_null($queryId)){
  236. $html = MdRender::xmlQueryId($html, $queryId);
  237. }
  238. $tpl = MdRender::xml2tpl($html,$channelId,$mode);
  239. //生成可展开组件
  240. $tpl = str_replace("<div/>","<div></div>",$tpl);
  241. $pattern = '/<li><div>(.+?)<\/div><\/li>/';
  242. $replacement = '<li><MdTpl name="toggle" tpl="toggle" props=""><div>$1</div></MdTpl></li>';
  243. $tpl = preg_replace($pattern,$replacement,$tpl);
  244. return $tpl;
  245. }
  246. public static function markdown2wiki(string $markdown,$channelType,$contentType): string{
  247. //$markdown = mb_convert_encoding($markdown,'UTF-8','UTF-8');
  248. $markdown = iconv('UTF-8','UTF-8//IGNORE',$markdown);
  249. Log::info('nissaya');
  250. /**
  251. * nissaya
  252. * aaa=bbb\n
  253. * {{nissaya|aaa|bbb}}
  254. */
  255. if($channelType==='nissaya'){
  256. if($contentType === "json"){
  257. $json = json_decode($markdown);
  258. $nissayaWord = [];
  259. foreach ($json as $word) {
  260. if(count($word->sn) === 1){
  261. //只输出第一层级
  262. $str = "{{nissaya|";
  263. if(isset($word->word->value)){
  264. $str .= $word->word->value;
  265. }
  266. $str .= "|";
  267. if(isset($word->meaning->value)){
  268. $str .= $word->meaning->value;
  269. }
  270. $str .= "}}";
  271. $nissayaWord[] = $str;
  272. }
  273. }
  274. $markdown = implode('',$nissayaWord);
  275. }else if($contentType === "markdown"){
  276. $lines = explode("\n",$markdown);
  277. $newLines = array();
  278. foreach ($lines as $line) {
  279. if(strstr($line,'=') === FALSE){
  280. $newLines[] = $line;
  281. }else{
  282. $nissaya = explode('=',$line);
  283. $meaning = array_slice($nissaya,1);
  284. $meaning = implode('=',$meaning);
  285. $newLines[] = "{{nissaya|{$nissaya[0]}|{$meaning}}}";
  286. }
  287. }
  288. $markdown = implode("\n",$newLines);
  289. }
  290. }
  291. //$markdown = preg_replace("/\n\n/","<div></div>",$markdown);
  292. /**
  293. * 处理 mermaid
  294. */
  295. Log::info('mermaid');
  296. Log::info('mermaid:'.strpos($markdown,"```mermaid"));
  297. if(strpos($markdown,"```mermaid") !== false){
  298. Log::info('has mermaid');
  299. $lines = explode("\n",$markdown);
  300. $newLines = array();
  301. $mermaidBegin = false;
  302. $mermaidString = array();
  303. foreach ($lines as $line) {
  304. if($line === "```mermaid"){
  305. Log::info('mermaidBegin');
  306. $mermaidBegin = true;
  307. $mermaidString = [];
  308. continue;
  309. }
  310. if($mermaidBegin){
  311. if($line === "```"){
  312. Log::info('mermaid end');
  313. $newLines[] = "{{mermaid|".base64_encode(\json_encode($mermaidString))."}}";
  314. $mermaidBegin = false;
  315. }else{
  316. $mermaidString[] = $line;
  317. }
  318. }else{
  319. $newLines[] = $line;
  320. }
  321. }
  322. $markdown = implode("\n",$newLines);
  323. }
  324. /**
  325. * 替换换行符
  326. * react 无法处理 <br> 替换为<div></div>代替换行符作用
  327. */
  328. $markdown = str_replace('<br>','<div></div>',$markdown);
  329. /**
  330. * markdown -> html
  331. */
  332. Log::info('markdown -> html');
  333. $markdown = str_replace(['[[',']]'],['㐛','㐚'],$markdown);
  334. $html = Str::markdown($markdown);
  335. $html = str_replace(['㐛','㐚'],['[[',']]'],$html);
  336. $html = MdRender::fixHtml($html);
  337. #替换术语
  338. $pattern = "/\[\[(.+?)\]\]/";
  339. $replacement = '{{term|$1}}';
  340. $html = preg_replace($pattern,$replacement,$html);
  341. #替换句子模版
  342. $pattern = "/\{\{([0-9].+?)\}\}/";
  343. $replacement = '{{sent|$1}}';
  344. $html = preg_replace($pattern,$replacement,$html);
  345. #替换单行注释
  346. #<code>bla</code>
  347. #{{note|bla}}
  348. $pattern = '/<code>(.+?)<\/code>/';
  349. $replacement = '{{note|$1}}';
  350. $html = preg_replace($pattern,$replacement,$html);
  351. #替换多行注释
  352. #<pre><code>bla</code></pre>
  353. #{{note|bla}}
  354. $pattern = '/<pre><code>([\w\W]+?)<\/code><\/pre>/';
  355. $replacement = '{{note|$1}}';
  356. $html = preg_replace($pattern,$replacement,$html);
  357. return $html;
  358. }
  359. /**
  360. * string[] $channelId
  361. */
  362. public static function render($markdown,$channelId,$queryId=null,$mode='read',$channelType='translation',$contentType="markdown"){
  363. return MdRender::render2($markdown,$channelId,$queryId,$mode,$channelType,$contentType);
  364. }
  365. public static function fixHtml($html) {
  366. $doc = new \DOMDocument();
  367. libxml_use_internal_errors(true);
  368. $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
  369. $doc->loadHTML('<span>'.$html.'</span>',LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  370. $fixed = $doc->saveHTML();
  371. $fixed = mb_convert_encoding($fixed, "UTF-8", 'HTML-ENTITIES');
  372. return $fixed;
  373. }
  374. }