ExportChapter.php 7.1 KB

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