ExportChapter.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use App\Models\ProgressChapter;
  6. use App\Models\Channel;
  7. use App\Models\PaliText;
  8. use App\Models\Sentence;
  9. use App\Http\Api\ChannelApi;
  10. use App\Http\Api\MdRender;
  11. use App\Tools\Export;
  12. use Illuminate\Support\Facades\Log;
  13. class ExportChapter extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. * php artisan export:chapter 213 1849 a19eaf75-c63f-4b84-8125-1bce18311e23
  18. * @var string
  19. */
  20. protected $signature = 'export:chapter {book} {para} {channel} {--debug}';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = 'Command description';
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return int
  40. */
  41. public function handle()
  42. {
  43. Log::debug('task export offline chapter-table start');
  44. if(\App\Tools\Tools::isStop()){
  45. return 0;
  46. }
  47. $book = $this->argument('book');
  48. $para = $this->argument('para');
  49. $channelId = $this->argument('channel');
  50. $channel = ChannelApi::getById($channelId);
  51. $chapter = PaliText::where('book',$book)->where('paragraph',$para)->first();
  52. if(!$chapter){
  53. return $this->error("no data");
  54. }
  55. $bookMeta = array();
  56. if(empty($chapter->toc)){
  57. $bookMeta['title'] = "unknown";
  58. }else{
  59. $title = ProgressChapter::where('book',$book)->where('para',$para)
  60. ->where('channel_id',$channelId)
  61. ->value('title');
  62. $bookMeta['book_title'] = $title?$title:$chapter->toc;
  63. $bookMeta['sub_title'] = $chapter->toc;
  64. }
  65. if($channel){
  66. $bookMeta['book_author'] = $channel['name'];
  67. }
  68. $subChapter = PaliText::where('book',$book)->where('parent',$para)
  69. ->where('level','<',8)
  70. ->orderBy('paragraph')
  71. ->get();
  72. $sections = array();
  73. foreach ($subChapter as $key => $sub) {
  74. # code...
  75. $chapter = ProgressChapter::where('book',$book)->where('para',$sub->paragraph)
  76. ->where('channel_id',$channelId)
  77. ->first();
  78. if($chapter){
  79. $filename = "{$sub->paragraph}.tex";
  80. $bookMeta['sections'][] = ['filename'=>$filename];
  81. $paliTitle = PaliText::where('book',$book)->where('paragraph',$sub->paragraph)->value('toc');
  82. $title = $chapter->title?$chapter->title:$paliTitle;
  83. $content = array();
  84. $chapterStart = $sub->paragraph+1;
  85. $chapterEnd = $sub->paragraph + $sub->chapter_len;
  86. $chapterBody = PaliText::where('book',$book)
  87. ->whereBetween('paragraph',[$chapterStart,$chapterEnd])
  88. ->orderBy('paragraph')->get();
  89. foreach ($chapterBody as $body) {
  90. # code...
  91. $translationData = Sentence::where('book_id',$book)
  92. ->where('paragraph',$body->paragraph)
  93. ->where('channel_uid',$channelId)
  94. ->orderBy('word_start')->get();
  95. $sentContent = array();
  96. foreach ($translationData as $sent) {
  97. $texText = MdRender::render($sent->content,
  98. [$sent->channel_uid],
  99. null,
  100. 'read',
  101. $channel['type'],
  102. $sent->content_type,
  103. 'tex'
  104. );
  105. $sentContent[] = trim($texText);
  106. }
  107. $paraContent = implode(' ',$sentContent);
  108. if($body->level > 7){
  109. $content[] = '\par '.$paraContent;
  110. }else{
  111. $currLevel = $body->level - $sub->level;
  112. if($currLevel>0){
  113. if(empty($paraContent)){
  114. $subSessionTitle = PaliText::where('book',$book)
  115. ->where('paragraph',$body->paragraph)
  116. ->value('toc');
  117. }else{
  118. $subSessionTitle = $paraContent;
  119. }
  120. $subStr = array_fill(0,$currLevel,'sub');
  121. $content[] = '\\'. implode('',$subStr) . "section{".$subSessionTitle.'}';
  122. }else{
  123. $content[] = '\par '.$paraContent;
  124. }
  125. }
  126. $content[] = "\n";
  127. }
  128. $sections[] = [
  129. 'name'=>$filename,
  130. 'body'=>[
  131. 'title'=>$title,
  132. 'content'=>implode('',$content)
  133. ]
  134. ];
  135. }
  136. }
  137. $tex = array();
  138. $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES,
  139. 'delimiters' => '[[ ]]',
  140. 'escape'=>function ($value){
  141. return $value;
  142. }));
  143. $tpl = file_get_contents(resource_path("mustache/tex/main.tex"));
  144. $texContent = $m->render($tpl,$bookMeta);
  145. $tex[] = ['name'=>'main.tex',
  146. 'content'=>$texContent
  147. ];
  148. foreach ($sections as $key => $section) {
  149. $tpl = file_get_contents(resource_path("mustache/tex/section.tex"));
  150. $texContent = $m->render($tpl,$section['body']);
  151. $tex[] = ['name'=>$section['name'],
  152. 'content'=>$texContent
  153. ];
  154. }
  155. if($this->option('debug')){
  156. $dir = "export/{$book}-{$para}-{$channelId}/";
  157. foreach ($tex as $key => $section) {
  158. Storage::disk('local')->put($dir.$section['name'], $section['content']);
  159. }
  160. }
  161. $data = Export::ToPdf($tex);
  162. if($data['ok']){
  163. $filename = "export/{$book}-{$para}-{$channelId}.pdf";
  164. $this->info($data['content-type']);
  165. Storage::disk('local')->put($filename, $data['data']);
  166. }else{
  167. $this->error($data['code'].'-'.$data['message']);
  168. }
  169. Log::debug('task export offline chapter-table finished');
  170. return 0;
  171. }
  172. }