ExportChapter.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. use App\Tools\RedisClusters;
  14. class ExportChapter extends Command
  15. {
  16. /**
  17. * The name and signature of the console command.
  18. * php artisan export:chapter 213 1913 a19eaf75-c63f-4b84-8125-1bce18311e23 213-1913 --format=html
  19. * @var string
  20. */
  21. protected $signature = 'export:chapter {book} {para} {channel} {filename} {--debug} {--format=tex} ';
  22. /**
  23. * The console command description.
  24. *
  25. * @var string
  26. */
  27. protected $description = 'Command description';
  28. /**
  29. * Create a new command instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct()
  34. {
  35. parent::__construct();
  36. }
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return int
  41. */
  42. public function handle()
  43. {
  44. $this->info('task export chapter start');
  45. Log::debug('task export chapter start');
  46. if(\App\Tools\Tools::isStop()){
  47. return 0;
  48. }
  49. switch ($this->option('format')) {
  50. case 'md':
  51. $renderFormat='markdown';
  52. break;
  53. case 'html':
  54. $renderFormat='html';
  55. break;
  56. default:
  57. $renderFormat=$this->option('format');
  58. break;
  59. }
  60. $book = $this->argument('book');
  61. $para = $this->argument('para');
  62. $channelId = $this->argument('channel');
  63. $channel = ChannelApi::getById($channelId);
  64. $chapter = PaliText::where('book',$book)->where('paragraph',$para)->first();
  65. if(!$chapter){
  66. return $this->error("no data");
  67. }
  68. $bookMeta = array();
  69. if(empty($chapter->toc)){
  70. $bookMeta['title'] = "unknown";
  71. }else{
  72. $title = ProgressChapter::where('book',$book)->where('para',$para)
  73. ->where('channel_id',$channelId)
  74. ->value('title');
  75. $bookMeta['book_title'] = $title?$title:$chapter->toc;
  76. $bookMeta['sub_title'] = $chapter->toc;
  77. }
  78. if($channel){
  79. $bookMeta['book_author'] = $channel['name'];
  80. }
  81. $subChapter = PaliText::where('book',$book)->where('parent',$para)
  82. ->where('level','<',8)
  83. ->orderBy('paragraph')
  84. ->get();
  85. $sections = array();
  86. foreach ($subChapter as $key => $sub) {
  87. # code...
  88. $chapter = ProgressChapter::where('book',$book)->where('para',$sub->paragraph)
  89. ->where('channel_id',$channelId)
  90. ->first();
  91. if($chapter){
  92. $filename = "{$sub->paragraph}.".$this->option('format');
  93. $bookMeta['sections'][] = ['filename'=>$filename];
  94. $paliTitle = PaliText::where('book',$book)->where('paragraph',$sub->paragraph)->value('toc');
  95. $title = $chapter->title?$chapter->title:$paliTitle;
  96. $content = array();
  97. $chapterStart = $sub->paragraph+1;
  98. $chapterEnd = $sub->paragraph + $sub->chapter_len;
  99. $chapterBody = PaliText::where('book',$book)
  100. ->whereBetween('paragraph',[$chapterStart,$chapterEnd])
  101. ->orderBy('paragraph')->get();
  102. foreach ($chapterBody as $body) {
  103. # code...
  104. $translationData = Sentence::where('book_id',$book)
  105. ->where('paragraph',$body->paragraph)
  106. ->where('channel_uid',$channelId)
  107. ->orderBy('word_start')->get();
  108. $sentContent = array();
  109. foreach ($translationData as $sent) {
  110. $texText = MdRender::render($sent->content,
  111. [$sent->channel_uid],
  112. null,
  113. 'read',
  114. $channel['type'],
  115. $sent->content_type,
  116. $renderFormat
  117. );
  118. $sentContent[] = trim($texText);
  119. }
  120. $paraContent = implode(' ',$sentContent);
  121. if($body->level > 7){
  122. switch ($this->option('format')) {
  123. case 'tex':
  124. $content[] = '\par '.$paraContent;
  125. break;
  126. case 'html':
  127. $content[] = '<p>'.$paraContent.'</p>';
  128. break;
  129. case 'md':
  130. $content[] = "\n\n".$paraContent;
  131. break;
  132. }
  133. }else{
  134. $currLevel = $body->level - $sub->level;
  135. if($currLevel>0){
  136. if(empty($paraContent)){
  137. $subSessionTitle = PaliText::where('book',$book)
  138. ->where('paragraph',$body->paragraph)
  139. ->value('toc');
  140. }else{
  141. $subSessionTitle = $paraContent;
  142. }
  143. switch ($this->option('format')) {
  144. case 'tex':
  145. $subStr = array_fill(0,$currLevel,'sub');
  146. $content[] = '\\'. implode('',$subStr) . "section{".$subSessionTitle.'}';
  147. break;
  148. case 'md':
  149. $subStr = array_fill(0,$currLevel,'#');
  150. $content[] = implode('',$subStr) . " ".$subSessionTitle;
  151. break;
  152. case 'html':
  153. $level = $currLevel+2;
  154. $content[] = "<h{$currLevel}".$subSessionTitle."</h{$currLevel}";
  155. break;
  156. }
  157. }else{
  158. $content[] = '\par '.$paraContent;
  159. }
  160. }
  161. $content[] = "\n\n";
  162. }
  163. $sections[] = [
  164. 'name'=>$filename,
  165. 'body'=>[
  166. 'title'=>$title,
  167. 'content'=>implode('',$content)
  168. ]
  169. ];
  170. }
  171. }
  172. $tex = array();
  173. $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES,
  174. 'delimiters' => '[[ ]]',
  175. 'escape'=>function ($value){
  176. return $value;
  177. }));
  178. $tpl = file_get_contents(resource_path("mustache/".$this->option('format')."/main.".$this->option('format')));
  179. $texContent = $m->render($tpl,$bookMeta);
  180. $tex[] = ['name'=>'main.'.$this->option('format'),
  181. 'content'=>$texContent
  182. ];
  183. foreach ($sections as $key => $section) {
  184. $tpl = file_get_contents(resource_path("mustache/".$this->option('format')."/section.".$this->option('format')));
  185. $texContent = $m->render($tpl,$section['body']);
  186. $tex[] = ['name'=>$section['name'],
  187. 'content'=>$texContent
  188. ];
  189. }
  190. //脚注
  191. $tplFile = resource_path("mustache/".$this->option('format')."/footnote.".$this->option('format'));
  192. if(isset($GLOBALS['note']) &&
  193. is_array($GLOBALS['note']) &&
  194. count($GLOBALS['note'])>0 &&
  195. file_exists($tplFile)){
  196. $tpl = file_get_contents($tplFile);
  197. $texContent = $m->render($tpl,['footnote'=>$GLOBALS['note']]);
  198. $tex[] = ['name'=>'footnote.'.$this->option('format'),
  199. 'content'=>$texContent
  200. ];
  201. }
  202. if($this->option('debug')){
  203. $dir = "export/".$this->option('format')."/{$book}-{$para}-{$channelId}/";
  204. foreach ($tex as $key => $section) {
  205. Storage::disk('local')->put($dir.$section['name'], $section['content']);
  206. }
  207. }
  208. //save
  209. switch ($this->option('format')) {
  210. case 'tex':
  211. $data = Export::ToPdf($tex);
  212. if($data['ok']){
  213. $filename = "export/".$this->argument('filename');
  214. $this->info($data['content-type']);
  215. Storage::put($filename, $data['data']);
  216. }else{
  217. $this->error($data['code'].'-'.$data['message']);
  218. }
  219. break;
  220. case 'html':
  221. $filename = "export/".$this->argument('filename');
  222. $file = array();
  223. foreach ($tex as $key => $section) {
  224. $file[] = $section['content'];
  225. }
  226. Storage::put($filename, implode('',$file));
  227. break;
  228. }
  229. RedisClusters::put('export/done/'.$this->argument('filename'),true,3600*24);
  230. Log::debug('task export offline chapter-table finished');
  231. return 0;
  232. }
  233. }