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