visuddhinanda 2 лет назад
Родитель
Сommit
f4bd987dbf
2 измененных файлов с 26 добавлено и 19 удалено
  1. 9 5
      app/Console/Commands/TestMdRender.php
  2. 17 14
      app/Http/Api/MdRender.php

+ 9 - 5
app/Console/Commands/TestMdRender.php

@@ -10,10 +10,10 @@ class TestMdRender extends Command
 {
     /**
      * The name and signature of the console command.
-     *
+     * run php artisan test:md.render term unity
      * @var string
      */
-    protected $signature = 'test:md.render {item?}';
+    protected $signature = 'test:md.render {item?} {--format=html}';
 
     /**
      * The console command description.
@@ -41,8 +41,8 @@ class TestMdRender extends Command
     {
         $data = array();
         $data['bold'] = <<<md
-        三十 **"aa"** **“aa”**  **“十位”** **[[十位]]** 经
-        在[中间]六处为**[ditthippatta]**,在极果为**慧解脱**
+        **三十位** 经
+        在[中间]六处为**[[licchavi]]**,在极果为**慧解脱**
         md;
 
         $data['sentence'] = <<<md
@@ -124,7 +124,11 @@ class TestMdRender extends Command
             if(!empty($_item) && $key !==$_item){
                 continue;
             }
-            echo MdRender::render2($value,['00ae2c48-c204-4082-ae79-79ba2740d506'],null,'read','translation');
+            $format = $this->option('format');
+            echo MdRender::render2($value,
+                                  ['00ae2c48-c204-4082-ae79-79ba2740d506'],
+                                  null,'read','translation',
+                                  $contentType="markdown",$format);
         }
 
         return 0;

+ 17 - 14
app/Http/Api/MdRender.php

@@ -75,12 +75,7 @@ class MdRender{
         }
     }
 
-    public static function wiki2xml(string $wiki,$channelId=[],$mode='read'):string{
-        /**
-         * 替换{{}} 到xml之前 要先把换行符号去掉
-         */
-        //$wiki = str_replace("\n","",$wiki);
-
+    public static function wiki2xml(string $wiki,$channelId=[],$mode='read',$format='html'):string{
         /**
          * 把模版转换为xml
          */
@@ -109,8 +104,7 @@ class MdRender{
                 //tpl to react
                 $tpl = str_replace('<param','<span class="param"',$tpl);
                 $tpl = str_replace('</param>','</span>',$tpl);
-                $tpl = MdRender::xml2tpl($tpl,$channelId,$mode);
-
+                $tpl = MdRender::xml2tpl($tpl,$channelId,$mode,$format);
                 $buffer[] = $tpl;
             }
             $remain = $arrWiki['data'][2];
@@ -174,7 +168,7 @@ class MdRender{
         }
         return $output;
     }
-    public static function xml2tpl(string $xml, $channelId=[],$mode='read'):string{
+    public static function xml2tpl(string $xml, $channelId=[],$mode='read',$format='html'):string{
         /**
          * 解析xml
          * 获取模版参数
@@ -266,15 +260,24 @@ class MdRender{
         }
         $html = $doc->saveHTML();
         $html = str_replace(['<dfn','</dfn>'],['<MdTpl','</MdTpl>'],$html);
-        return trim($html);
+        if($format==='html'){
+            return trim($html);
+        }else{
+            if($tplProps){
+                return "{{"."{$tplProps['tpl']}|{$tplProps['props']}"."}}";
+            }else{
+                return '';
+            }
+        }
+
     }
 
-    public static function render2($markdown,$channelId=[],$queryId=null,$mode='read',$channelType,$contentType="markdown"){
+    public static function render2($markdown,$channelId=[],$queryId=null,$mode='read',$channelType,$contentType="markdown",$format='html'){
         if(empty($markdown)){
             return "<span></span>";
         }
         $wiki = MdRender::markdown2wiki($markdown,$channelType,$contentType);
-        $html = MdRender::wiki2xml($wiki,$channelId,$mode);
+        $html = MdRender::wiki2xml($wiki,$channelId,$mode,$format);
         if(!is_null($queryId)){
             $html = MdRender::xmlQueryId($html, $queryId);
         }
@@ -490,8 +493,8 @@ class MdRender{
     /**
      * string[] $channelId
      */
-    public static function render($markdown,$channelId,$queryId=null,$mode='read',$channelType='translation',$contentType="markdown"){
-        return MdRender::render2($markdown,$channelId,$queryId,$mode,$channelType,$contentType);
+    public static function render($markdown,$channelId,$queryId=null,$mode='read',$channelType='translation',$contentType="markdown",$format='html'){
+        return MdRender::render2($markdown,$channelId,$queryId,$mode,$channelType,$contentType,$format);
     }
 
     public static function  fixHtml($html) {