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