Skip to content

Commit b9eb060

Browse files
authored
Make generated example IDs page specific (#210)
* Make example id attribute counter reset on each page (fixes #205)
1 parent 3264920 commit b9eb060

File tree

7 files changed

+39
-1
lines changed

7 files changed

+39
-1
lines changed

phpdotnet/phd/PIHandler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
namespace phpdotnet\phd;
33

44
abstract class PIHandler {
5+
/**
6+
* @var \phpdotnet\phd\Format
7+
*/
58
protected $format;
69

710
public function __construct($format) {

phpdotnet/phd/Package/Generic/XHTML.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,12 @@ abstract class Package_Generic_XHTML extends Format_Abstract_XHTML {
548548

549549
protected int $exampleCounter = 0;
550550

551+
protected int $perPageExampleCounter = 0;
552+
553+
protected bool $exampleCounterIsPerPage = false;
554+
555+
protected array $perPageExampleIds = [];
556+
551557
public function __construct(
552558
Config $config,
553559
OutputHandler $outputHandler
@@ -631,7 +637,11 @@ public function createLink($for, &$desc = null, $type = Format::SDESC) {
631637
$rsl = $this->indexes[$for];
632638
$retval = $rsl["filename"] . $this->ext;
633639
if ($rsl["filename"] != $rsl["docbook_id"]) {
634-
$retval .= '#' . $rsl["docbook_id"];
640+
if (isset($this->perPageExampleIds[$for])) {
641+
$retval .= '#' . $this->perPageExampleIds[$for];
642+
} else {
643+
$retval .= '#' . $rsl["docbook_id"];
644+
}
635645
}
636646
$desc = $rsl["sdesc"] ?: $rsl["ldesc"];
637647
}
@@ -2538,4 +2548,23 @@ public function format_whitespace($whitespace, $elementStack, $currentDepth) {
25382548
public function format_caption($open, $name, $attrs, $props) {
25392549
return $open ? '<div class="caption">' : '</div>';
25402550
}
2551+
2552+
public function getGeneratedExampleID($index)
2553+
{
2554+
$originalId = parent::getGeneratedExampleID($index);
2555+
if (! $this->exampleCounterIsPerPage) {
2556+
return $originalId;
2557+
}
2558+
if (preg_match('/^example\-[0-9]+$/', $originalId)) {
2559+
$this->perPageExampleCounter++;
2560+
$this->perPageExampleIds[$originalId] = 'example-' . $this->perPageExampleCounter;
2561+
return $this->perPageExampleIds[$originalId];
2562+
}
2563+
return $originalId;
2564+
}
2565+
2566+
public function onNewPage(): void
2567+
{
2568+
$this->perPageExampleCounter = 0;
2569+
}
25412570
}

phpdotnet/phd/Package/PHP/CHM.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ public function __construct(
208208
) {
209209
parent::__construct($config, $outputHandler);
210210
$this->registerFormatName("PHP-CHM");
211+
$this->exampleCounterIsPerPage = false;
211212
}
212213

213214
public function __destruct() {

phpdotnet/phd/Package/PHP/EnhancedCHM.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function __construct(
1515
) {
1616
parent::__construct($config, $outputHandler);
1717
$this->registerFormatName("PHP-EnhancedCHM");
18+
$this->exampleCounterIsPerPage = false;
1819
}
1920

2021
public function update($event, $value = null) {

phpdotnet/phd/Package/PHP/Epub.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function __construct(
3030
parent::__construct($config, $outputHandler);
3131
$this->setExt('.xhtml');
3232
$this->registerFormatName("PHP-Epub");
33+
$this->exampleCounterIsPerPage = false;
3334
}
3435

3536
public function update($event, $value = null) {

phpdotnet/phd/Package/PHP/Web.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function __construct(
1717
$this->setTitle("PHP Manual");
1818
$this->setChunked(true);
1919
$this->setExt($this->config->ext === null ? ".php" : $this->config->ext);
20+
$this->exampleCounterIsPerPage = true;
2021
}
2122

2223
public function close() {
@@ -54,6 +55,7 @@ public function appendData($data) {
5455
}
5556

5657
public function writeChunk($id, $fp) {
58+
$this->onNewPage();
5759
$filename = $this->getOutputDir() . $id . $this->getExt();
5860

5961
rewind($fp);

phpdotnet/phd/TestPHPChunkedXHTML.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function update($event, $value = null) {
2525
}
2626

2727
public function writeChunk($id, $fp) {
28+
$this->onNewPage();
2829
$filename = $this->getOutputDir() . $id . $this->getExt();
2930

3031
rewind($fp);

0 commit comments

Comments
 (0)