2021-06-26 15:23:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Http\Controllers;
|
2019-09-15 22:33:27 +01:00
|
|
|
|
2019-10-05 12:55:01 +01:00
|
|
|
use BookStack\Entities\Repos\ChapterRepo;
|
2021-06-26 15:23:15 +00:00
|
|
|
use BookStack\Entities\Tools\ExportFormatter;
|
2019-09-15 22:33:27 +01:00
|
|
|
use BookStack\Exceptions\NotFoundException;
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
class ChapterExportController extends Controller
|
|
|
|
{
|
2019-10-05 12:55:01 +01:00
|
|
|
protected $chapterRepo;
|
2020-11-22 01:26:14 +00:00
|
|
|
protected $exportFormatter;
|
2019-09-15 22:33:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ChapterExportController constructor.
|
|
|
|
*/
|
2020-11-22 01:26:14 +00:00
|
|
|
public function __construct(ChapterRepo $chapterRepo, ExportFormatter $exportFormatter)
|
2019-09-15 22:33:27 +01:00
|
|
|
{
|
2019-10-05 12:55:01 +01:00
|
|
|
$this->chapterRepo = $chapterRepo;
|
2020-11-22 01:26:14 +00:00
|
|
|
$this->exportFormatter = $exportFormatter;
|
2021-08-28 21:48:17 +01:00
|
|
|
$this->middleware('can:content-export');
|
2019-09-15 22:33:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-05 12:55:01 +01:00
|
|
|
* Exports a chapter to pdf.
|
2021-06-26 15:23:15 +00:00
|
|
|
*
|
2019-09-15 22:33:27 +01:00
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
public function pdf(string $bookSlug, string $chapterSlug)
|
|
|
|
{
|
2019-10-05 12:55:01 +01:00
|
|
|
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
|
2020-11-22 01:26:14 +00:00
|
|
|
$pdfContent = $this->exportFormatter->chapterToPdf($chapter);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2022-06-08 23:50:42 +01:00
|
|
|
return $this->download()->directly($pdfContent, $chapterSlug . '.pdf');
|
2019-09-15 22:33:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a chapter to a self-contained HTML file.
|
2021-06-26 15:23:15 +00:00
|
|
|
*
|
2019-09-15 22:33:27 +01:00
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
public function html(string $bookSlug, string $chapterSlug)
|
|
|
|
{
|
2019-10-05 12:55:01 +01:00
|
|
|
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
|
2020-11-22 01:26:14 +00:00
|
|
|
$containedHtml = $this->exportFormatter->chapterToContainedHtml($chapter);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2022-06-08 23:50:42 +01:00
|
|
|
return $this->download()->directly($containedHtml, $chapterSlug . '.html');
|
2019-09-15 22:33:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a chapter to a simple plaintext .txt file.
|
2021-06-26 15:23:15 +00:00
|
|
|
*
|
2019-09-15 22:33:27 +01:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function plainText(string $bookSlug, string $chapterSlug)
|
|
|
|
{
|
2019-10-05 12:55:01 +01:00
|
|
|
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
|
2020-11-22 01:26:14 +00:00
|
|
|
$chapterText = $this->exportFormatter->chapterToPlainText($chapter);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2022-06-08 23:50:42 +01:00
|
|
|
return $this->download()->directly($chapterText, $chapterSlug . '.txt');
|
2019-09-15 22:33:27 +01:00
|
|
|
}
|
2020-05-12 21:12:26 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a chapter to a simple markdown file.
|
2021-06-26 15:23:15 +00:00
|
|
|
*
|
2020-05-12 21:12:26 -07:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function markdown(string $bookSlug, string $chapterSlug)
|
|
|
|
{
|
|
|
|
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
|
2021-06-22 21:02:18 +01:00
|
|
|
$chapterText = $this->exportFormatter->chapterToMarkdown($chapter);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2022-06-08 23:50:42 +01:00
|
|
|
return $this->download()->directly($chapterText, $chapterSlug . '.md');
|
2020-05-12 21:12:26 -07:00
|
|
|
}
|
2019-09-15 22:33:27 +01:00
|
|
|
}
|