visuddhinanda 2 лет назад
Родитель
Сommit
60d0373388
2 измененных файлов с 149 добавлено и 65 удалено
  1. 61 48
      app/Http/Api/MdRender.php
  2. 88 17
      app/Http/Api/TemplateRender.php

+ 61 - 48
app/Http/Api/MdRender.php

@@ -244,9 +244,9 @@ class MdRender{
              */
             //TODO 判断$channelId里面的是否都是uuid
             $channelInfo = Channel::whereIn('uid',$channelId)->get();
-            $tplRender = new TemplateRender($props,$channelInfo,$mode);
+            $tplRender = new TemplateRender($props,$channelInfo,$mode,$format);
             $tplProps = $tplRender->render($tpl_name);
-            if($tplProps){
+            if($format==='react' && $tplProps){
                 $props = $doc->createAttribute("props");
                 $props->nodeValue = $tplProps['props'];
                 $tpl->appendChild($props);
@@ -260,48 +260,76 @@ class MdRender{
         }
         $html = $doc->saveHTML();
         $html = str_replace(['<dfn','</dfn>'],['<MdTpl','</MdTpl>'],$html);
-        if($format==='react'){
-            return trim($html);
-        }else if($format==='unity'){
-            if($tplProps){
-                return "{{"."{$tplProps['tpl']}|{$tplProps['props']}"."}}";
-            }else{
-                return '';
-            }
-        }else if($format==='text'){
-            if(isset($tplProps['text'])){
-                return $tplProps['text'];
-            }else{
+        switch ($format) {
+            case 'react':
+                return trim($html);
+                break;
+            case 'unity':
+                if($tplProps){
+                    return "{{"."{$tplProps['tpl']}|{$tplProps['props']}"."}}";
+                }else{
+                    return '';
+                }
+                break;
+            case 'text':
+                return $tplProps;
+                break;
+            case 'tex':
+                return $tplProps;
+                break;
+            default:
                 return '';
-            }
-        }else{
-            return '';
+                break;
         }
     }
 
     public static function render2($markdown,$channelId=[],$queryId=null,$mode='read',$channelType,$contentType="markdown",$format='react'){
         if(empty($markdown)){
-            return "<span></span>";
+            switch ($format) {
+                case 'react':
+                    return "<span></span>";
+                    break;
+                default:
+                    return "";
+                    break;
+            }
         }
         $wiki = MdRender::markdown2wiki($markdown,$channelType,$contentType);
         $html = MdRender::wiki2xml($wiki,$channelId,$mode,$format);
         if(!is_null($queryId)){
             $html = MdRender::xmlQueryId($html, $queryId);
         }
-        $html = MdRender::markdownToHtml($html);
+        $html = MdRender::markdownToHtml($html,$format);
         //$tpl = MdRender::xml2tpl($html,$channelId,$mode);
 
-        if($format==='react'){
-            //生成可展开组件
-            $html = str_replace("<div/>","<div></div>",$html);
-            $pattern = '/<li><div>(.+?)<\/div><\/li>/';
-            $replacement = '<li><MdTpl name="toggle" tpl="toggle" props=""><div>$1</div></MdTpl></li>';
-            $html = preg_replace($pattern,$replacement,$html);
+        //后期处理
+        $output = '';
+        switch ($format) {
+            case 'react':
+                //生成可展开组件
+                $html = str_replace("<div/>","<div></div>",$html);
+                $pattern = '/<li><div>(.+?)<\/div><\/li>/';
+                $replacement = '<li><MdTpl name="toggle" tpl="toggle" props=""><div>$1</div></MdTpl></li>';
+                $output = preg_replace($pattern,$replacement,$html);
+                break;
+            case 'text':
+                $html = strip_tags($html);
+                $output = htmlspecialchars_decode($html,ENT_QUOTES);
+                //$output = html_entity_decode($html);
+                break;
+            case 'tex':
+                $html = strip_tags($html);
+                $output = htmlspecialchars_decode($html,ENT_QUOTES);
+                //$output = html_entity_decode($html);
+                break;
+            case 'unity':
+                $html = str_replace(['<strong>','</strong>','<em>','</em>'],['[%b%]','[%/b%]','[%i%]','[%/i%]'],$html);
+                $html = strip_tags($html);
+                $html = str_replace(['[%b%]','[%/b%]','[%i%]','[%/i%]'],['<b>','</b>','<i>','</i>'],$html);
+                $output = htmlspecialchars_decode($html,ENT_QUOTES);
+                break;
         }
-        if($format==='text'){
-            $html = strip_tags($html);
-        }
-        return $html;
+        return $output;
     }
 
 
@@ -453,32 +481,17 @@ class MdRender{
         $replacement = '{{note|$1}}';
         $markdown = preg_replace($pattern,$replacement,$markdown);
 
-/*
-        #替换多行注释
-        #<pre><code>bla</code></pre>
-        #{{note|bla}}
-        $pattern = '/<pre><code>([\w\W]+?)<\/code><\/pre>/';
-        $replacement = '{{note|$1}}';
-        $html = preg_replace($pattern,$replacement,$html);
-
-        #替换单行注释
-        #<code>bla</code>
-        #{{note|bla}}
-        $pattern = '/<code>(.+?)<\/code>/';
-        $replacement = '{{note|$1}}';
-        $html = preg_replace($pattern,$replacement,$html);
-*/
-
         return $markdown;
     }
 
-    public static function markdownToHtml($markdown){
+    public static function markdownToHtml($markdown,$format='react'){
         $markdown = str_replace('MdTpl','mdtpl',$markdown);
         $markdown = str_replace(['<param','</param>'],['<span','</span>'],$markdown);
 
         $html = Markdown::render($markdown);
-        $html = MdRender::fixHtml($html);
-
+        if($format==='react'){
+            $html = MdRender::fixHtml($html);
+        }
         $html = str_replace('<hr>','<hr />',$html);
         //给H1-6 添加uuid
         for ($i=1; $i<7 ; $i++) {

+ 88 - 17
app/Http/Api/TemplateRender.php

@@ -8,18 +8,22 @@ use App\Http\Controllers\CorpusController;
 use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\Log;
 use App\Http\Api\ChannelApi;
+use App\Http\Api\MdRender;
 
 class TemplateRender{
     protected $param = [];
     protected $mode = "read";
     protected $channel_id = [];
+    protected $format = 'react';
+
 
     /**
      * Create a new command instance.
-     * int $mode  'read' | 'edit'
+     * string $mode  'read' | 'edit'
+     * string $format  'react' | 'text' | 'tex' | 'unity'
      * @return void
      */
-    public function __construct($param, $channelInfo, $mode)
+    public function __construct($param, $channelInfo, $mode,$format='react')
     {
         $this->param = $param;
         foreach ($channelInfo as $value) {
@@ -27,6 +31,7 @@ class TemplateRender{
         }
         $this->channelInfo = $channelInfo;
         $this->mode = $mode;
+        $this->format = $format;
     }
 
     public function render($tpl_name){
@@ -172,31 +177,97 @@ class TemplateRender{
         $channelId = $this->channel_id[0];
         $channelInfo = $this->channelInfo[0];
         $key = "/term/{$channelId}/{$word}";
-        //Log::info("term cache key:{$key}");
         $props = $this->getTermProps($word,$channelId,$channelInfo);
-        return [
-            'props'=>base64_encode(\json_encode($props)),
-            'html'=>$props['innerHtml'],
-            'tag'=>'span',
-            'tpl'=>'term',
-            ];
+
+        $output = $props['word'];
+        switch ($this->format) {
+            case 'react':
+                $output=[
+                    'props'=>base64_encode(\json_encode($props)),
+                    'html'=>$props['innerHtml'],
+                    'tag'=>'span',
+                    'tpl'=>'term',
+                    ];
+                break;
+            case 'unity':
+                $output=[
+                    'props'=>base64_encode(\json_encode($props)),
+                    'tpl'=>'term',
+                    ];
+                break;
+            case 'text':
+                if(isset($props["meaning"])){
+                    $key = 'term-'.$props["word"];
+                    if(isset($GLOBALS[$key])){
+                        $output = $props["meaning"];
+                    }else{
+                        $GLOBALS[$key] = 1;
+                        $output = $props["meaning"].'('.$props["word"].')';
+                    }
+                }else{
+                    $output = $props["word"];
+                }
+                break;
+            case 'tex':
+                if(isset($props["meaning"])){
+                    $key = 'term-'.$props["word"];
+                    if(isset($GLOBALS[$key])){
+                        $output = $props["meaning"];
+                    }else{
+                        $GLOBALS[$key] = 1;
+                        $output = $props["meaning"].'('.$props["word"].')';
+                    }
+                }else{
+                    $output = $props["word"];
+                }
+                break;
+        }
+        return $output;
     }
 
     private  function render_note(){
-
-        $props = ["note" => $this->get_param($this->param,"text",1)];
+        $note = $this->get_param($this->param,"text",1);
         $trigger = $this->get_param($this->param,"trigger",2);
+        $props = ["note" => $note ];
         $innerString = "";
         if(!empty($trigger)){
             $props["trigger"] = $trigger;
             $innerString = $props["trigger"];
         }
-        return [
-            'props'=>base64_encode(\json_encode($props)),
-            'html'=>$innerString,
-            'tag'=>'span',
-            'tpl'=>'note',
-            ];
+        if($this->format==='unity'){
+            $props["note"] = MdRender::render($props["note"],
+                                $this->channel_id,
+                                null,
+                                'read',
+                                'translation',
+                                'markdown',
+                                'unity'
+                                );
+        }
+        $output = $note;
+        switch ($this->format) {
+            case 'react':
+                $output=[
+                    'props'=>base64_encode(\json_encode($props)),
+                    'html'=>$innerString,
+                    'tag'=>'span',
+                    'tpl'=>'note',
+                    ];
+                break;
+            case 'unity':
+                $output=[
+                    'props'=>base64_encode(\json_encode($props)),
+                    'tpl'=>'note',
+                    ];
+                break;
+            case 'text':
+                $output = $trigger;
+                break;
+            case 'tex':
+                $output = $trigger;
+                break;
+        }
+        return $output;
     }
     private  function render_nissaya(){