MdRender.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. class MdRender{
  12. public static function wiki2xml(string $wiki):string{
  13. /**
  14. * 替换{{}} 到xml之前 要先把换行符号去掉
  15. */
  16. $html = str_replace("\n","",$wiki);
  17. $pattern = "/\{\{(.+?)\|/";
  18. $replacement = '<MdTpl name="$1"><param>';
  19. $html = preg_replace($pattern,$replacement,$html);
  20. $html = str_replace("}}","</param></MdTpl>",$html);
  21. $html = str_replace("|","</param><param>",$html);
  22. /**
  23. * 替换变量名
  24. */
  25. $pattern = "/<param>([a-z]+?)=/";
  26. $replacement = '<param name="$1">';
  27. $html = preg_replace($pattern,$replacement,$html);
  28. $html = str_replace("<p>","<div>",$html);
  29. $html = str_replace("</p>","</div>",$html);
  30. $html = "<span>".$html."</span>";
  31. return $html;
  32. }
  33. public static function xmlQueryId(string $xml, string $id):string{
  34. try{
  35. $dom = simplexml_load_string($xml);
  36. }catch(\Exception $e){
  37. Log::error($e);
  38. return "<div></div>";
  39. }
  40. $tpl_list = $dom->xpath('//MdTpl');
  41. foreach ($tpl_list as $key => $tpl) {
  42. foreach ($tpl->children() as $param) {
  43. # 处理每个参数
  44. if($param->getName() === "param"){
  45. foreach($param->attributes() as $pa => $pa_value){
  46. $pValue = $pa_value->__toString();
  47. if($pa === "name" && $pValue === "id"){
  48. if($param->__toString() === $id){
  49. return $tpl->asXML();
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. return "<div></div>";
  57. }
  58. public static function take_sentence(string $xml):array{
  59. $output = [];
  60. try{
  61. $dom = simplexml_load_string($xml);
  62. }catch(\Exception $e){
  63. Log::error($e);
  64. return $output;
  65. }
  66. $tpl_list = $dom->xpath('//MdTpl');
  67. foreach ($tpl_list as $key => $tpl) {
  68. foreach($tpl->attributes() as $a => $a_value){
  69. if($a==="name"){
  70. if($a_value->__toString() ==="sent"){
  71. foreach ($tpl->children() as $param) {
  72. # 处理每个参数
  73. if($param->getName() === "param"){
  74. $sent = $param->__toString();
  75. if(!empty($sent)){
  76. $output[] = $sent;
  77. break;
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. return $output;
  86. }
  87. public static function xml2tpl(string $xml, $channelId=[],$mode='read'):string{
  88. /**
  89. * 解析xml
  90. * 获取模版参数
  91. * 生成react 组件参数
  92. */
  93. try{
  94. $dom = simplexml_load_string($xml);
  95. }catch(\Exception $e){
  96. Log::error($e);
  97. Log::error($xml);
  98. return "<span>xml解析错误{$e}</span>";
  99. }
  100. $tpl_list = $dom->xpath('//MdTpl');
  101. foreach ($tpl_list as $key => $tpl) {
  102. /**
  103. * 遍历 MdTpl 处理参数
  104. */
  105. $props = [];
  106. $tpl_name = '';
  107. foreach($tpl->attributes() as $a => $a_value){
  108. if($a==="name"){
  109. $tpl_name = $a_value;
  110. }
  111. }
  112. $param_id = 0;
  113. foreach ($tpl->children() as $param) {
  114. # 处理每个参数
  115. if($param->getName() === "param"){
  116. $param_id++;
  117. $paramName = "";
  118. foreach($param->attributes() as $pa => $pa_value){
  119. if($pa === "name"){
  120. $props["{$pa_value}"] = $param->__toString();
  121. $paramName = $pa_value;
  122. }
  123. }
  124. if(empty($paramName)){
  125. $props["{$param_id}"] = $param->__toString();
  126. }
  127. }
  128. }
  129. /**
  130. * 生成模版参数
  131. */
  132. $channelInfo = Channel::whereIn('uid',$channelId)->get();
  133. $tplRender = new TemplateRender($props,$channelInfo,$mode);
  134. $tplProps = $tplRender->render($tpl_name);
  135. if($tplProps){
  136. $tpl->addAttribute("props",$tplProps['props']);
  137. $tpl->addAttribute("tpl",$tplProps['tpl']);
  138. $tpl->addChild($tplProps['tag'],$tplProps['html']);
  139. }
  140. }
  141. $html = str_replace('<?xml version="1.0"?>','',$dom->asXML()) ;
  142. $html = str_replace(['<xml>','</xml>'],['<span>','</span>'],$html);
  143. return $html;
  144. }
  145. public static function render2($markdown,$channelId=[],$queryId=null,$mode='read',$channelType,$contentType="markdown"){
  146. $wiki = MdRender::markdown2wiki($markdown,$channelType,$contentType);
  147. $html = MdRender::wiki2xml($wiki);
  148. if(!is_null($queryId)){
  149. $html = MdRender::xmlQueryId($html, $queryId);
  150. }
  151. $tpl = MdRender::xml2tpl($html,$channelId,$mode);
  152. //生成可展开组件
  153. $tpl = str_replace("<div/>","<div></div>",$tpl);
  154. $pattern = '/<li><div>(.+?)<\/div><\/li>/';
  155. $replacement = '<li><MdTpl name="toggle" tpl="toggle" props=""><div>$1</div></MdTpl></li>';
  156. $tpl = preg_replace($pattern,$replacement,$tpl);
  157. return $tpl;
  158. }
  159. public static function markdown2wiki(string $markdown,$channelType,$contentType): string{
  160. //$markdown = mb_convert_encoding($markdown,'UTF-8','UTF-8');
  161. $markdown = iconv('UTF-8','UTF-8//IGNORE',$markdown);
  162. /**
  163. * nissaya
  164. * aaa=bbb\n
  165. * {{nissaya|aaa|bbb}}
  166. */
  167. if($channelType==='nissaya'){
  168. if($contentType === "json"){
  169. $json = json_decode($markdown);
  170. $nissayaWord = [];
  171. foreach ($json as $word) {
  172. if(count($word->sn) === 1){
  173. //只输出第一层级
  174. $str = "{{nissaya|";
  175. if(isset($word->word->value)){
  176. $str .= $word->word->value;
  177. }
  178. $str .= "|";
  179. if(isset($word->meaning->value)){
  180. $str .= $word->meaning->value;
  181. }
  182. $str .= "}}";
  183. $nissayaWord[] = $str;
  184. }
  185. }
  186. $markdown = implode('',$nissayaWord);
  187. }else if($contentType === "markdown"){
  188. /*
  189. $pattern = '/(.+?)=(.+?)\n/';
  190. $replacement = '{{nissaya|$1|$2}}';
  191. $markdown = preg_replace($pattern,$replacement,$markdown);
  192. $pattern = '/(.+?)=(.?)\n/';
  193. $replacement = '{{nissaya|$1|$2}}';
  194. $markdown = preg_replace($pattern,$replacement,$markdown);
  195. $pattern = '/(.?)=(.+?)\n/';
  196. $replacement = '{{nissaya|$1|$2}}';
  197. $markdown = preg_replace($pattern,$replacement,$markdown);
  198. */
  199. $lines = explode("\n",$markdown);
  200. $newLines = array();
  201. foreach ($lines as $line) {
  202. if(strstr($line,'=') === FALSE){
  203. $newLines[] = $line;
  204. }else{
  205. $nissaya = explode('=',$line);
  206. $meaning = array_slice($nissaya,1);
  207. $meaning = implode('=',$meaning);
  208. $newLines[] = "{{nissaya|{$nissaya[0]}|{$meaning}}}";
  209. }
  210. }
  211. $markdown = implode("\n",$newLines);
  212. }
  213. }
  214. //$markdown = preg_replace("/\n\n/","<div></div>",$markdown);
  215. /**
  216. * 替换换行符
  217. * react 无法处理 <br> 替换为<div></div>代替换行符作用
  218. */
  219. $markdown = str_replace('<br>','<div></div>',$markdown);
  220. /**
  221. * markdown -> html
  222. */
  223. $markdown = str_replace(['[[',']]'],['㐛','㐚'],$markdown);
  224. $html = Str::markdown($markdown);
  225. $html = str_replace(['㐛','㐚'],['[[',']]'],$html);
  226. #替换术语
  227. $pattern = "/\[\[(.+?)\]\]/";
  228. $replacement = '{{term|$1}}';
  229. $html = preg_replace($pattern,$replacement,$html);
  230. #替换句子模版
  231. $pattern = "/\{\{([0-9].+?)\}\}/";
  232. $replacement = '{{sent|$1}}';
  233. $html = preg_replace($pattern,$replacement,$html);
  234. #替换单行注释
  235. #<code>bla</code>
  236. #{{note|bla}}
  237. $pattern = '/<code>(.+?)<\/code>/';
  238. $replacement = '{{note|$1}}';
  239. $html = preg_replace($pattern,$replacement,$html);
  240. #替换多行注释
  241. #<pre><code>bla</code></pre>
  242. #{{note|bla}}
  243. $pattern = '/<pre><code>([\w\W]+?)<\/code><\/pre>/';
  244. $replacement = '{{note|$1}}';
  245. $html = preg_replace($pattern,$replacement,$html);
  246. return $html;
  247. }
  248. /**
  249. * string[] $channelId
  250. */
  251. public static function render($markdown,$channelId,$queryId=null,$mode='read',$channelType='translation',$contentType="markdown"){
  252. return MdRender::render2($markdown,$channelId,$queryId,$mode,$channelType,$contentType);
  253. }
  254. }