mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-04-20 11:20:05 +00:00
Exports: Fixed issues with book text export format
- Fixed missing page content for direct page children - Fixed lack of book description. - Fixed inconsistent spacing between items. - Fixed lack of spacing between HTML items when HTML on same line. For #4557
This commit is contained in:
parent
d5a3bdb7aa
commit
e9664dc678
2 changed files with 68 additions and 17 deletions
|
@ -254,17 +254,20 @@ class ExportFormatter
|
||||||
* Converts the page contents into simple plain text.
|
* Converts the page contents into simple plain text.
|
||||||
* This method filters any bad looking content to provide a nice final output.
|
* This method filters any bad looking content to provide a nice final output.
|
||||||
*/
|
*/
|
||||||
public function pageToPlainText(Page $page): string
|
public function pageToPlainText(Page $page, bool $pageRendered = false, bool $fromParent = false): string
|
||||||
{
|
{
|
||||||
$html = (new PageContent($page))->render();
|
$html = $pageRendered ? $page->html : (new PageContent($page))->render();
|
||||||
$text = strip_tags($html);
|
// Add proceeding spaces before tags so spaces remain between
|
||||||
|
// text within elements after stripping tags.
|
||||||
|
$html = str_replace('<', " <", $html);
|
||||||
|
$text = trim(strip_tags($html));
|
||||||
// Replace multiple spaces with single spaces
|
// Replace multiple spaces with single spaces
|
||||||
$text = preg_replace('/\ {2,}/', ' ', $text);
|
$text = preg_replace('/ {2,}/', ' ', $text);
|
||||||
// Reduce multiple horrid whitespace characters.
|
// Reduce multiple horrid whitespace characters.
|
||||||
$text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
|
$text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
|
||||||
$text = html_entity_decode($text);
|
$text = html_entity_decode($text);
|
||||||
// Add title
|
// Add title
|
||||||
$text = $page->name . "\n\n" . $text;
|
$text = $page->name . ($fromParent ? "\n" : "\n\n") . $text;
|
||||||
|
|
||||||
return $text;
|
return $text;
|
||||||
}
|
}
|
||||||
|
@ -274,13 +277,15 @@ class ExportFormatter
|
||||||
*/
|
*/
|
||||||
public function chapterToPlainText(Chapter $chapter): string
|
public function chapterToPlainText(Chapter $chapter): string
|
||||||
{
|
{
|
||||||
$text = $chapter->name . "\n\n";
|
$text = $chapter->name . "\n" . $chapter->description;
|
||||||
$text .= $chapter->description . "\n\n";
|
$text = trim($text) . "\n\n";
|
||||||
|
|
||||||
|
$parts = [];
|
||||||
foreach ($chapter->getVisiblePages() as $page) {
|
foreach ($chapter->getVisiblePages() as $page) {
|
||||||
$text .= $this->pageToPlainText($page);
|
$parts[] = $this->pageToPlainText($page, false, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $text;
|
return $text . implode("\n\n", $parts);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -288,17 +293,20 @@ class ExportFormatter
|
||||||
*/
|
*/
|
||||||
public function bookToPlainText(Book $book): string
|
public function bookToPlainText(Book $book): string
|
||||||
{
|
{
|
||||||
$bookTree = (new BookContents($book))->getTree(false, false);
|
$bookTree = (new BookContents($book))->getTree(false, true);
|
||||||
$text = $book->name . "\n\n";
|
$text = $book->name . "\n" . $book->description;
|
||||||
|
$text = rtrim($text) . "\n\n";
|
||||||
|
|
||||||
|
$parts = [];
|
||||||
foreach ($bookTree as $bookChild) {
|
foreach ($bookTree as $bookChild) {
|
||||||
if ($bookChild->isA('chapter')) {
|
if ($bookChild->isA('chapter')) {
|
||||||
$text .= $this->chapterToPlainText($bookChild);
|
$parts[] = $this->chapterToPlainText($bookChild);
|
||||||
} else {
|
} else {
|
||||||
$text .= $this->pageToPlainText($bookChild);
|
$parts[] = $this->pageToPlainText($bookChild, true, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $text;
|
return $text . implode("\n\n", $parts);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -46,17 +46,43 @@ class ExportTest extends TestCase
|
||||||
|
|
||||||
public function test_book_text_export()
|
public function test_book_text_export()
|
||||||
{
|
{
|
||||||
$page = $this->entities->page();
|
$book = $this->entities->bookHasChaptersAndPages();
|
||||||
$book = $page->book;
|
$directPage = $book->directPages()->first();
|
||||||
|
$chapter = $book->chapters()->first();
|
||||||
|
$chapterPage = $chapter->pages()->first();
|
||||||
|
$this->entities->updatePage($directPage, ['html' => '<p>My awesome page</p>']);
|
||||||
|
$this->entities->updatePage($chapterPage, ['html' => '<p>My little nested page</p>']);
|
||||||
$this->asEditor();
|
$this->asEditor();
|
||||||
|
|
||||||
$resp = $this->get($book->getUrl('/export/plaintext'));
|
$resp = $this->get($book->getUrl('/export/plaintext'));
|
||||||
$resp->assertStatus(200);
|
$resp->assertStatus(200);
|
||||||
$resp->assertSee($book->name);
|
$resp->assertSee($book->name);
|
||||||
$resp->assertSee($page->name);
|
$resp->assertSee($chapterPage->name);
|
||||||
|
$resp->assertSee($chapter->name);
|
||||||
|
$resp->assertSee($directPage->name);
|
||||||
|
$resp->assertSee('My awesome page');
|
||||||
|
$resp->assertSee('My little nested page');
|
||||||
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
|
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_book_text_export_format()
|
||||||
|
{
|
||||||
|
$entities = $this->entities->createChainBelongingToUser($this->users->viewer());
|
||||||
|
$this->entities->updatePage($entities['page'], ['html' => '<p>My great page</p><p>Full of <strong>great</strong> stuff</p>', 'name' => 'My wonderful page!']);
|
||||||
|
$entities['chapter']->name = 'Export chapter';
|
||||||
|
$entities['chapter']->description = "A test chapter to be exported\nIt has loads of info within";
|
||||||
|
$entities['book']->name = 'Export Book';
|
||||||
|
$entities['book']->description = "This is a book with stuff to export";
|
||||||
|
$entities['chapter']->save();
|
||||||
|
$entities['book']->save();
|
||||||
|
|
||||||
|
$resp = $this->asEditor()->get($entities['book']->getUrl('/export/plaintext'));
|
||||||
|
|
||||||
|
$expected = "Export Book\nThis is a book with stuff to export\n\nExport chapter\nA test chapter to be exported\nIt has loads of info within\n\n";
|
||||||
|
$expected .= "My wonderful page!\nMy great page Full of great stuff";
|
||||||
|
$resp->assertSee($expected);
|
||||||
|
}
|
||||||
|
|
||||||
public function test_book_pdf_export()
|
public function test_book_pdf_export()
|
||||||
{
|
{
|
||||||
$page = $this->entities->page();
|
$page = $this->entities->page();
|
||||||
|
@ -99,15 +125,32 @@ class ExportTest extends TestCase
|
||||||
{
|
{
|
||||||
$chapter = $this->entities->chapter();
|
$chapter = $this->entities->chapter();
|
||||||
$page = $chapter->pages[0];
|
$page = $chapter->pages[0];
|
||||||
|
$this->entities->updatePage($page, ['html' => '<p>This is content within the page!</p>']);
|
||||||
$this->asEditor();
|
$this->asEditor();
|
||||||
|
|
||||||
$resp = $this->get($chapter->getUrl('/export/plaintext'));
|
$resp = $this->get($chapter->getUrl('/export/plaintext'));
|
||||||
$resp->assertStatus(200);
|
$resp->assertStatus(200);
|
||||||
$resp->assertSee($chapter->name);
|
$resp->assertSee($chapter->name);
|
||||||
$resp->assertSee($page->name);
|
$resp->assertSee($page->name);
|
||||||
|
$resp->assertSee('This is content within the page!');
|
||||||
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
|
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_chapter_text_export_format()
|
||||||
|
{
|
||||||
|
$entities = $this->entities->createChainBelongingToUser($this->users->viewer());
|
||||||
|
$this->entities->updatePage($entities['page'], ['html' => '<p>My great page</p><p>Full of <strong>great</strong> stuff</p>', 'name' => 'My wonderful page!']);
|
||||||
|
$entities['chapter']->name = 'Export chapter';
|
||||||
|
$entities['chapter']->description = "A test chapter to be exported\nIt has loads of info within";
|
||||||
|
$entities['chapter']->save();
|
||||||
|
|
||||||
|
$resp = $this->asEditor()->get($entities['book']->getUrl('/export/plaintext'));
|
||||||
|
|
||||||
|
$expected = "Export chapter\nA test chapter to be exported\nIt has loads of info within\n\n";
|
||||||
|
$expected .= "My wonderful page!\nMy great page Full of great stuff";
|
||||||
|
$resp->assertSee($expected);
|
||||||
|
}
|
||||||
|
|
||||||
public function test_chapter_pdf_export()
|
public function test_chapter_pdf_export()
|
||||||
{
|
{
|
||||||
$chapter = $this->entities->chapter();
|
$chapter = $this->entities->chapter();
|
||||||
|
|
Loading…
Add table
Reference in a new issue