Skip to content

Commit

Permalink
Fix FQN class and method linking
Browse files Browse the repository at this point in the history
Closes GH-170
  • Loading branch information
haszi committed Jan 1, 2025
1 parent f128079 commit 80c2cd3
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 4 deletions.
8 changes: 4 additions & 4 deletions phpdotnet/phd/Package/PHP/XHTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ public function format_type_methodparam_text($type, $tagname) {
}

public function format_type_text($type, $tagname) {
$t = strtr(strtolower($type), ["_" => "-", "\\" => "-"]);
$t = strtr(ltrim(strtolower($type), "\\"), ["_" => "-", "\\" => "-"]);
$fragment = "";

switch($t) {
Expand Down Expand Up @@ -714,7 +714,7 @@ public function format_type_text($type, $tagname) {
return false;
}

$classNames = ($type === "?") ? ($tagname . ' null') : ($tagname . ' ' . $type);
$classNames = ($type === "?") ? ($tagname . ' null') : ($tagname . ' ' . ltrim($type, "\\"));
if ($href && $this->chunked) {
return '<a href="' .$href. $this->getExt().($fragment ? "#$fragment" : ""). '" class="' . $classNames . '">' .$type. '</a>';
}
Expand Down Expand Up @@ -837,7 +837,7 @@ public function format_function_text($value, $tag, $display_value = null) {
if (isset($non_functions[$value])) {
$filename = "function." . str_replace("_", "-", $value);
} else {
$ref = strtolower($value);
$ref = ltrim(strtolower($value), "\\");
$filename = $this->getRefnameLink($ref);
}
if ($filename !== null) {
Expand Down Expand Up @@ -880,7 +880,7 @@ public function format_classsynopsis_oo_name_text($value, $tag) {
}

public function format_classname_text($value, $tag) {
if (($filename = $this->getClassnameLink(strtolower($value))) !== null && $this->cchunk["class_name_ref"] !== strtolower($value)) {
if (($filename = $this->getClassnameLink(ltrim(strtolower($value), "\\"))) !== null && $this->cchunk["class_name_ref"] !== strtolower($value)) {
$href = $this->chunked ? $filename.$this->ext : "#$filename";
return '<a href="'.$href. '" class="' .$tag. '">' .$value. '</a>';
}
Expand Down
79 changes: 79 additions & 0 deletions tests/package/php/class_and_method_link_rendering_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
--TEST--
Class and method link rendering 001
--FILE--
<?php
namespace phpdotnet\phd;

require_once __DIR__ . "/../../setup.php";

$config->setXml_file(
__DIR__ . "/data/class_and_method_link_rendering_001.xml"
);

$indices = [
[
"docbook_id" => "class.extension-namespace-existing-class",
"filename" => "extensionname.classpage",
"element" => "phpdoc:classref",
],
[
"docbook_id" => "extension-namespace-classname.existingmethodname",
"filename" => "extension-namespace-classname.methodpage",
],
];

$format = new TestPHPChunkedXHTML($config, $outputHandler);

foreach ($indices as $index) {
$format->SQLiteIndex(
null, // $context,
null, // $index,
$index["docbook_id"] ?? "", // $id,
$index["filename"] ?? "", // $filename,
$index["parent_id"] ?? "", // $parent,
$index["sdesc"] ?? "", // $sdesc,
$index["ldesc"] ?? "", // $ldesc,
$index["element"] ?? "", // $element,
$index["previous"] ?? "", // $previous,
$index["next"] ?? "", // $next,
$index["chunk"] ?? 0, // $chunk
);
}

$format->addClassname("class.extension-namespace-existing-class", "extension\\namespace\\existing_class");
$format->addRefname("extension-namespace-classname.existingmethodname", "extension\\namespace\\classname::existingmethodname");

$render = new TestRender(new Reader($outputHandler), $config, $format);

$render->run();
?>
--EXPECTF--
Filename: class_and_method_link_rendering.html
Content:
<div id="class_and_method_link_rendering" class="chapter">

<div class="section">
<p class="para">1. Class linking</p>
<span class="classname"><a href="class.extension-namespace-existing-class.html" class="classname">Extension\Namespace\Existing_Class</a></span>
<span class="classname"><a href="class.extension-namespace-existing-class.html" class="classname">\Extension\Namespace\Existing_Class</a></span>
</div>

<div class="section">
<p class="para">2. Method/Function linking</p>
<span class="methodname"><a href="extension-namespace-classname.existingmethodname.html" class="methodname">Extension\Namespace\Classname::existingMethodName()</a></span>
<span class="methodname"><a href="extension-namespace-classname.existingmethodname.html" class="methodname">\Extension\Namespace\Classname::existingMethodName()</a></span>
</div>

<div class="section">
<p class="para">3. Class linking (non-FQN) in method/function parameter and return type</p>
<div class="methodsynopsis dc-description"><span class="methodname"><strong>method_name</strong></span>(<span class="methodparam"><span class="type"><a href="extensionname.classpage.html" class="type Extension\Namespace\Existing_Class">Extension\Namespace\Existing_Class</a></span> <code class="parameter">$paramName</code></span>): <span class="type"><a href="extensionname.classpage.html" class="type Extension\Namespace\Existing_Class">Extension\Namespace\Existing_Class</a></span></div>

</div>

<div class="section">
<p class="para">4. Class linking (FQN) in method/function parameter and return type</p>
<div class="methodsynopsis dc-description"><span class="methodname"><strong>method_name</strong></span>(<span class="methodparam"><span class="type"><a href="extensionname.classpage.html" class="type Extension\Namespace\Existing_Class">\Extension\Namespace\Existing_Class</a></span> <code class="parameter">$paramName</code></span>): <span class="type"><a href="extensionname.classpage.html" class="type Extension\Namespace\Existing_Class">\Extension\Namespace\Existing_Class</a></span></div>

</div>

</div>
32 changes: 32 additions & 0 deletions tests/package/php/data/class_and_method_link_rendering_001.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<chapter xml:id="class_and_method_link_rendering" xmlns="http://docbook.org/ns/docbook">

<section>
<para>1. Class linking</para>
<classname>Extension\Namespace\Existing_Class</classname>
<classname>\Extension\Namespace\Existing_Class</classname>
</section>

<section>
<para>2. Method/Function linking</para>
<methodname>Extension\Namespace\Classname::existingMethodName</methodname>
<methodname>\Extension\Namespace\Classname::existingMethodName</methodname>
</section>

<section>
<para>3. Class linking (non-FQN) in method/function parameter and return type</para>
<methodsynopsis>
<type>Extension\Namespace\Existing_Class</type><methodname>method_name</methodname>
<methodparam><type>Extension\Namespace\Existing_Class</type><parameter>paramName</parameter></methodparam>
</methodsynopsis>
</section>

<section>
<para>4. Class linking (FQN) in method/function parameter and return type</para>
<methodsynopsis>
<type>\Extension\Namespace\Existing_Class</type><methodname>method_name</methodname>
<methodparam><type>\Extension\Namespace\Existing_Class</type><parameter>paramName</parameter></methodparam>
</methodsynopsis>
</section>

</chapter>

0 comments on commit 80c2cd3

Please sign in to comment.