Просмотр исходного кода

:sparkles: add article tmplate

visuddhinanda 3 лет назад
Родитель
Сommit
e97f12d8a2
2 измененных файлов с 27 добавлено и 136 удалено
  1. 1 136
      app/Http/Api/MdRender.php
  2. 26 0
      app/Http/Api/TemplateRender.php

+ 1 - 136
app/Http/Api/MdRender.php

@@ -32,7 +32,7 @@ class MdRender{
 
         $html = str_replace("<p>","<div>",$html);
         $html = str_replace("</p>","</div>",$html);
-        $html = "<xml>".$html."</xml>";
+        $html = "<span>".$html."</span>";
         return $html;
     }
     public static function xmlQueryId(string $xml, string $id):string{
@@ -174,141 +174,6 @@ class MdRender{
      */
     public static function render($markdown,$channelId,$queryId=null,$mode='read'){
         return MdRender::render2($markdown,$channelId,$queryId,$mode);
-
-        $html = MdRender::markdown2wiki($markdown);
-
-        /**
-         * 转换为Mustache模版
-         */
-        $pattern = "/\{\{(.+?)\}\}/";
-        $replacement = "\n{{#function}}\n$1\n{{/function}}\n";
-        $html = preg_replace($pattern,$replacement,$html);
-
-        /**
-         * Mustache_Engine 处理Mustache模版
-         * 把Mustache模版内容转换为react组件
-         */
-        $m = new \Mustache_Engine(array('entity_flags' => ENT_QUOTES));
-        $html = $m->render($html, array(
-          'function' => function($text) use($m,$channelId) {
-            //1: 解析
-            $param = explode("|",$text);
-            //3: 处理业务逻辑
-            $tplName = trim($param[0]);
-            $innerString = "";
-            switch($tplName){
-                case 'term':
-                    //获取实际的参数
-                    $word = trim($param[1]);
-                    $props = Cache::remember("/term/{$channelId}/{$word}",
-                          60,
-                          function() use($word,$channelId){
-                            $tplParam = DhammaTerm::where("word",$word)->first();
-                            $output = [
-                                "word" => $word,
-                                "channel" => $channelId,
-                                ];
-                                $innerString = $output["word"];
-                            if($tplParam){
-                                $output["id"] = $tplParam->guid;
-                                $output["meaning"] = $tplParam->meaning;
-                                $innerString = $output["meaning"];
-                                if(!empty($tplParam->other_meaning)){
-                                    $output["meaning2"] = $tplParam->other_meaning;
-                                }
-                            }
-                            return $output;
-                          });
-                    break;
-                case 'note':
-                    if(isset($param[1])){
-                        $props = ["note"=>trim($param[1])];
-                    }
-                    if(isset($param[2])){
-                        $props["trigger"] = trim($param[2]);
-                        $innerString = $props["trigger"];
-                    }
-                    break;
-                case 'sent':
-                    $tplName = "sentedit";
-                    $innerString = "";
-                    $sentInfo = explode('@',trim($param[1]));
-                    $sentId = $sentInfo[0];
-                    $Sent = new CorpusController();
-                    if(empty($channelId)){
-                        $channels = [];
-                    }else{
-                        $channels = [$channelId];
-                    }
-                    if(isset($sentInfo[1])){
-                        $channels = [$sentInfo[1]];
-                    }
-                    $html = $Sent->getSentTpl($param[1],$channels);
-                    return $html;
-                    break;
-                case 'quote':
-                    $paraId = trim($param[1]);
-                    $props = Cache::remember("/quote/{$channelId}/{$paraId}",
-                          60,
-                          function() use($paraId,$channelId){
-                            $para = \explode('-',$paraId);
-                            $output = [
-                                "paraId" => $paraId,
-                                "channel" => $channelId,
-                                "innerString" => $paraId,
-                                ];
-                            if(count($para)<2){
-                                return $output;
-                            }
-                            $PaliText = PaliText::where("book",$para[0])
-                                                ->where("paragraph",$para[1])
-                                                ->select(['toc','path'])
-                                                ->first();
-
-                            if($PaliText){
-                                $output["pali"] = $PaliText->toc;
-                                $output["paliPath"] = \json_decode($PaliText->path);
-                                $innerString = $PaliText->toc;
-                            }
-                            return $output;
-                          });
-                    break;
-                case 'exercise':
-                    $exeId = trim($param[1]);
-                    $exeContent = trim($param[2]);
-                    $props = Cache::remember("/quote/{$channelId}/{$exeId}",
-                          60,
-                          function() use($exeId,$channelId){
-                            $output = [
-                                "id" => $exeId,
-                                "channel" => $channelId,
-                                ];
-                            return $output;
-                          });
-                    #替换句子
-                    $pattern = "/\(\((.+?)\)\)/";
-                    $replacement = '{{sent|$1}}';
-                    Log::info("content{$exeContent}");
-
-                    $exeContent = preg_replace($pattern,$replacement,$exeContent);
-                    Log::info("content{$exeContent}");
-                    $innerString = MdRender::render($exeContent,$channelId);
-                    break;
-                default:
-                    break;
-            }
-            //4: 返回拼好的字符串
-
-            $props = base64_encode(\json_encode($props));
-            $html = "<MdTpl tpl='{$tplName}' props='{$props}' >{$innerString}</MdTpl>";
-            return $html;
-          }
-        ));
-        if(substr_count($html,"<p>") === 1){
-            $html = \str_replace(['<p>','</p>'],'',$html);
-        }
-        //LOG::info($html);
-        return "<xml>{$html}</xml>";
     }
 
 }

+ 26 - 0
app/Http/Api/TemplateRender.php

@@ -42,6 +42,9 @@ class TemplateRender{
             case 'exercise':
                 $result = $this->render_exercise();
                 break;
+            case 'article':
+                $result = $this->render_article();
+                break;
             default:
                 # code...
                 $result = [
@@ -120,7 +123,30 @@ class TemplateRender{
             'tpl'=>'exercise',
             ];
     }
+    private  function render_article(){
 
+        $type = $this->get_param($this->param,"type",1);
+        $id = $this->get_param($this->param,"id",2);
+        $text = $this->get_param($this->param,"text",3);
+        $channel = $this->get_param($this->param,"channel",4);
+        $props = [
+                    "type" => $type,
+                    "id" => $id,
+                    "channel" => $channel,
+                ];
+        if(empty($channel)){
+            $props['channel'] = $this->channel_id;
+        }
+        if(!empty($text)){
+            $props['text'] = $text;
+        }
+        return [
+            'props'=>base64_encode(\json_encode($props)),
+            'html'=>"",
+            'tag'=>'span',
+            'tpl'=>'article',
+            ];
+    }
     private  function render_quote(){
         $paraId = $this->get_param($this->param,"para",1);
         $channelId = $this->channel_id;