1
0
mirror of https://github.com/php/phd.git synced 2026-03-23 22:52:05 +01:00

Make generated example IDs page specific (#210)

* Make example id attribute counter reset on each page (fixes #205)
This commit is contained in:
AllenJB
2025-11-04 00:47:09 +00:00
committed by GitHub
parent 3264920aa1
commit b9eb060be4
7 changed files with 39 additions and 1 deletions

View File

@@ -2,6 +2,9 @@
namespace phpdotnet\phd;
abstract class PIHandler {
/**
* @var \phpdotnet\phd\Format
*/
protected $format;
public function __construct($format) {

View File

@@ -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 @@ abstract class Package_Generic_XHTML extends Format_Abstract_XHTML {
$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 @@ abstract class Package_Generic_XHTML extends Format_Abstract_XHTML {
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;
}
}

View File

@@ -208,6 +208,7 @@ class Package_PHP_CHM extends Package_PHP_ChunkedXHTML
) {
parent::__construct($config, $outputHandler);
$this->registerFormatName("PHP-CHM");
$this->exampleCounterIsPerPage = false;
}
public function __destruct() {

View File

@@ -15,6 +15,7 @@ class Package_PHP_EnhancedCHM extends Package_PHP_CHM
) {
parent::__construct($config, $outputHandler);
$this->registerFormatName("PHP-EnhancedCHM");
$this->exampleCounterIsPerPage = false;
}
public function update($event, $value = null) {

View File

@@ -30,6 +30,7 @@ class Package_PHP_Epub extends Package_PHP_ChunkedXHTML
parent::__construct($config, $outputHandler);
$this->setExt('.xhtml');
$this->registerFormatName("PHP-Epub");
$this->exampleCounterIsPerPage = false;
}
public function update($event, $value = null) {

View File

@@ -17,6 +17,7 @@ class Package_PHP_Web extends Package_PHP_XHTML {
$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 @@ class Package_PHP_Web extends Package_PHP_XHTML {
}
public function writeChunk($id, $fp) {
$this->onNewPage();
$filename = $this->getOutputDir() . $id . $this->getExt();
rewind($fp);

View File

@@ -25,6 +25,7 @@ class TestPHPChunkedXHTML extends Package_PHP_ChunkedXHTML {
}
public function writeChunk($id, $fp) {
$this->onNewPage();
$filename = $this->getOutputDir() . $id . $this->getExt();
rewind($fp);