-
Notifications
You must be signed in to change notification settings - Fork 50
Fix FQN class, method, property and constant linking #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1811,7 +1811,7 @@ private function convertConstantNameToId(string $constantName): string { | |
$tempLinkValue = str_replace( | ||
array("\\", "_"), | ||
array("-", "-"), | ||
strtolower(trim($constantName, "_")) | ||
trim($this->normalizeFQN($constantName), "_") | ||
); | ||
|
||
if (str_contains($constantName, '::')) { | ||
|
@@ -1855,7 +1855,7 @@ public function format_property_text($value, $tag) { | |
$tempLinkValue = str_replace( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The replace list here differs from other point. Maybe there is necessary two normalize functions, or if the difference is accidental, to test what list of substitutions is the correct. |
||
["\\", "_", "$"], | ||
["-", "-", ""], | ||
strtolower(trim($value, "_")) | ||
trim($this->normalizeFQN($value), "_") | ||
); | ||
|
||
list($extensionAndClass, $property) = explode("::", $tempLinkValue); | ||
|
@@ -1870,6 +1870,10 @@ public function format_property_text($value, $tag) { | |
return '<a href="' . $link . '">' . $value . '</a>'; | ||
} | ||
|
||
protected function normalizeFQN(string $fqn): string { | ||
return \ltrim(\strtolower($fqn), "\\"); | ||
} | ||
|
||
public function admonition_title($title, $lang) | ||
{ | ||
return '<strong class="' .(strtolower($title)). '">' .($this->autogen($title, $lang)). '</strong>'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -666,7 +666,7 @@ public function format_type_methodparam_text($type, $tagname) { | |
} | ||
|
||
public function format_type_text($type, $tagname) { | ||
$t = strtr(strtolower($type), ["_" => "-", "\\" => "-"]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note how these substations lists are copied everywhere. |
||
$t = strtr($this->normalizeFQN($type), ["_" => "-", "\\" => "-"]); | ||
$fragment = ""; | ||
|
||
switch($t) { | ||
|
@@ -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>'; | ||
} | ||
|
@@ -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 = $this->normalizeFQN($value); | ||
$filename = $this->getRefnameLink($ref); | ||
} | ||
if ($filename !== null) { | ||
|
@@ -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($this->normalizeFQN($value))) !== null && $this->cchunk["class_name_ref"] !== strtolower($value)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is somewhat unrelated, but in a future version, first test if |
||
$href = $this->chunked ? $filename.$this->ext : "#$filename"; | ||
return '<a href="'.$href. '" class="' .$tag. '">' .$value. '</a>'; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
--TEST-- | ||
Class and method link rendering 001 | ||
--FILE-- | ||
<?php | ||
namespace phpdotnet\phd; | ||
|
||
require_once __DIR__ . "/../../setup.php"; | ||
|
||
$config->xmlFile = __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> |
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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a future version, move all transformations inside the normalize function (trim, replace), to make understanding the code easier.