@@ -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}
0 commit comments