Skip to content

Commit bcd40cb

Browse files
committed
Add linking support for enumidentifier elements
When <enumidentifier> contains a fully qualified name with :: separator (e.g., RoundingMode::HalfAwayFromZero), the enum name is extracted and used to generate a link to the corresponding enum documentation page. Links are not generated in enumsynopsis context (where enumidentifier defines the case) or when the enum is not found in the index. Refs #180
1 parent 94548d1 commit bcd40cb

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

phpdotnet/phd/Package/PHP/XHTML.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ abstract class Package_PHP_XHTML extends Package_Generic_XHTML {
125125
'classsynopsis' => 'format_classsynopsis_oo_name_text',
126126
]
127127
],
128+
'enumidentifier' => [
129+
/* DEFAULT */ 'format_enumidentifier_text',
130+
'enumsynopsis' => false,
131+
],
128132
'methodname' => array(
129133
/* DEFAULT */ 'format_function_text',
130134
'constructorsynopsis' => array(
@@ -898,6 +902,25 @@ public function format_classname_text($value, $tag) {
898902
return '<strong class="' .$tag. '">' .$value. '</strong>';
899903
}
900904

905+
public function format_enumidentifier_text($value, $tag) {
906+
if (!str_contains($value, '::')) {
907+
return $value;
908+
}
909+
910+
list($enumName) = explode('::', $value);
911+
$t = strtr($this->normalizeFQN($enumName), ["_" => "-", "\\" => "-"]);
912+
$href = Format::getFilename("enum.$t");
913+
914+
if ($href === false) {
915+
return $value;
916+
}
917+
918+
if ($this->chunked) {
919+
return '<a href="' . $href . $this->ext . '" class="' . $tag . '">' . $value . '</a>';
920+
}
921+
return '<a href="#' . $href . '" class="' . $tag . '">' . $value . '</a>';
922+
}
923+
901924

902925
/*Chunk Functions*/
903926
private function isChunkedByAttributes(array $attributes): bool {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<chapter xml:id="enumidentifier_link_rendering" xmlns="http://docbook.org/ns/docbook">
3+
4+
<section>
5+
<para>1. Enum case inside enumsynopsis (no link - definition context)</para>
6+
<enumsynopsis>
7+
<enumname>TestEnum</enumname>
8+
<enumitem>
9+
<enumidentifier>CaseDefinition</enumidentifier>
10+
</enumitem>
11+
</enumsynopsis>
12+
</section>
13+
14+
<section>
15+
<para>2. Enum case reference with FQN (linked)</para>
16+
<enumidentifier>Enum\Namespace\Existing_Enum::SomeCase</enumidentifier>
17+
</section>
18+
19+
<section>
20+
<para>3. Enum case reference with FQN and leading backslash (linked)</para>
21+
<enumidentifier>\Enum\Namespace\Existing_Enum::AnotherCase</enumidentifier>
22+
</section>
23+
24+
<section>
25+
<para>4. Enum case without namespace separator (no link)</para>
26+
<enumidentifier>JustCaseName</enumidentifier>
27+
</section>
28+
29+
<section>
30+
<para>5. Enum case for non-existent enum (no link)</para>
31+
<enumidentifier>NonExistent\Enum::SomeCase</enumidentifier>
32+
</section>
33+
34+
</chapter>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
--TEST--
2+
Enum case (enumidentifier) link rendering
3+
--FILE--
4+
<?php
5+
namespace phpdotnet\phd;
6+
7+
require_once __DIR__ . "/../../setup.php";
8+
9+
$config->xmlFile = __DIR__ . "/data/enumidentifier_link_rendering.xml";
10+
11+
$format = new TestPHPChunkedXHTML($config, $outputHandler);
12+
$format->SQLiteIndex(
13+
null, // $context,
14+
null, // $index,
15+
"enum.enum-namespace-existing-enum", // $id,
16+
"enum.enum-namespace-existing-enum", // $filename,
17+
"", // $parent,
18+
"", // $sdesc,
19+
"", // $ldesc,
20+
"phpdoc:classref", // $element,
21+
"", // $previous,
22+
"", // $next,
23+
0, // $chunk
24+
);
25+
26+
$render = new TestRender(new Reader($outputHandler), $config, $format);
27+
$render->run();
28+
?>
29+
--EXPECT--
30+
Filename: enumidentifier_link_rendering.html
31+
Content:
32+
<div id="enumidentifier_link_rendering" class="chapter">
33+
34+
<div class="section">
35+
<p class="para">1. Enum case inside enumsynopsis (no link - definition context)</p>
36+
<div class="classsynopsis"><div class="classsynopsisinfo">
37+
<span class="modifier">enum</span> <strong class="classname"><strong class="enumname">TestEnum</strong></strong><br/>{</div>
38+
<div class="fieldsynopsis">
39+
<span class="modifier">case</span> <span class="classname">CaseDefinition</span>
40+
</div>
41+
}</div>
42+
</div>
43+
44+
<div class="section">
45+
<p class="para">2. Enum case reference with FQN (linked)</p>
46+
<span class="modifier">case</span> <span class="classname"><a href="enum.enum-namespace-existing-enum.html" class="enumidentifier">Enum\Namespace\Existing_Enum::SomeCase</a></span>
47+
</div>
48+
49+
<div class="section">
50+
<p class="para">3. Enum case reference with FQN and leading backslash (linked)</p>
51+
<span class="modifier">case</span> <span class="classname"><a href="enum.enum-namespace-existing-enum.html" class="enumidentifier">\Enum\Namespace\Existing_Enum::AnotherCase</a></span>
52+
</div>
53+
54+
<div class="section">
55+
<p class="para">4. Enum case without namespace separator (no link)</p>
56+
<span class="modifier">case</span> <span class="classname">JustCaseName</span>
57+
</div>
58+
59+
<div class="section">
60+
<p class="para">5. Enum case for non-existent enum (no link)</p>
61+
<span class="modifier">case</span> <span class="classname">NonExistent\Enum::SomeCase</span>
62+
</div>
63+
64+
</div>

0 commit comments

Comments
 (0)