visuddhinanda 2 年之前
父節點
當前提交
731f22613f

+ 150 - 0
app/Console/Commands/ExportChapter.php

@@ -0,0 +1,150 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Storage;
+use App\Models\ProgressChapter;
+use App\Models\Channel;
+use App\Models\PaliText;
+use App\Models\Sentence;
+use App\Http\Api\ChannelApi;
+use App\Http\Api\MdRender;
+use App\Tools\Export;
+
+class ExportChapter extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'export:chapter {book} {para} {channel}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $book = $this->argument('book');
+        $para = $this->argument('para');
+        $channelId = $this->argument('channel');
+        $channel = ChannelApi::getById($channelId);
+        $chapter = PaliText::where('book',$book)->where('paragraph',$para)->first();
+        if(!$chapter){
+            return $this->error("no data");
+        }
+        $bookMeta = array();
+        if(empty($chapter->toc)){
+            $bookMeta['title'] = "unknown";
+        }else{
+            $title = ProgressChapter::where('book',$book)->where('para',$para)
+                                    ->where('channel_id',$channelId)
+                                    ->value('title');
+            $bookMeta['book_title'] = $title?$title:$chapter->toc;
+            $bookMeta['sub_title'] = $chapter->toc;
+        }
+        if($channel){
+            $bookMeta['book_author'] = $channel['name'];
+        }
+        $subChapter = PaliText::where('book',$book)->where('parent',$para)
+                              ->where('level','<',8)
+                              ->orderBy('paragraph')
+                              ->get();
+        $sections = array();
+        foreach ($subChapter as $key => $sub) {
+            # code...
+            $chapter = ProgressChapter::where('book',$book)->where('para',$sub->paragraph)
+                                    ->where('channel_id',$channelId)
+                                    ->first();
+            if($chapter){
+                $filename = "{$sub->paragraph}.tex";
+                $bookMeta['sections'][] = ['filename'=>$filename];
+                $paliTitle = PaliText::where('book',$book)->where('paragraph',$sub->paragraph)->value('toc');
+                $title = $chapter->title?$chapter->title:$paliTitle;
+                $content = array();
+
+                $chapterStart = $sub->paragraph+1;
+                $chapterEnd = $sub->paragraph + $sub->lenght;
+                $chapterBody = PaliText::where('book',$book)
+                                          ->whereBetween('paragraph',[$chapterStart,$chapterEnd])
+                                          ->orderBy('paragraph')->get();
+                foreach ($chapterBody as $body) {
+                    # code...
+                    $translationData = Sentence::where('book_id',$book)
+                                    ->where('paragraph',$body->paragraph)
+                                    ->where('channel_uid',$channelId)
+                                    ->orderBy('word_start')->get();
+                    $sentContent = array();
+                    foreach ($translationData as $sent) {
+                        /*
+                        $sentContent[] = MdRender::render($sent->content,
+                                                        [$sent->channel_uid],
+                                                        null,
+                                                        'read',
+                                                        $channel['type'],
+                                                        $sent->content_type,
+                                                        'unity'
+                                                        );*/
+                        $sentContent[] = $sent->content;
+                    }
+                    $content[] = '\par ';
+                    $content[] = implode(' ',$sentContent);
+                    $content[] = "\n";
+                }
+
+                $sections[] = [
+                                'name'=>$filename,
+                                'body'=>[
+                                    'title'=>$title,
+                                    'content'=>implode('',$content)
+                                ]
+                            ];
+            }
+        }
+        $tex = array();
+        $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES,'delimiters' => '[[ ]]'));
+        $tpl = file_get_contents(resource_path("mustache/tex/main.tex"));
+        $texContent = $m->render($tpl,$bookMeta);
+        $tex[] = ['name'=>'main.tex',
+                  'content'=>$texContent
+                 ];
+        foreach ($sections as $key => $section) {
+            $tpl = file_get_contents(resource_path("mustache/tex/section.tex"));
+            $texContent = $m->render($tpl,$section['body']);
+            $tex[] = ['name'=>$section['name'],
+                  'content'=>$texContent
+                 ];
+        }
+
+        $data = Export::ToPdf($tex);
+        if($data['ok']){
+            $filename = "export/test.pdf";
+            $this->info($data['content-type']);
+            Storage::disk('local')->put($filename, $data['data']);
+        }else{
+            $this->error($data['code'].'-'.$data['message']);
+        }
+
+        return 0;
+    }
+}

+ 99 - 0
app/Console/Commands/TestTex.php

@@ -0,0 +1,99 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Storage;
+use App\Tools\Export;
+
+class TestTex extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'test:tex';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $tex = array();
+        $content = <<<'EOF'
+% 导言区
+\documentclass[a4paper, 12pt, fontset=ubuntu]{article} % book, report, letter
+\usepackage{ctex} % Use chinese package
+
+\title{\heiti 一级标题}
+\author{\kaishu 半闲}
+\date{\today}
+
+% 正文区
+
+\begin{document}
+    \maketitle % 头部信息在正文显示
+    \newpage
+    \tableofcontents % 显示索引列
+
+    \include{section-1.tex}
+    \include{section-2.tex}
+
+\end{document}
+
+EOF;
+$tex[] = ['name'=>'main.tex','content'=>$content];
+$content = <<<'EOF'
+\section{三十位经}
+
+住在王舍城的竹林园。
+那时,三十位波婆城的比丘全是住林野者、全是常乞食者、全是穿粪扫衣者、全是但三衣者、全是尚有结缚者,他们去见世尊。
+\subsubsection{子章节1.1 标题}
+子章节1-1 正文
+\subsection{子章节1.2 标题}
+子章节1-2 正文
+EOF;
+$tex[] = ['name'=>'section-1.tex','content'=>$content];
+
+$content = <<<'EOF'
+\section{章节2 标题}
+章节2 正文
+\subsection{子章节2.1 标题}
+子章节2-1 正文
+\subsection{子章节2.2 标题}
+子章节2-2 正文
+EOF;
+
+$tex[] = ['name'=>'section-2.tex','content'=>$content];
+
+        $data = Export::ToPdf($tex);
+        if($data['ok']){
+            $filename = "export/test.pdf";
+            $this->info($data['content-type']);
+            Storage::disk('local')->put($filename, $data['data']);
+        }else{
+            $this->error($data['code'].'-'.$data['message']);
+        }
+        return 0;
+    }
+}

+ 38 - 0
app/Tools/Export.php

@@ -0,0 +1,38 @@
+<?php
+namespace App\Tools;
+use Illuminate\Support\Str;
+use Illuminate\Support\Facades\Log;
+
+class Export
+{
+    public static function ToPdf($tex){
+        return Export::tex2pdf_lily($tex);
+    }
+
+    private static function tex2pdf_lily($tex)
+    {
+        $request = new \Palm\Lily\V1\TexToRequest();
+        foreach ($tex as $key => $value) {
+            $request->getFiles()[$value['name']] = $value['content'];
+            //Log::info($value['name']);
+            //Log::info($value['content']);
+        }
+        $host = env('LILY_RPC_SERVER');
+        $client = new \Palm\Lily\V1\TexClient($host, [
+            'credentials' => \Grpc\ChannelCredentials::createInsecure(),
+        ]);
+
+        list($response, $status) = $client->ToPdf($request)->wait();
+        if ($status->code !== \Grpc\STATUS_OK) {
+            echo "ERROR: " . $status->code . ", " . $status->details . PHP_EOL;
+            return ['ok'=>false,
+                    'code'=>$status->code,
+                    'message'=>$status->details
+                    ];
+        }
+        return ['ok'=>true,
+                'content-type'=>$response->getContentType(),
+                'data'=>$response->getPayload()
+                ];
+    }
+}

二進制
grpc/GPBMetadata/Lily.php


+ 20 - 0
resources/mustache/tex/main.tex

@@ -0,0 +1,20 @@
+% 导言区
+\documentclass[a4paper, 12pt, fontset=ubuntu]{article} % book, report, letter
+\usepackage{ctex} % Use chinese package
+
+\title{\heiti [[book_title]]}
+\author{\kaishu [[book_author]]}
+\date{\today}
+
+% 正文区
+
+\begin{document}
+    \maketitle % 头部信息在正文显示
+    \newpage
+    \tableofcontents % 显示索引列
+
+    [[#sections]]
+    \include{ [[filename]]}
+    [[/sections]]
+
+\end{document}

+ 6 - 0
resources/mustache/tex/section.tex

@@ -0,0 +1,6 @@
+\section{[[title]]}
+[[content]]
+[[#subsections]]
+\subsection{[[title]]}
+[[content]]
+[[/subsections]]