Includes: Started foundations for new include tag parser

This commit is contained in:
Dan Brown 2023-11-22 22:14:28 +00:00
parent 22a9cf1e48
commit 04d21c8a97
No known key found for this signature in database
GPG key ID: 46D9F943C24A2EF9
2 changed files with 103 additions and 0 deletions

View file

@ -0,0 +1,57 @@
<?php
namespace BookStack\Entities\Tools;
use BookStack\Util\HtmlDocument;
use Closure;
class PageIncludeParser
{
protected static string $includeTagRegex = "/{{@\s?([0-9].*?)}}/";
public function __construct(
protected string $pageHtml,
protected Closure $pageContentForId,
) {
}
public function parse(): string
{
$html = new HtmlDocument($this->pageHtml);
$includeHosts = $html->queryXPath("//body//*[contains(text(), '{{@')]");
$node = $includeHosts->item(0);
// One of the direct child textnodes of the "$includeHosts" should be
// the one with the include tag within.
$textNode = $node->childNodes->item(0);
// TODO:
// Hunt down the specific text nodes with matches
// Split out tag text node from rest of content
// Fetch tag content->
// If range or top-block: delete tag text node, [Promote to top-block], delete old top-block if empty
// If inline: Replace current text node with new text or elem
// !! "Range" or "inline" status should come from tag parser and content fetcher, not guessed direct from content
// since we could have a range of inline elements
// [Promote to top-block]
// Tricky operation.
// Can throw in before or after current top-block depending on relative position
// Could [Split] top-block but complex past a single level depth.
// Maybe [Split] if one level depth, otherwise default to before/after block
// Should work for the vast majority of cases, and not for those which would
// technically be invalid in-editor anyway.
// [Split]
// Copy original top-block node type and attrs (apart from ID)
// Move nodes after promoted tag-node into copy
// Insert copy after original (after promoted top-block eventually)
// Notes: May want to eventually parse through backwards, which should avoid issues
// in changes affecting the next tag, where tags may be in the same/adjacent nodes.
return $html->getBodyInnerHtml();
}
}

View file

@ -0,0 +1,46 @@
<?php
namespace Tests\Unit;
use BookStack\Entities\Tools\PageIncludeParser;
use Tests\TestCase;
class PageIncludeParserTest extends TestCase
{
public function test_include_simple_inline_text()
{
$this->runParserTest(
'<p>{{@45#content}}</p>',
['45' => '<p id="content">Testing</p>'],
'<p>Testing</p>',
);
}
public function test_include_simple_inline_text_with_existing_siblings()
{
$this->runParserTest(
'<p>{{@45#content}} <strong>Hi</strong>there!</p>',
['45' => '<p id="content">Testing</p>'],
'<p>Testing <strong>Hi</strong>there!</p>',
);
}
public function test_include_simple_inline_text_within_other_text()
{
$this->runParserTest(
'<p>Hello {{@45#content}}there!</p>',
['45' => '<p id="content">Testing</p>'],
'<p>Hello Testingthere!</p>',
);
}
protected function runParserTest(string $html, array $contentById, string $expected)
{
$parser = new PageIncludeParser($html, function (int $id) use ($contentById) {
return $contentById[strval($id)] ?? null;
});
$result = $parser->parse();
$this->assertEquals($expected, $result);
}
}