MdRender.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Http\Api;
  3. use Illuminate\Support\Str;
  4. use mustache\mustache;
  5. use App\Models\DhammaTerm;
  6. use App\Http\Controllers\CorpusController;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\Log;
  9. class MdRender{
  10. /**
  11. *
  12. */
  13. public static function render($markdown,$channelId,$isArticle=false){
  14. $html = Str::markdown($markdown);
  15. #替换术语
  16. $pattern = "/\[\[(.+?)\]\]/";
  17. $replacement = '{{term|$1}}';
  18. $html = preg_replace($pattern,$replacement,$html);
  19. #替换句子
  20. $pattern = "/\{\{([0-9].+?)\}\}/";
  21. $replacement = '{{sent|$1}}';
  22. $html = preg_replace($pattern,$replacement,$html);
  23. #替换注释
  24. #<code>bla</code>
  25. #{{note:bla}}
  26. #替换术语
  27. $pattern = '/<code>(.+?)<\/code>/';
  28. $replacement = '{{note|$1}}';
  29. $html = preg_replace($pattern,$replacement,$html);
  30. $pattern = "/\{\{(.+?)\}\}/";
  31. $replacement = "\n{{#function}}\n$1\n{{/function}}\n";
  32. $html = preg_replace($pattern,$replacement,$html);
  33. $m = new \Mustache_Engine(array('entity_flags' => ENT_QUOTES));
  34. $html = $m->render($html, array(
  35. 'function' => function($text) use($m,$channelId) {
  36. //1: 解析
  37. $param = explode("|",$text);
  38. //3: 处理业务逻辑
  39. $tplName = trim($param[0]);
  40. $innerString = "";
  41. switch($tplName){
  42. case 'term':
  43. //获取实际的参数
  44. $word = trim($param[1]);
  45. $props = Cache::remember("/term/{$channelId}/{$word}",
  46. 60,
  47. function() use($word,$channelId){
  48. $tplParam = DhammaTerm::where("word",$word)->first();
  49. $output = [
  50. "word" => $word,
  51. "channel" => $channelId,
  52. ];
  53. $innerString = $output["word"];
  54. if($tplParam){
  55. $output["id"] = $tplParam->guid;
  56. $output["meaning"] = $tplParam->meaning;
  57. $innerString = $output["meaning"];
  58. if(!empty($tplParam->other_meaning)){
  59. $output["meaning2"] = $tplParam->other_meaning;
  60. }
  61. }
  62. return $output;
  63. });
  64. break;
  65. case 'note':
  66. if(isset($param[1])){
  67. $props = ["note"=>trim($param[1])];
  68. }
  69. if(isset($param[2])){
  70. $props["trigger"] = trim($param[2]);
  71. $innerString = $props["trigger"];
  72. }
  73. break;
  74. case 'sent':
  75. $tplName = "sentedit";
  76. $innerString = "";
  77. $sentId = trim($param[1]);
  78. $Sent = new CorpusController();
  79. $html = $Sent->getSentTpl($param[1],[$channelId]);
  80. return $html;
  81. break;
  82. default:
  83. break;
  84. }
  85. //4: 返回拼好的字符串
  86. $props = base64_encode(\json_encode($props));
  87. $html = "<MdTpl tpl='{$tplName}' props='{$props}' >{$innerString}</MdTpl>";
  88. return $html;
  89. }
  90. ));
  91. if(substr_count($html,"<p>") === 1){
  92. $html = \str_replace(['<p>','</p>'],'',$html);
  93. }
  94. //LOG::info($html);
  95. return $html;
  96. }
  97. }