Skip to content

Commit 8755a92

Browse files
authored
Make <preface> 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.
1 parent d7f7004 commit 8755a92

File tree

4 files changed

+87
-7
lines changed

4 files changed

+87
-7
lines changed

phpdotnet/phd/Index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public function format_chunk($open, $name, $attrs, $props) {
330330
} elseif (isset($attrs[Reader::XMLNS_DOCBOOK]['annotations'])) {
331331
$this->isChunk[] = !str_contains($attrs[Reader::XMLNS_DOCBOOK]['annotations'], 'chunk:false');
332332
} else {
333-
$this->isChunk[] = true;
333+
$this->isChunk[] = ($name !== 'preface');
334334
}
335335

336336
if (end($this->isChunk)) {

phpdotnet/phd/Package/PHP/XHTML.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -935,16 +935,16 @@ public function format_enumidentifier_text($value, $tag) {
935935

936936

937937
/*Chunk Functions*/
938-
private function isChunkedByAttributes(array $attributes): bool {
938+
private function isChunkedByAttributes(array $attributes, string $name = ''): bool {
939939
/* Legacy way to mark chunks */
940940
if (isset($attributes[Reader::XMLNS_PHD]['chunk'])) {
941941
return $attributes[Reader::XMLNS_PHD]['chunk'] != 'false';
942942
} elseif (isset($attributes[Reader::XMLNS_DOCBOOK]['annotations'])) {
943943
/** Annotations attribute is a standard DocBook attribute and could be used for various things */
944944
return !str_contains($attributes[Reader::XMLNS_DOCBOOK]['annotations'], 'chunk:false');
945945
} else {
946-
/* Chunked by default */
947-
return true;
946+
/* Chunked by default, except preface */
947+
return $name !== 'preface';
948948
}
949949
}
950950

@@ -953,7 +953,7 @@ public function format_container_chunk($open, $name, $attrs, $props) {
953953

954954
$this->CURRENT_CHUNK = $this->CURRENT_ID = $id = $attrs[Reader::XMLNS_XML]["id"] ?? '';
955955

956-
if ($this->isChunkedByAttributes($attrs)) {
956+
if ($this->isChunkedByAttributes($attrs, $name)) {
957957
$this->cchunk = $this->dchunk;
958958
}
959959

@@ -1063,7 +1063,7 @@ public function format_chunk($open, $name, $attrs, $props) {
10631063
}
10641064

10651065
$this->CURRENT_CHUNK = $this->CURRENT_ID = $id;
1066-
if ($this->isChunkedByAttributes($attrs)) {
1066+
if ($this->isChunkedByAttributes($attrs, $name)) {
10671067
$this->cchunk = $this->dchunk;
10681068
$this->notify(Render::CHUNK, Render::OPEN);
10691069
}
@@ -1078,7 +1078,7 @@ public function format_chunk($open, $name, $attrs, $props) {
10781078
}
10791079
return '<div id="'.$id.'" class="'.$name.'">';
10801080
}
1081-
if ($this->isChunkedByAttributes($attrs)) {
1081+
if ($this->isChunkedByAttributes($attrs, $name)) {
10821082
$this->notify(Render::CHUNK, Render::CLOSE);
10831083
}
10841084
return '</div>';
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<set xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0" xml:id="index" xml:lang="en">
4+
<title>Test Manual</title>
5+
6+
<book xml:id="test-book">
7+
<title>Test Book</title>
8+
9+
<preface xml:id="preface-default">
10+
<info><title>Default Preface</title></info>
11+
<para>This preface should not be chunked by default.</para>
12+
</preface>
13+
14+
<preface annotations="chunk:true" xml:id="preface-chunked">
15+
<info><title>Chunked Preface</title></info>
16+
<para>This preface should be chunked because of the annotation.</para>
17+
</preface>
18+
19+
<preface annotations="chunk:false" xml:id="preface-not-chunked">
20+
<info><title>Not Chunked Preface</title></info>
21+
<para>This preface should not be chunked because of the annotation.</para>
22+
</preface>
23+
24+
<chapter xml:id="test-chapter">
25+
<info><title>Test Chapter</title></info>
26+
<para>A regular chapter for comparison.</para>
27+
</chapter>
28+
</book>
29+
30+
</set>

tests/index/preface_no_chunk.phpt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--TEST--
2+
Preface is not chunked by default
3+
--FILE--
4+
<?php
5+
namespace phpdotnet\phd;
6+
7+
require_once __DIR__ . "/../setup.php";
8+
9+
$xmlFile = __DIR__ . "/data/preface_no_chunk.xml";
10+
11+
$config->forceIndex = true;
12+
$config->xmlFile = $xmlFile;
13+
14+
$indexRepository = new IndexRepository(new \SQLite3(":memory:"));
15+
$indexRepository->init();
16+
17+
$index = new TestIndex($indexRepository, $config, $outputHandler);
18+
$render = new TestRender(new Reader($outputHandler), $config, null, $index);
19+
20+
$render->run();
21+
22+
$nfo = $index->getNfo();
23+
24+
echo "All IDs stored:\n";
25+
var_dump(isset($nfo["preface-default"]));
26+
var_dump(isset($nfo["preface-chunked"]));
27+
var_dump(isset($nfo["preface-not-chunked"]));
28+
var_dump(isset($nfo["test-chapter"]));
29+
30+
echo "Chunk status:\n";
31+
echo "preface-default: ";
32+
var_dump($nfo["preface-default"]["chunk"]);
33+
echo "preface-chunked: ";
34+
var_dump($nfo["preface-chunked"]["chunk"]);
35+
echo "preface-not-chunked: ";
36+
var_dump($nfo["preface-not-chunked"]["chunk"]);
37+
echo "test-chapter: ";
38+
var_dump($nfo["test-chapter"]["chunk"]);
39+
?>
40+
--EXPECT--
41+
All IDs stored:
42+
bool(true)
43+
bool(true)
44+
bool(true)
45+
bool(true)
46+
Chunk status:
47+
preface-default: bool(false)
48+
preface-chunked: bool(true)
49+
preface-not-chunked: bool(false)
50+
test-chapter: bool(true)

0 commit comments

Comments
 (0)