Skip to content

Commit

Permalink
Trim leading and trailing underscores in constant IDs (#106)
Browse files Browse the repository at this point in the history
Co-authored-by: haszi <[email protected]>
  • Loading branch information
haszi and haszi authored Mar 3, 2024
1 parent 262926d commit a22459f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
14 changes: 9 additions & 5 deletions phpdotnet/phd/Package/Generic/XHTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -1612,13 +1612,17 @@ public function format_constant($open, $name, $attrs)
public function format_constant_text($value, $tag) {
if (str_contains($value, '::')) {
// class constant
$normalizedLinkFormat = str_replace(
array("::", "\\", "_"),
array(".constants.", "-", "-"),
strtolower($value)
list($extensionAndClass, $constant) = explode(
"::",
str_replace(
array("\\", "_"),
array("-", "-"),
strtolower($value)
)
);
$normalizedLinkFormat = $extensionAndClass . ".constants." . trim($constant, "-");
} else {
$normalizedLinkFormat = 'constant.' . str_replace('_', '-', strtolower($value));
$normalizedLinkFormat = 'constant.' . str_replace('_', '-', strtolower(trim($value, "_")));
}
$link = $this->createLink($normalizedLinkFormat);

Expand Down
16 changes: 16 additions & 0 deletions tests/package/php/constant_links_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ $indices = [
"docbook_id" => "vendor-namespace.constants.definitely-exists2",
"filename" => "extensionname2.constantspage2",
],
[
"docbook_id" => "constant.leading-and-trailing-undescores",
"filename" => "extensionname3.constantspage3",
],
[
"docbook_id" => "extension-class.constants.leading-and-trailing-undescores2",
"filename" => "extensionname4.constantspage4",
],
];

$format = new TestPHPChunkedXHTML;
Expand Down Expand Up @@ -64,4 +72,12 @@ Content:
</p>
</div>

<div class="section">
<p class="para">%d. Constant with leading and trailing underscores in ID</p>
<strong><code><a href="extensionname3.constantspage3.html#constant.leading-and-trailing-undescores">__LEADING_AND_TRAILING_UNDESCORES__</a></code></strong>
<p class="para">
<strong><code><a href="extensionname4.constantspage4.html#extension-class.constants.leading-and-trailing-undescores2">Extension\Class::__LEADING_AND_TRAILING_UNDESCORES2__</a></code></strong>
</p>
</div>

</div>
8 changes: 8 additions & 0 deletions tests/package/php/data/constant_links.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@
</para>
</section>

<section>
<para>3. Constant with leading and trailing underscores in ID</para>
<constant>__LEADING_AND_TRAILING_UNDESCORES__</constant>
<para>
<constant>Extension\Class::__LEADING_AND_TRAILING_UNDESCORES2__</constant>
</para>
</section>

</chapter>

0 comments on commit a22459f

Please sign in to comment.