From 8755a928aa2b6272bb90669c4fc469a2743e64fb Mon Sep 17 00:00:00 2001 From: Louis-Arnaud Date: Tue, 17 Feb 2026 15:30:25 +0100 Subject: [PATCH] Make not produce a new page by default (#242) Preface elements are now rendered inline in the parent page instead of being chunked into separate pages. Explicit annotations like chunk:true or phd:chunk="true" can still force chunking. --- phpdotnet/phd/Index.php | 2 +- phpdotnet/phd/Package/PHP/XHTML.php | 12 +++---- tests/index/data/preface_no_chunk.xml | 30 ++++++++++++++++ tests/index/preface_no_chunk.phpt | 50 +++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 tests/index/data/preface_no_chunk.xml create mode 100644 tests/index/preface_no_chunk.phpt diff --git a/phpdotnet/phd/Index.php b/phpdotnet/phd/Index.php index e377ed0..8b3a6d3 100644 --- a/phpdotnet/phd/Index.php +++ b/phpdotnet/phd/Index.php @@ -330,7 +330,7 @@ class Index extends Format } elseif (isset($attrs[Reader::XMLNS_DOCBOOK]['annotations'])) { $this->isChunk[] = !str_contains($attrs[Reader::XMLNS_DOCBOOK]['annotations'], 'chunk:false'); } else { - $this->isChunk[] = true; + $this->isChunk[] = ($name !== 'preface'); } if (end($this->isChunk)) { diff --git a/phpdotnet/phd/Package/PHP/XHTML.php b/phpdotnet/phd/Package/PHP/XHTML.php index 878afb8..fce9f6f 100644 --- a/phpdotnet/phd/Package/PHP/XHTML.php +++ b/phpdotnet/phd/Package/PHP/XHTML.php @@ -935,7 +935,7 @@ abstract class Package_PHP_XHTML extends Package_Generic_XHTML { /*Chunk Functions*/ - private function isChunkedByAttributes(array $attributes): bool { + private function isChunkedByAttributes(array $attributes, string $name = ''): bool { /* Legacy way to mark chunks */ if (isset($attributes[Reader::XMLNS_PHD]['chunk'])) { return $attributes[Reader::XMLNS_PHD]['chunk'] != 'false'; @@ -943,8 +943,8 @@ abstract class Package_PHP_XHTML extends Package_Generic_XHTML { /** Annotations attribute is a standard DocBook attribute and could be used for various things */ return !str_contains($attributes[Reader::XMLNS_DOCBOOK]['annotations'], 'chunk:false'); } else { - /* Chunked by default */ - return true; + /* Chunked by default, except preface */ + return $name !== 'preface'; } } @@ -953,7 +953,7 @@ abstract class Package_PHP_XHTML extends Package_Generic_XHTML { $this->CURRENT_CHUNK = $this->CURRENT_ID = $id = $attrs[Reader::XMLNS_XML]["id"] ?? ''; - if ($this->isChunkedByAttributes($attrs)) { + if ($this->isChunkedByAttributes($attrs, $name)) { $this->cchunk = $this->dchunk; } @@ -1063,7 +1063,7 @@ abstract class Package_PHP_XHTML extends Package_Generic_XHTML { } $this->CURRENT_CHUNK = $this->CURRENT_ID = $id; - if ($this->isChunkedByAttributes($attrs)) { + if ($this->isChunkedByAttributes($attrs, $name)) { $this->cchunk = $this->dchunk; $this->notify(Render::CHUNK, Render::OPEN); } @@ -1078,7 +1078,7 @@ abstract class Package_PHP_XHTML extends Package_Generic_XHTML { } return '
'; } - if ($this->isChunkedByAttributes($attrs)) { + if ($this->isChunkedByAttributes($attrs, $name)) { $this->notify(Render::CHUNK, Render::CLOSE); } return '
'; diff --git a/tests/index/data/preface_no_chunk.xml b/tests/index/data/preface_no_chunk.xml new file mode 100644 index 0000000..0d6bfc9 --- /dev/null +++ b/tests/index/data/preface_no_chunk.xml @@ -0,0 +1,30 @@ + + + + Test Manual + + + Test Book + + + Default Preface + This preface should not be chunked by default. + + + + Chunked Preface + This preface should be chunked because of the annotation. + + + + Not Chunked Preface + This preface should not be chunked because of the annotation. + + + + Test Chapter + A regular chapter for comparison. + + + + diff --git a/tests/index/preface_no_chunk.phpt b/tests/index/preface_no_chunk.phpt new file mode 100644 index 0000000..79f7a62 --- /dev/null +++ b/tests/index/preface_no_chunk.phpt @@ -0,0 +1,50 @@ +--TEST-- +Preface is not chunked by default +--FILE-- +forceIndex = true; +$config->xmlFile = $xmlFile; + +$indexRepository = new IndexRepository(new \SQLite3(":memory:")); +$indexRepository->init(); + +$index = new TestIndex($indexRepository, $config, $outputHandler); +$render = new TestRender(new Reader($outputHandler), $config, null, $index); + +$render->run(); + +$nfo = $index->getNfo(); + +echo "All IDs stored:\n"; +var_dump(isset($nfo["preface-default"])); +var_dump(isset($nfo["preface-chunked"])); +var_dump(isset($nfo["preface-not-chunked"])); +var_dump(isset($nfo["test-chapter"])); + +echo "Chunk status:\n"; +echo "preface-default: "; +var_dump($nfo["preface-default"]["chunk"]); +echo "preface-chunked: "; +var_dump($nfo["preface-chunked"]["chunk"]); +echo "preface-not-chunked: "; +var_dump($nfo["preface-not-chunked"]["chunk"]); +echo "test-chapter: "; +var_dump($nfo["test-chapter"]["chunk"]); +?> +--EXPECT-- +All IDs stored: +bool(true) +bool(true) +bool(true) +bool(true) +Chunk status: +preface-default: bool(false) +preface-chunked: bool(true) +preface-not-chunked: bool(false) +test-chapter: bool(true)