Ver Fonte

添加 html 支持

visuddhinanda há 2 anos atrás
pai
commit
1d14ef1c2e

+ 77 - 19
app/Console/Commands/ExportChapter.php

@@ -17,10 +17,10 @@ class ExportChapter extends Command
 {
     /**
      * The name and signature of the console command.
-     * php artisan export:chapter 213 1849 a19eaf75-c63f-4b84-8125-1bce18311e23
+     * php artisan export:chapter 213 1913 a19eaf75-c63f-4b84-8125-1bce18311e23 --format=html
      * @var string
      */
-    protected $signature = 'export:chapter {book} {para} {channel} {--debug}';
+    protected $signature = 'export:chapter {book} {para} {channel} {--debug} {--format=tex}';
 
     /**
      * The console command description.
@@ -50,6 +50,17 @@ class ExportChapter extends Command
         if(\App\Tools\Tools::isStop()){
             return 0;
         }
+        switch ($this->option('format')) {
+            case 'md':
+                $renderFormat='markdown';
+                break;
+            case 'html':
+                $renderFormat='html';
+                break;
+            default:
+                $renderFormat=$this->option('format');
+                break;
+        }
         $book = $this->argument('book');
         $para = $this->argument('para');
         $channelId = $this->argument('channel');
@@ -82,7 +93,7 @@ class ExportChapter extends Command
                                     ->where('channel_id',$channelId)
                                     ->first();
             if($chapter){
-                $filename = "{$sub->paragraph}.tex";
+                $filename = "{$sub->paragraph}.".$this->option('format');
                 $bookMeta['sections'][] = ['filename'=>$filename];
                 $paliTitle = PaliText::where('book',$book)->where('paragraph',$sub->paragraph)->value('toc');
                 $title = $chapter->title?$chapter->title:$paliTitle;
@@ -107,13 +118,24 @@ class ExportChapter extends Command
                                                         'read',
                                                         $channel['type'],
                                                         $sent->content_type,
-                                                        'tex'
+                                                        $renderFormat
                                                         );
                         $sentContent[] = trim($texText);
                     }
                     $paraContent = implode(' ',$sentContent);
                     if($body->level > 7){
-                        $content[] = '\par '.$paraContent;
+                        switch ($this->option('format')) {
+                            case 'tex':
+                                $content[] = '\par '.$paraContent;
+                                break;
+                            case 'html':
+                                $content[] = '<p>'.$paraContent.'</p>';
+                                break;
+                            case 'md':
+                                $content[] = "\n\n".$paraContent;
+                                break;
+                        }
+
                     }else{
                         $currLevel = $body->level - $sub->level;
                         if($currLevel>0){
@@ -124,13 +146,26 @@ class ExportChapter extends Command
                             }else{
                                 $subSessionTitle = $paraContent;
                             }
-                            $subStr = array_fill(0,$currLevel,'sub');
-                            $content[] = '\\'. implode('',$subStr) . "section{".$subSessionTitle.'}';
+                            switch ($this->option('format')) {
+                                case 'tex':
+                                    $subStr = array_fill(0,$currLevel,'sub');
+                                    $content[] = '\\'. implode('',$subStr) . "section{".$subSessionTitle.'}';
+                                    break;
+                                case 'md':
+                                    $subStr = array_fill(0,$currLevel,'#');
+                                    $content[] = implode('',$subStr) . " ".$subSessionTitle;
+                                    break;
+                                case 'html':
+                                    $level = $currLevel+2;
+                                    $content[] = "<h{$currLevel}".$subSessionTitle."</h{$currLevel}";
+                                    break;
+                            }
+
                         }else{
                             $content[] = '\par '.$paraContent;
                         }
                     }
-                    $content[] = "\n";
+                    $content[] = "\n\n";
                 }
 
                 $sections[] = [
@@ -148,32 +183,55 @@ class ExportChapter extends Command
                                         'escape'=>function ($value){
                                             return $value;
                                         }));
-        $tpl = file_get_contents(resource_path("mustache/tex/main.tex"));
+        $tpl = file_get_contents(resource_path("mustache/".$this->option('format')."/main.".$this->option('format')));
         $texContent = $m->render($tpl,$bookMeta);
-        $tex[] = ['name'=>'main.tex',
+        $tex[] = ['name'=>'main.'.$this->option('format'),
                   'content'=>$texContent
                  ];
         foreach ($sections as $key => $section) {
-            $tpl = file_get_contents(resource_path("mustache/tex/section.tex"));
+            $tpl = file_get_contents(resource_path("mustache/".$this->option('format')."/section.".$this->option('format')));
             $texContent = $m->render($tpl,$section['body']);
             $tex[] = ['name'=>$section['name'],
                   'content'=>$texContent
                  ];
         }
 
+        //脚注
+        $tplFile = resource_path("mustache/".$this->option('format')."/footnote.".$this->option('format'));
+        if(isset($GLOBALS['note']) &&
+            is_array($GLOBALS['note']) &&
+            count($GLOBALS['note'])>0 &&
+            file_exists($tplFile)){
+            $tpl = file_get_contents($tplFile);
+            $texContent = $m->render($tpl,['footnote'=>$GLOBALS['note']]);
+            $tex[] = ['name'=>'footnote.'.$this->option('format'),
+                        'content'=>$texContent
+                        ];
+        }
         if($this->option('debug')){
-            $dir = "export/{$book}-{$para}-{$channelId}/";
+            $dir = "export/".$this->option('format')."/{$book}-{$para}-{$channelId}/";
             foreach ($tex as $key => $section) {
                 Storage::disk('local')->put($dir.$section['name'], $section['content']);
             }
         }
-        $data = Export::ToPdf($tex);
-        if($data['ok']){
-            $filename = "export/{$book}-{$para}-{$channelId}.pdf";
-            $this->info($data['content-type']);
-            Storage::disk('local')->put($filename, $data['data']);
-        }else{
-            $this->error($data['code'].'-'.$data['message']);
+        switch ($this->option('format')) {
+            case 'tex':
+                $data = Export::ToPdf($tex);
+                if($data['ok']){
+                    $filename = "export/{$book}-{$para}-{$channelId}.pdf";
+                    $this->info($data['content-type']);
+                    Storage::disk('local')->put($filename, $data['data']);
+                }else{
+                    $this->error($data['code'].'-'.$data['message']);
+                }
+                break;
+            case 'html':
+                $fHtml = "export/".$this->option('format')."/{$book}-{$para}-{$channelId}.html";
+                Storage::disk('local')->put($fHtml, '');
+                foreach ($tex as $key => $section) {
+                    Storage::disk('local')->append($fHtml, $section['content']);
+                }
+                break;
         }
 
         Log::debug('task export offline chapter-table finished');

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

@@ -14,7 +14,7 @@ class TestMdRender extends Command
      * run php artisan test:md.render term unity
      * @var string
      */
-    protected $signature = 'test:md.render {item?} {--format}';
+    protected $signature = 'test:md.render {item?} {--format=}';
 
     /**
      * The console command description.
@@ -59,11 +59,8 @@ class TestMdRender extends Command
         md;
 
         $data['term'] = <<<md
-        ## heading
+        ## term
         [[bhagavantu]]
-        ```
-        test
-        ```
         md;
         $data['noteMulti'] = <<<md
         ## heading
@@ -130,11 +127,11 @@ class TestMdRender extends Command
         //$sent = MdRender::take_sentence($html);
         //print_r($sent);
 
-        $formats = $this->option('format');
+        $format = $this->option('format');
         if(empty($format)){
-            $formats = ['react','unity','text','tex'];
+            $formats = ['react','unity','text','tex','html'];
         }else{
-            $formats = [$formats];
+            $formats = [$format];
         }
         foreach ($formats as $format) {
             $this->info("format:{$format}");

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

@@ -289,6 +289,18 @@ class MdRender{
                     return '';
                 }
                 break;
+            case 'html':
+                if(isset($tplProps)){
+                    if(is_array($tplProps)){
+                        return '';
+                    }else{
+                        return $tplProps;
+                    }
+                }else{
+                    Log::error('tplProps undefine');
+                    return '';
+                }
+                break;
             case 'text':
                 if(isset($tplProps)){
                     if(is_array($tplProps)){
@@ -300,7 +312,6 @@ class MdRender{
                     Log::error('tplProps undefine');
                     return '';
                 }
-
                 break;
             case 'tex':
                 if(isset($tplProps)){
@@ -558,6 +569,10 @@ class MdRender{
                 $html = str_replace(['[%b%]','[%/b%]','[%i%]','[%/i%]'],['<b>','</b>','<i>','</i>'],$html);
                 $output = htmlspecialchars_decode($html,ENT_QUOTES);
                 break;
+            case 'html':
+                $html = str_replace(['<p>','</p>'],['',''],$html);
+                $output = htmlspecialchars_decode($html,ENT_QUOTES);
+                break;
         }
         return $output;
     }

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

@@ -210,6 +210,20 @@ class TemplateRender{
                     'tpl'=>'term',
                     ];
                 break;
+            case 'html':
+                if(isset($props["meaning"])){
+                    $key = 'term-'.$props["word"];
+                    $termHead = "<a href='#'>".$props['meaning']."</a>";
+                    if(isset($GLOBALS[$key])){
+                        $output = $termHead;
+                    }else{
+                        $GLOBALS[$key] = 1;
+                        $output = $termHead.'(<em>'.$props["word"].'</em>)';
+                    }
+                }else{
+                    $output = $props["word"];
+                }
+                break;
             case 'text':
                 if(isset($props["meaning"])){
                     $key = 'term-'.$props["word"];
@@ -275,6 +289,33 @@ class TemplateRender{
                     'tpl'=>'note',
                     ];
                 break;
+            case 'html':
+                if(isset($GLOBALS['note_sn'])){
+                    $GLOBALS['note_sn']++;
+                }else{
+                    $GLOBALS['note_sn'] = 1;
+                    $GLOBALS['note'] = array();
+                }
+                $GLOBALS['note'][] = [
+                        'sn' => 1,
+                        'trigger' => $trigger,
+                        'content' => MdRender::render($props["note"],
+                                        $this->channel_id,
+                                        null,
+                                        'read',
+                                        'translation',
+                                        'markdown',
+                                        'html'
+                                    ),
+                        ];
+
+                $link="<a href='#footnote-".$GLOBALS['note_sn']."' name='note-".$GLOBALS['note_sn']."'>";
+                if(empty($trigger)){
+                    $output =  $link. "<sup>[" . $GLOBALS['note_sn'] . "]</sup></a>";
+                }else{
+                    $output = $link . $trigger . "</a>";
+                }
+                break;
             case 'text':
                 $output = $trigger;
                 break;