MdRender.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. $tpl_list = $dom->xpath('//MdTpl');
  173. foreach ($tpl_list as $key => $tpl) {
  174. /**
  175. * 遍历 MdTpl 处理参数
  176. */
  177. $props = [];
  178. $tpl_name = '';
  179. foreach($tpl->attributes() as $a => $a_value){
  180. if($a==="name"){
  181. $tpl_name = $a_value;
  182. }
  183. }
  184. $param_id = 0;
  185. foreach ($tpl->children() as $param) {
  186. # 处理每个参数
  187. if($param->getName() === "param"){
  188. $param_id++;
  189. $paramName = "";
  190. foreach($param->attributes() as $pa => $pa_value){
  191. if($pa === "name"){
  192. $props["{$pa_value}"] = $param->__toString();
  193. $paramName = $pa_value;
  194. }
  195. }
  196. if(empty($paramName)){
  197. $props["{$param_id}"] = $param->__toString();
  198. }
  199. }
  200. }
  201. /**
  202. * 生成模版参数
  203. */
  204. $channelInfo = Channel::whereIn('uid',$channelId)->get();
  205. $tplRender = new TemplateRender($props,$channelInfo,$mode);
  206. $tplProps = $tplRender->render($tpl_name);
  207. if($tplProps){
  208. $tpl->addAttribute("props",$tplProps['props']);
  209. $tpl->addAttribute("tpl",$tplProps['tpl']);
  210. $tpl->addChild($tplProps['tag'],$tplProps['html']);
  211. }
  212. }
  213. $html = str_replace('<?xml version="1.0"?>','',$dom->asXML()) ;
  214. $html = str_replace(['<xml>','</xml>'],['<span>','</span>'],$html);
  215. return $html;
  216. }
  217. public static function render2($markdown,$channelId=[],$queryId=null,$mode='read',$channelType,$contentType="markdown"){
  218. $wiki = MdRender::markdown2wiki($markdown,$channelType,$contentType);
  219. $html = MdRender::wiki2xml($wiki);
  220. if(!is_null($queryId)){
  221. $html = MdRender::xmlQueryId($html, $queryId);
  222. }
  223. $tpl = MdRender::xml2tpl($html,$channelId,$mode);
  224. //生成可展开组件
  225. $tpl = str_replace("<div/>","<div></div>",$tpl);
  226. $pattern = '/<li><div>(.+?)<\/div><\/li>/';
  227. $replacement = '<li><MdTpl name="toggle" tpl="toggle" props=""><div>$1</div></MdTpl></li>';
  228. $tpl = preg_replace($pattern,$replacement,$tpl);
  229. return $tpl;
  230. }
  231. public static function markdown2wiki(string $markdown,$channelType,$contentType): string{
  232. //$markdown = mb_convert_encoding($markdown,'UTF-8','UTF-8');
  233. $markdown = iconv('UTF-8','UTF-8//IGNORE',$markdown);
  234. /**
  235. * nissaya
  236. * aaa=bbb\n
  237. * {{nissaya|aaa|bbb}}
  238. */
  239. if($channelType==='nissaya'){
  240. if($contentType === "json"){
  241. $json = json_decode($markdown);
  242. $nissayaWord = [];
  243. foreach ($json as $word) {
  244. if(count($word->sn) === 1){
  245. //只输出第一层级
  246. $str = "{{nissaya|";
  247. if(isset($word->word->value)){
  248. $str .= $word->word->value;
  249. }
  250. $str .= "|";
  251. if(isset($word->meaning->value)){
  252. $str .= $word->meaning->value;
  253. }
  254. $str .= "}}";
  255. $nissayaWord[] = $str;
  256. }
  257. }
  258. $markdown = implode('',$nissayaWord);
  259. }else if($contentType === "markdown"){
  260. /*
  261. $pattern = '/(.+?)=(.+?)\n/';
  262. $replacement = '{{nissaya|$1|$2}}';
  263. $markdown = preg_replace($pattern,$replacement,$markdown);
  264. $pattern = '/(.+?)=(.?)\n/';
  265. $replacement = '{{nissaya|$1|$2}}';
  266. $markdown = preg_replace($pattern,$replacement,$markdown);
  267. $pattern = '/(.?)=(.+?)\n/';
  268. $replacement = '{{nissaya|$1|$2}}';
  269. $markdown = preg_replace($pattern,$replacement,$markdown);
  270. */
  271. $lines = explode("\n",$markdown);
  272. $newLines = array();
  273. foreach ($lines as $line) {
  274. if(strstr($line,'=') === FALSE){
  275. $newLines[] = $line;
  276. }else{
  277. $nissaya = explode('=',$line);
  278. $meaning = array_slice($nissaya,1);
  279. $meaning = implode('=',$meaning);
  280. $newLines[] = "{{nissaya|{$nissaya[0]}|{$meaning}}}";
  281. }
  282. }
  283. $markdown = implode("\n",$newLines);
  284. }
  285. }
  286. //$markdown = preg_replace("/\n\n/","<div></div>",$markdown);
  287. /**
  288. * 替换换行符
  289. * react 无法处理 <br> 替换为<div></div>代替换行符作用
  290. */
  291. $markdown = str_replace('<br>','<div></div>',$markdown);
  292. /**
  293. * markdown -> html
  294. */
  295. $markdown = str_replace(['[[',']]'],['㐛','㐚'],$markdown);
  296. $html = Str::markdown($markdown);
  297. $html = str_replace(['㐛','㐚'],['[[',']]'],$html);
  298. #替换术语
  299. $pattern = "/\[\[(.+?)\]\]/";
  300. $replacement = '{{term|$1}}';
  301. $html = preg_replace($pattern,$replacement,$html);
  302. #替换句子模版
  303. $pattern = "/\{\{([0-9].+?)\}\}/";
  304. $replacement = '{{sent|$1}}';
  305. $html = preg_replace($pattern,$replacement,$html);
  306. #替换单行注释
  307. #<code>bla</code>
  308. #{{note|bla}}
  309. $pattern = '/<code>(.+?)<\/code>/';
  310. $replacement = '{{note|$1}}';
  311. $html = preg_replace($pattern,$replacement,$html);
  312. #替换多行注释
  313. #<pre><code>bla</code></pre>
  314. #{{note|bla}}
  315. $pattern = '/<pre><code>([\w\W]+?)<\/code><\/pre>/';
  316. $replacement = '{{note|$1}}';
  317. $html = preg_replace($pattern,$replacement,$html);
  318. return $html;
  319. }
  320. /**
  321. * string[] $channelId
  322. */
  323. public static function render($markdown,$channelId,$queryId=null,$mode='read',$channelType='translation',$contentType="markdown"){
  324. return MdRender::render2($markdown,$channelId,$queryId,$mode,$channelType,$contentType);
  325. }
  326. }