浏览代码

add $channelType

visuddhinanda 2 年之前
父节点
当前提交
2b464183b6

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

@@ -147,8 +147,8 @@ class MdRender{
         return $html;
     }
 
-    public static function render2($markdown,$channelId='',$queryId=null,$mode='read',$channelType){
-        $wiki = MdRender::markdown2wiki($markdown,$channelType);
+    public static function render2($markdown,$channelId='',$queryId=null,$mode='read',$channelType,$contentType="markdown"){
+        $wiki = MdRender::markdown2wiki($markdown,$channelType,$contentType);
         $html = MdRender::wiki2xml($wiki);
         if(!is_null($queryId)){
             $html = MdRender::xmlQueryId($html, $queryId);
@@ -156,22 +156,44 @@ class MdRender{
         $tpl = MdRender::xml2tpl($html,$channelId,$mode);
         return $tpl;
     }
-    public static function markdown2wiki(string $markdown,$channelType): string{
+    public static function markdown2wiki(string $markdown,$channelType,$contentType): string{
                 /**
          * nissaya
          * aaa=bbb\n
          * {{nissaya|aaa|bbb}}
          */
         if($channelType==='nissaya'){
-            $pattern = '/(.+?)=(.+?)\n/';
-            $replacement = '{{nissaya|$1|$2}}';
-            $markdown = preg_replace($pattern,$replacement,$markdown);
-            $pattern = '/(.+?)=(.?)\n/';
-            $replacement = '{{nissaya|$1|$2}}';
-            $markdown = preg_replace($pattern,$replacement,$markdown);
-            $pattern = '/(.?)=(.+?)\n/';
-            $replacement = '{{nissaya|$1|$2}}';
-            $markdown = preg_replace($pattern,$replacement,$markdown);
+            if($contentType === "json"){
+                $json = json_decode($markdown);
+                $nissayaWord = [];
+                foreach ($json as $word) {
+                    if(count($word->sn) === 1){
+                        //只输出第一层级
+                        $str = "{{nissaya|";
+                        if(isset($word->word->value)){
+                            $str .= $word->word->value;
+                        }
+                        $str .= "|";
+                        if(isset($word->meaning->value)){
+                            $str .= $word->meaning->value;
+                        }
+                        $str .= "}}";
+                        $nissayaWord[] = $str;
+                    }
+
+                }
+                $markdown = implode('',$nissayaWord);
+            }else{
+                $pattern = '/(.+?)=(.+?)\n/';
+                $replacement = '{{nissaya|$1|$2}}';
+                $markdown = preg_replace($pattern,$replacement,$markdown);
+                $pattern = '/(.+?)=(.?)\n/';
+                $replacement = '{{nissaya|$1|$2}}';
+                $markdown = preg_replace($pattern,$replacement,$markdown);
+                $pattern = '/(.?)=(.+?)\n/';
+                $replacement = '{{nissaya|$1|$2}}';
+                $markdown = preg_replace($pattern,$replacement,$markdown);
+            }
         }
         $markdown = preg_replace("/\n\n/","<div></div>",$markdown);
 
@@ -217,8 +239,8 @@ class MdRender{
     /**
      *
      */
-    public static function render($markdown,$channelId,$queryId=null,$mode='read',$channelType='translation'){
-        return MdRender::render2($markdown,$channelId,$queryId,$mode,$channelType);
+    public static function render($markdown,$channelId,$queryId=null,$mode='read',$channelType='translation',$contentType="markdown"){
+        return MdRender::render2($markdown,$channelId,$queryId,$mode,$channelType,$contentType);
     }
 
 }

+ 3 - 1
app/Http/Controllers/CorpusController.php

@@ -50,6 +50,7 @@ class CorpusController extends Controller
         "word_end",
         'channel_uid',
         'content',
+        'content_type',
         'editor_uid',
         'acceptor_uid',
         'pr_edit_at',
@@ -501,6 +502,7 @@ class CorpusController extends Controller
                 if($row){
                     $newSent['id'] = $row->uid;
                     $newSent['content'] = $row->content;
+                    $newSent['contentType'] = $row->content_type;
                     $newSent['html'] = "";
                     $newSent["editor"]=UserApi::getById($row->editor_uid);
                     $newSent['updateAt'] = $row->updated_at;
@@ -544,7 +546,7 @@ class CorpusController extends Controller
                         case 'nissaya':
                             $newSent['html'] = Cache::remember("/sent/{$channelId}/{$currSentId}",10,
                             function() use($row,$mode){
-                                return MdRender::render($row->content,$row->channel_uid,null,$mode,"nissaya");
+                                return MdRender::render($row->content,$row->channel_uid,null,$mode,"nissaya",$row->content_type);
                             });
                             break;
                         default:

+ 6 - 3
app/Http/Controllers/SentenceController.php

@@ -21,7 +21,7 @@ class SentenceController extends Controller
     public function index(Request $request)
     {
         $result=false;
-		$indexCol = ['id','book_id','paragraph','word_start','word_end','content','channel_uid','editor_uid','acceptor_uid','pr_edit_at','updated_at'];
+		$indexCol = ['id','book_id','paragraph','word_start','word_end','content','content_type','channel_uid','editor_uid','acceptor_uid','pr_edit_at','updated_at'];
 
 		switch ($request->get('view')) {
             case 'public':
@@ -266,7 +266,7 @@ class SentenceController extends Controller
         $user = AuthApi::current($request);
         if(!$user){
             //未登录鉴权失败
-            return $this->error(__('auth.failed'));
+            return $this->error(__('auth.failed'),[],403);
         }
         $channel = Channel::where('uid',$param[4])->first();
         if(!$channel){
@@ -274,7 +274,7 @@ class SentenceController extends Controller
         }
         if($channel->owner_uid !== $user["user_uid"]){
             //TODO 判断是否为协作
-            return $this->error(__('auth.failed'));
+            return $this->error(__('auth.failed'),[],403);
         }
 
         $sent = Sentence::firstOrNew([
@@ -289,6 +289,9 @@ class SentenceController extends Controller
             "create_time"=>time()*1000,
         ]);
         $sent->content = $request->get('content');
+        if($request->has('content_type')){
+            $sent->content_type = $request->get('content_type');
+        }
         $sent->language = $channel->lang;
         $sent->status = $channel->status;
         $sent->editor_uid = $user["user_uid"];

+ 7 - 1
app/Http/Resources/SentResource.php

@@ -28,6 +28,7 @@ class SentResource extends JsonResource
         $data = [
                 "id" => $this->uid,
                 "content"=>$this->content,
+                "content_type"=>$this->content_type,
                 "html"=> "",
                 "book"=> $this->book_id,
                 "paragraph"=> $this->paragraph,
@@ -39,7 +40,12 @@ class SentResource extends JsonResource
                 "updated_at"=> $this->updated_at,
             ];
         if($request->get('html',true)){
-            $data['html'] = MdRender::render($this->content,$this->channel_uid,null,$mode,$channel['type']);
+            $data['html'] = MdRender::render($this->content,
+                                             $this->channel_uid,
+                                             null,
+                                             $mode,
+                                             $channel['type'],
+                                             $this->content_type);
         }
         if($request->get('mode')==="edit" || $request->get('mode')==="wbw"){
             $data['suggestionCount'] = SuggestionApi::getCountBySent($this->book_id,