Skip to content

Commit dda2c63

Browse files
haszihaszi
andauthored
Add basic Attribute linking (#127)
This adds support for 'known' attributes on method and function parameters. 'Known' in this context means an attribute that has been declared in the documentation with an ID in the `class.attribute_name` format (all current predefined attributes are supported). Current limitations are: - No support for namespaced attributes - No support for attributes on a return type Co-authored-by: haszi <[email protected]>
1 parent b535eeb commit dda2c63

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

phpdotnet/phd/Package/Generic/XHTML.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ abstract class Package_Generic_XHTML extends Format_Abstract_XHTML {
145145
'methodsynopsis' => 'format_methodsynopsis',
146146
'methodname' => 'format_methodname',
147147
'member' => 'format_member',
148-
'modifier' => 'span',
148+
'modifier' => 'format_modifier',
149149
'note' => 'format_note',
150150
'orgname' => 'span',
151151
'othercredit' => 'format_div',
@@ -393,6 +393,8 @@ abstract class Package_Generic_XHTML extends Format_Abstract_XHTML {
393393
'modifier' => array(
394394
/* DEFAULT */ false,
395395
'fieldsynopsis' => 'format_fieldsynopsis_modifier_text',
396+
'methodparam' => 'format_modifier_text',
397+
'methodsynopsis' => 'format_modifier_text',
396398
),
397399
/** Those are used to retrieve the class/interface name to be able to remove it from method names */
398400
'classname' => [
@@ -1217,6 +1219,31 @@ public function format_fieldsynopsis_modifier_text($value, $tag) {
12171219
return $this->TEXT($value);
12181220
}
12191221

1222+
public function format_modifier($open, $name, $attrs, $props) {
1223+
if ($open) {
1224+
if (isset($attrs[Reader::XMLNS_DOCBOOK]["role"])) {
1225+
$this->pushRole($attrs[Reader::XMLNS_DOCBOOK]["role"]);
1226+
return '<span class="' . $this->getRole() . '">';
1227+
}
1228+
return '<span class="modifier">';
1229+
}
1230+
if (isset($attrs[Reader::XMLNS_DOCBOOK]["role"])) {
1231+
$this->popRole();
1232+
}
1233+
return '</span>';
1234+
}
1235+
1236+
public function format_modifier_text($value, $tag) {
1237+
if ($this->getRole() === "attribute") {
1238+
$attribute = trim(strtolower($value), "#[]\\");
1239+
$href = Format::getFilename("class.$attribute");
1240+
if ($href) {
1241+
return '<a href="' . $href . $this->getExt() . '">' .$value. '</a> ';
1242+
}
1243+
}
1244+
return false;
1245+
}
1246+
12201247
public function format_methodsynopsis($open, $name, $attrs, $props) {
12211248
if ($open) {
12221249

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
--TEST--
2+
Attribute formatting 001
3+
--FILE--
4+
<?php
5+
namespace phpdotnet\phd;
6+
7+
require_once __DIR__ . "/../../setup.php";
8+
9+
$xml_file = __DIR__ . "/data/attribute_formatting_001.xml";
10+
11+
Config::init(["xml_file" => $xml_file]);
12+
13+
$format = new TestGenericChunkedXHTML;
14+
15+
$format->SQLiteIndex(
16+
null, // $context,
17+
null, // $index,
18+
"class.knownattribute", // $id,
19+
"file.knownattribute.is.in", // $filename,
20+
"", // $parent,
21+
"", // $sdesc,
22+
"", // $ldesc,
23+
"", // $element,
24+
"", // $previous,
25+
"", // $next,
26+
0, // $chunk
27+
);
28+
29+
$render = new TestRender(new Reader, new Config, $format);
30+
31+
$render->run();
32+
?>
33+
--EXPECT--
34+
Filename: attribute-formatting.html
35+
Content:
36+
<div id="attribute-formatting" class="chapter">
37+
<div class="section">
38+
<p class="para">1. Class methodparameter with unknown attribute</p>
39+
<div class="constructorsynopsis dc-description"><span class="modifier">public</span> <span class="methodname">mysqli::__construct</span>(<span class="methodparam"><span class="attribute">#[\UnknownAttribute]</span><span class="type"><span class="type">string</span><span class="type">null</span></span> <code class="parameter">$password</code><span class="initializer"> = <span class="type">null</span></span></span>)</div>
40+
41+
</div>
42+
43+
<div class="section">
44+
<p class="para">2. Class methodparameter with known attribute</p>
45+
<div class="constructorsynopsis dc-description"><span class="modifier">public</span> <span class="methodname">mysqli::__construct</span>(<span class="methodparam"><span class="attribute"><a href="file.knownattribute.is.in.html">#[\KnownAttribute]</a> </span><span class="type"><span class="type">string</span><span class="type">null</span></span> <code class="parameter">$password</code><span class="initializer"> = <span class="type">null</span></span></span>)</div>
46+
47+
</div>
48+
49+
<div class="section">
50+
<p class="para">3. Function parameter with unknown attribute</p>
51+
<div class="methodsynopsis dc-description"><span class="type">bool</span> <span class="methodname">password_verify</span><span class="attribute">#[\UnknownAttribute]</span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$password</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$hash</code></span>)</div>
52+
53+
</div>
54+
55+
<div class="section">
56+
<p class="para">4. Function parameter with known attribute</p>
57+
<div class="methodsynopsis dc-description"><span class="type">bool</span> <span class="methodname">password_verify</span><span class="attribute"><a href="file.knownattribute.is.in.html">#[\KnownAttribute]</a> </span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$password</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$hash</code></span>)</div>
58+
59+
</div>
60+
</div>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<chapter xml:id="attribute-formatting">
2+
<section>
3+
<para>1. Class methodparameter with unknown attribute</para>
4+
<constructorsynopsis role="mysqli">
5+
<modifier>public</modifier> <methodname>mysqli::__construct</methodname>
6+
<methodparam><modifier role="attribute">#[\UnknownAttribute]</modifier><type class="union"><type>string</type><type>null</type></type><parameter>password</parameter><initializer><type>null</type></initializer></methodparam>
7+
</constructorsynopsis>
8+
</section>
9+
10+
<section>
11+
<para>2. Class methodparameter with known attribute</para>
12+
<constructorsynopsis role="mysqli">
13+
<modifier>public</modifier> <methodname>mysqli::__construct</methodname>
14+
<methodparam><modifier role="attribute">#[\KnownAttribute]</modifier><type class="union"><type>string</type><type>null</type></type><parameter>password</parameter><initializer><type>null</type></initializer></methodparam>
15+
</constructorsynopsis>
16+
</section>
17+
18+
<section>
19+
<para>3. Function parameter with unknown attribute</para>
20+
<methodsynopsis>
21+
<type>bool</type><methodname>password_verify</methodname>
22+
<modifier role="attribute">#[\UnknownAttribute]</modifier><methodparam><type>string</type><parameter>password</parameter></methodparam>
23+
<methodparam><type>string</type><parameter>hash</parameter></methodparam>
24+
</methodsynopsis>
25+
</section>
26+
27+
<section>
28+
<para>4. Function parameter with known attribute</para>
29+
<methodsynopsis>
30+
<type>bool</type><methodname>password_verify</methodname>
31+
<modifier role="attribute">#[\KnownAttribute]</modifier><methodparam><type>string</type><parameter>password</parameter></methodparam>
32+
<methodparam><type>string</type><parameter>hash</parameter></methodparam>
33+
</methodsynopsis>
34+
</section>
35+
</chapter>

0 commit comments

Comments
 (0)