[DOC-CVS] [phd] master: Make generated example IDs page specific (#210)

[email protected] (AllenJB via GitHub)
Newsgroups php.doc.cvs
Message-ID <[email protected]>
Author: AllenJB (AllenJB)
Committer: GitHub (web-flow)
Pusher: Girgias
Date: 2025-11-04T00:47:09Z

Commit: https://github.com/php/phd/commit/b9eb060be4f6649162b46477572abcdcd2fcc1aa
Raw diff: https://github.com/php/phd/commit/b9eb060be4f6649162b46477572abcdcd2fcc1aa.diff

Make generated example IDs page specific (#210)

* Make example id attribute counter reset on each page (fixes #205)

Changed paths:
  M  phpdotnet/phd/PIHandler.php
  M  phpdotnet/phd/Package/Generic/XHTML.php
  M  phpdotnet/phd/Package/PHP/CHM.php
  M  phpdotnet/phd/Package/PHP/EnhancedCHM.php
  M  phpdotnet/phd/Package/PHP/Epub.php
  M  phpdotnet/phd/Package/PHP/Web.php
  M  phpdotnet/phd/TestPHPChunkedXHTML.php


Diff:

diff --git a/phpdotnet/phd/PIHandler.php b/phpdotnet/phd/PIHandler.php
index dc1ed4c2..92b33b42 100644
--- a/phpdotnet/phd/PIHandler.php
+++ b/phpdotnet/phd/PIHandler.php
@@ -2,6 +2,9 @@
 namespace phpdotnet\phd;
 
 abstract class PIHandler {
+    /**
+     * @var \phpdotnet\phd\Format
+     */
     protected $format;
 
     public function __construct($format) {
diff --git a/phpdotnet/phd/Package/Generic/XHTML.php b/phpdotnet/phd/Package/Generic/XHTML.php
index ad9dc327..8c419e42 100644
--- a/phpdotnet/phd/Package/Generic/XHTML.php
+++ b/phpdotnet/phd/Package/Generic/XHTML.php
@@ -548,6 +548,12 @@ abstract class Package_Generic_XHTML extends Format_Abstract_XHTML {
 
     protected int $exampleCounter = 0;
 
+    protected int $perPageExampleCounter = 0;
+
+    protected bool $exampleCounterIsPerPage = false;
+
+    protected array $perPageExampleIds = [];
+
     public function __construct(
         Config $config,
         OutputHandler $outputHandler
@@ -631,7 +637,11 @@ public function createLink($for, &$desc = null, $type = Format::SDESC) {
             $rsl = $this->indexes[$for];
             $retval = $rsl["filename"] . $this->ext;
             if ($rsl["filename"] != $rsl["docbook_id"]) {
-                $retval .= '#' . $rsl["docbook_id"];
+                if (isset($this->perPageExampleIds[$for])) {
+                    $retval .= '#' . $this->perPageExampleIds[$for];
+                } else {
+                    $retval .= '#' . $rsl["docbook_id"];
+                }
             }
             $desc = $rsl["sdesc"] ?: $rsl["ldesc"];
         }
@@ -2538,4 +2548,23 @@ public function format_whitespace($whitespace, $elementStack, $currentDepth) {
     public function format_caption($open, $name, $attrs, $props) {
         return $open ? '<div class="caption">' : '</div>';
     }
+
+    public function getGeneratedExampleID($index)
+    {
+        $originalId = parent::getGeneratedExampleID($index);
+        if (! $this->exampleCounterIsPerPage) {
+            return $originalId;
+        }
+        if (preg_match('/^example\-[0-9]+$/', $originalId)) {
+            $this->perPageExampleCounter++;
+            $this->perPageExampleIds[$originalId] = 'example-' . $this->perPageExampleCounter;
+            return $this->perPageExampleIds[$originalId];
+        }
+        return $originalId;
+    }
+
+    public function onNewPage(): void
+    {
+        $this->perPageExampleCounter = 0;
+    }
 }
diff --git a/phpdotnet/phd/Package/PHP/CHM.php b/phpdotnet/phd/Package/PHP/CHM.php
index f172f26d..40562440 100644
--- a/phpdotnet/phd/Package/PHP/CHM.php
+++ b/phpdotnet/phd/Package/PHP/CHM.php
@@ -208,6 +208,7 @@ public function __construct(
     ) {
         parent::__construct($config, $outputHandler);
         $this->registerFormatName("PHP-CHM");
+        $this->exampleCounterIsPerPage = false;
     }
 
     public function __destruct() {
diff --git a/phpdotnet/phd/Package/PHP/EnhancedCHM.php b/phpdotnet/phd/Package/PHP/EnhancedCHM.php
index 9be101cc..25532a10 100644
--- a/phpdotnet/phd/Package/PHP/EnhancedCHM.php
+++ b/phpdotnet/phd/Package/PHP/EnhancedCHM.php
@@ -15,6 +15,7 @@ public function __construct(
     ) {
         parent::__construct($config, $outputHandler);
         $this->registerFormatName("PHP-EnhancedCHM");
+        $this->exampleCounterIsPerPage = false;
     }
 
     public function update($event, $value = null) {
diff --git a/phpdotnet/phd/Package/PHP/Epub.php b/phpdotnet/phd/Package/PHP/Epub.php
index ef16501b..6f15c7cb 100644
--- a/phpdotnet/phd/Package/PHP/Epub.php
+++ b/phpdotnet/phd/Package/PHP/Epub.php
@@ -30,6 +30,7 @@ public function __construct(
         parent::__construct($config, $outputHandler);
         $this->setExt('.xhtml');
         $this->registerFormatName("PHP-Epub");
+        $this->exampleCounterIsPerPage = false;
     }
 
     public function update($event, $value = null) {
diff --git a/phpdotnet/phd/Package/PHP/Web.php b/phpdotnet/phd/Package/PHP/Web.php
index 31068bbb..68cae82d 100644
--- a/phpdotnet/phd/Package/PHP/Web.php
+++ b/phpdotnet/phd/Package/PHP/Web.php
@@ -17,6 +17,7 @@ public function __construct(
         $this->setTitle("PHP Manual");
         $this->setChunked(true);
         $this->setExt($this->config->ext === null ? ".php" : $this->config->ext);
+        $this->exampleCounterIsPerPage = true;
     }
 
     public function close() {
@@ -54,6 +55,7 @@ public function appendData($data) {
     }
 
     public function writeChunk($id, $fp) {
+        $this->onNewPage();
         $filename = $this->getOutputDir() . $id . $this->getExt();
 
         rewind($fp);
diff --git a/phpdotnet/phd/TestPHPChunkedXHTML.php b/phpdotnet/phd/TestPHPChunkedXHTML.php
index ef15fbe3..7828e128 100644
--- a/phpdotnet/phd/TestPHPChunkedXHTML.php
+++ b/phpdotnet/phd/TestPHPChunkedXHTML.php
@@ -25,6 +25,7 @@ public function update($event, $value = null) {
     }
 
     public function writeChunk($id, $fp) {
+        $this->onNewPage();
         $filename = $this->getOutputDir() . $id . $this->getExt();
 
         rewind($fp);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.