ExportChapter.php 7.0 KB

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