ExportChapter.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. $book = $this->argument('book');
  45. $para = $this->argument('para');
  46. $channelId = $this->argument('channel');
  47. $channel = ChannelApi::getById($channelId);
  48. $chapter = PaliText::where('book',$book)->where('paragraph',$para)->first();
  49. if(!$chapter){
  50. return $this->error("no data");
  51. }
  52. $bookMeta = array();
  53. if(empty($chapter->toc)){
  54. $bookMeta['title'] = "unknown";
  55. }else{
  56. $title = ProgressChapter::where('book',$book)->where('para',$para)
  57. ->where('channel_id',$channelId)
  58. ->value('title');
  59. $bookMeta['book_title'] = $title?$title:$chapter->toc;
  60. $bookMeta['sub_title'] = $chapter->toc;
  61. }
  62. if($channel){
  63. $bookMeta['book_author'] = $channel['name'];
  64. }
  65. $subChapter = PaliText::where('book',$book)->where('parent',$para)
  66. ->where('level','<',8)
  67. ->orderBy('paragraph')
  68. ->get();
  69. $sections = array();
  70. foreach ($subChapter as $key => $sub) {
  71. # code...
  72. $chapter = ProgressChapter::where('book',$book)->where('para',$sub->paragraph)
  73. ->where('channel_id',$channelId)
  74. ->first();
  75. if($chapter){
  76. $filename = "{$sub->paragraph}.tex";
  77. $bookMeta['sections'][] = ['filename'=>$filename];
  78. $paliTitle = PaliText::where('book',$book)->where('paragraph',$sub->paragraph)->value('toc');
  79. $title = $chapter->title?$chapter->title:$paliTitle;
  80. $content = array();
  81. $chapterStart = $sub->paragraph+1;
  82. $chapterEnd = $sub->paragraph + $sub->chapter_len;
  83. $chapterBody = PaliText::where('book',$book)
  84. ->whereBetween('paragraph',[$chapterStart,$chapterEnd])
  85. ->orderBy('paragraph')->get();
  86. foreach ($chapterBody as $body) {
  87. # code...
  88. $translationData = Sentence::where('book_id',$book)
  89. ->where('paragraph',$body->paragraph)
  90. ->where('channel_uid',$channelId)
  91. ->orderBy('word_start')->get();
  92. $sentContent = array();
  93. foreach ($translationData as $sent) {
  94. $texText = MdRender::render($sent->content,
  95. [$sent->channel_uid],
  96. null,
  97. 'read',
  98. $channel['type'],
  99. $sent->content_type,
  100. 'tex'
  101. );
  102. $sentContent[] = trim($texText);
  103. }
  104. $paraContent = implode(' ',$sentContent);
  105. if($body->level > 7){
  106. $content[] = '\par '.$paraContent;
  107. }else{
  108. $currLevel = $body->level - $sub->level;
  109. if($currLevel>0){
  110. if(empty($paraContent)){
  111. $subSessionTitle = PaliText::where('book',$book)
  112. ->where('paragraph',$body->paragraph)
  113. ->value('toc');
  114. }else{
  115. $subSessionTitle = $paraContent;
  116. }
  117. $subStr = array_fill(0,$currLevel,'sub');
  118. $content[] = '\\'. implode('',$subStr) . "section{".$subSessionTitle.'}';
  119. }else{
  120. $content[] = '\par '.$paraContent;
  121. }
  122. }
  123. $content[] = "\n";
  124. }
  125. $sections[] = [
  126. 'name'=>$filename,
  127. 'body'=>[
  128. 'title'=>$title,
  129. 'content'=>implode('',$content)
  130. ]
  131. ];
  132. }
  133. }
  134. $tex = array();
  135. $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES,
  136. 'delimiters' => '[[ ]]',
  137. 'escape'=>function ($value){
  138. return $value;
  139. }));
  140. $tpl = file_get_contents(resource_path("mustache/tex/main.tex"));
  141. $texContent = $m->render($tpl,$bookMeta);
  142. $tex[] = ['name'=>'main.tex',
  143. 'content'=>$texContent
  144. ];
  145. foreach ($sections as $key => $section) {
  146. $tpl = file_get_contents(resource_path("mustache/tex/section.tex"));
  147. $texContent = $m->render($tpl,$section['body']);
  148. $tex[] = ['name'=>$section['name'],
  149. 'content'=>$texContent
  150. ];
  151. }
  152. if($this->option('debug')){
  153. $dir = "export/{$book}-{$para}-{$channelId}/";
  154. foreach ($tex as $key => $section) {
  155. Storage::disk('local')->put($dir.$section['name'], $section['content']);
  156. }
  157. }
  158. $data = Export::ToPdf($tex);
  159. if($data['ok']){
  160. $filename = "export/{$book}-{$para}-{$channelId}.pdf";
  161. $this->info($data['content-type']);
  162. Storage::disk('local')->put($filename, $data['data']);
  163. }else{
  164. $this->error($data['code'].'-'.$data['message']);
  165. }
  166. Log::debug('task export offline chapter-table finished');
  167. return 0;
  168. }
  169. }