2019-10-05 19:55:01 +08:00
|
|
|
<?php namespace BookStack\Http\Controllers;
|
2019-09-16 05:33:27 +08:00
|
|
|
|
2020-11-22 08:17:45 +08:00
|
|
|
use BookStack\Entities\Tools\ExportFormatter;
|
2019-10-05 19:55:01 +08:00
|
|
|
use BookStack\Entities\Repos\ChapterRepo;
|
2019-09-16 05:33:27 +08:00
|
|
|
use BookStack\Exceptions\NotFoundException;
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
class ChapterExportController extends Controller
|
|
|
|
{
|
|
|
|
|
2019-10-05 19:55:01 +08:00
|
|
|
protected $chapterRepo;
|
2020-11-22 09:26:14 +08:00
|
|
|
protected $exportFormatter;
|
2019-09-16 05:33:27 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ChapterExportController constructor.
|
|
|
|
*/
|
2020-11-22 09:26:14 +08:00
|
|
|
public function __construct(ChapterRepo $chapterRepo, ExportFormatter $exportFormatter)
|
2019-09-16 05:33:27 +08:00
|
|
|
{
|
2019-10-05 19:55:01 +08:00
|
|
|
$this->chapterRepo = $chapterRepo;
|
2020-11-22 09:26:14 +08:00
|
|
|
$this->exportFormatter = $exportFormatter;
|
2019-09-16 05:33:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-05 19:55:01 +08:00
|
|
|
* Exports a chapter to pdf.
|
2019-09-16 05:33:27 +08:00
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
public function pdf(string $bookSlug, string $chapterSlug)
|
|
|
|
{
|
2019-10-05 19:55:01 +08:00
|
|
|
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
|
2020-11-22 09:26:14 +08:00
|
|
|
$pdfContent = $this->exportFormatter->chapterToPdf($chapter);
|
2019-09-16 05:33:27 +08:00
|
|
|
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 19:55:01 +08:00
|
|
|
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
|
2020-11-22 09:26:14 +08:00
|
|
|
$containedHtml = $this->exportFormatter->chapterToContainedHtml($chapter);
|
2019-09-16 05:33:27 +08:00
|
|
|
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 19:55:01 +08:00
|
|
|
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
|
2020-11-22 09:26:14 +08:00
|
|
|
$chapterText = $this->exportFormatter->chapterToPlainText($chapter);
|
2019-09-16 05:33:27 +08:00
|
|
|
return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
|
|
|
|
}
|
2020-05-13 12:12:26 +08: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-16 05:33:27 +08:00
|
|
|
}
|