ExportChapter.php 10.0 KB

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