2019-10-05 07:55:01 -04:00
|
|
|
<?php namespace BookStack\Http\Controllers;
|
2019-09-15 17:33:27 -04:00
|
|
|
|
|
|
|
use BookStack\Entities\ExportService;
|
2019-10-05 07:55:01 -04:00
|
|
|
use BookStack\Entities\Repos\ChapterRepo;
|
2019-09-15 17:33:27 -04:00
|
|
|
use BookStack\Exceptions\NotFoundException;
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
class ChapterExportController extends Controller
|
|
|
|
{
|
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
protected $chapterRepo;
|
2019-09-15 17:33:27 -04:00
|
|
|
protected $exportService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ChapterExportController constructor.
|
|
|
|
*/
|
2019-10-05 07:55:01 -04:00
|
|
|
public function __construct(ChapterRepo $chapterRepo, ExportService $exportService)
|
2019-09-15 17:33:27 -04:00
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$this->chapterRepo = $chapterRepo;
|
2019-09-15 17:33:27 -04:00
|
|
|
$this->exportService = $exportService;
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-05 07:55:01 -04:00
|
|
|
* Exports a chapter to pdf.
|
2019-09-15 17:33:27 -04:00
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
public function pdf(string $bookSlug, string $chapterSlug)
|
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
|
2019-09-15 17:33:27 -04:00
|
|
|
$pdfContent = $this->exportService->chapterToPdf($chapter);
|
|
|
|
return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a chapter to a self-contained HTML file.
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
public function html(string $bookSlug, string $chapterSlug)
|
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
|
2019-09-15 17:33:27 -04:00
|
|
|
$containedHtml = $this->exportService->chapterToContainedHtml($chapter);
|
|
|
|
return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a chapter to a simple plaintext .txt file.
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function plainText(string $bookSlug, string $chapterSlug)
|
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
|
2019-09-15 17:33:27 -04:00
|
|
|
$chapterText = $this->exportService->chapterToPlainText($chapter);
|
|
|
|
return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
|
|
|
|
}
|
2020-05-13 00:12:26 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a chapter to a simple markdown file.
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function markdown(string $bookSlug, string $chapterSlug)
|
|
|
|
{
|
|
|
|
// TODO: This should probably export to a zip file.
|
|
|
|
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
|
|
|
|
$chapterText = $this->exportService->chapterToMarkdown($chapter);
|
|
|
|
return $this->downloadResponse($chapterText, $chapterSlug . '.md');
|
|
|
|
}
|
2019-09-15 17:33:27 -04:00
|
|
|
}
|