ExportChapter.php 9.9 KB

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