]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterExportController.php
Apply fixes from StyleCI
[bookstack] / app / Http / Controllers / ChapterExportController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Entities\Repos\ChapterRepo;
6 use BookStack\Entities\Tools\ExportFormatter;
7 use BookStack\Exceptions\NotFoundException;
8 use Throwable;
9
10 class ChapterExportController extends Controller
11 {
12     protected $chapterRepo;
13     protected $exportFormatter;
14
15     /**
16      * ChapterExportController constructor.
17      */
18     public function __construct(ChapterRepo $chapterRepo, ExportFormatter $exportFormatter)
19     {
20         $this->chapterRepo = $chapterRepo;
21         $this->exportFormatter = $exportFormatter;
22     }
23
24     /**
25      * Exports a chapter to pdf.
26      *
27      * @throws NotFoundException
28      * @throws Throwable
29      */
30     public function pdf(string $bookSlug, string $chapterSlug)
31     {
32         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
33         $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
34
35         return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
36     }
37
38     /**
39      * Export a chapter to a self-contained HTML file.
40      *
41      * @throws NotFoundException
42      * @throws Throwable
43      */
44     public function html(string $bookSlug, string $chapterSlug)
45     {
46         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
47         $containedHtml = $this->exportFormatter->chapterToContainedHtml($chapter);
48
49         return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
50     }
51
52     /**
53      * Export a chapter to a simple plaintext .txt file.
54      *
55      * @throws NotFoundException
56      */
57     public function plainText(string $bookSlug, string $chapterSlug)
58     {
59         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
60         $chapterText = $this->exportFormatter->chapterToPlainText($chapter);
61
62         return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
63     }
64
65     /**
66      * Export a chapter to a simple markdown file.
67      *
68      * @throws NotFoundException
69      */
70     public function markdown(string $bookSlug, string $chapterSlug)
71     {
72         // TODO: This should probably export to a zip file.
73         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
74         $chapterText = $this->exportFormatter->chapterToMarkdown($chapter);
75
76         return $this->downloadResponse($chapterText, $chapterSlug . '.md');
77     }
78 }