Skip to content

Commit 10fa58b

Browse files
committed
Generators/HTML: move section link creation to own function
Move the creation of in-page anchor link text to a separate function as this was done in two separate places. The separate function will make it more straight-forward to safeguard that the anchors and the links to these anchors stay in sync.
1 parent 82a9ec2 commit 10fa58b

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/Generators/HTML.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ protected function getFormattedToc()
215215
$doc->load($file);
216216
$documentation = $doc->getElementsByTagName('documentation')->item(0);
217217
$title = $this->getTitle($documentation);
218-
$output .= sprintf($listItemTemplate, str_replace(' ', '-', $title), $title);
218+
$output .= sprintf($listItemTemplate, $this->titleToAnchor($title), $title);
219219
}
220220

221221
$output .= ' </ul>'.PHP_EOL;
@@ -290,14 +290,30 @@ public function processSniff(DOMNode $doc)
290290

291291
if (trim($content) !== '') {
292292
$title = $this->getTitle($doc);
293-
echo ' <a name="'.str_replace(' ', '-', $title).'" />'.PHP_EOL;
293+
echo ' <a name="'.$this->titleToAnchor($title).'" />'.PHP_EOL;
294294
echo ' <h2>'.$title.'</h2>'.PHP_EOL;
295295
echo $content;
296296
}
297297

298298
}//end processSniff()
299299

300300

301+
/**
302+
* Transform a title to a string which can be used as an HTML anchor.
303+
*
304+
* @param string $title The title.
305+
*
306+
* @since 3.12.0
307+
*
308+
* @return string
309+
*/
310+
private function titleToAnchor($title)
311+
{
312+
return str_replace(' ', '-', $title);
313+
314+
}//end titleToAnchor()
315+
316+
301317
/**
302318
* Print a text block found in a standard.
303319
*

0 commit comments

Comments
 (0)