From dda2c636037adc63346ea25a8a7a18cb304d92ed Mon Sep 17 00:00:00 2001 From: haszi Date: Mon, 20 May 2024 13:33:50 +0200 Subject: [PATCH] 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 --- phpdotnet/phd/Package/Generic/XHTML.php | 29 ++++++++- .../generic/attribute_formatting_001.phpt | 60 +++++++++++++++++++ .../generic/data/attribute_formatting_001.xml | 35 +++++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 tests/package/generic/attribute_formatting_001.phpt create mode 100644 tests/package/generic/data/attribute_formatting_001.xml diff --git a/phpdotnet/phd/Package/Generic/XHTML.php b/phpdotnet/phd/Package/Generic/XHTML.php index b35c975e..56aa6481 100644 --- a/phpdotnet/phd/Package/Generic/XHTML.php +++ b/phpdotnet/phd/Package/Generic/XHTML.php @@ -145,7 +145,7 @@ abstract class Package_Generic_XHTML extends Format_Abstract_XHTML { 'methodsynopsis' => 'format_methodsynopsis', 'methodname' => 'format_methodname', 'member' => 'format_member', - 'modifier' => 'span', + 'modifier' => 'format_modifier', 'note' => 'format_note', 'orgname' => 'span', 'othercredit' => 'format_div', @@ -393,6 +393,8 @@ abstract class Package_Generic_XHTML extends Format_Abstract_XHTML { 'modifier' => array( /* DEFAULT */ false, 'fieldsynopsis' => 'format_fieldsynopsis_modifier_text', + 'methodparam' => 'format_modifier_text', + 'methodsynopsis' => 'format_modifier_text', ), /** Those are used to retrieve the class/interface name to be able to remove it from method names */ 'classname' => [ @@ -1217,6 +1219,31 @@ public function format_fieldsynopsis_modifier_text($value, $tag) { return $this->TEXT($value); } + public function format_modifier($open, $name, $attrs, $props) { + if ($open) { + if (isset($attrs[Reader::XMLNS_DOCBOOK]["role"])) { + $this->pushRole($attrs[Reader::XMLNS_DOCBOOK]["role"]); + return ''; + } + return ''; + } + if (isset($attrs[Reader::XMLNS_DOCBOOK]["role"])) { + $this->popRole(); + } + return ''; + } + + public function format_modifier_text($value, $tag) { + if ($this->getRole() === "attribute") { + $attribute = trim(strtolower($value), "#[]\\"); + $href = Format::getFilename("class.$attribute"); + if ($href) { + return '' .$value. ' '; + } + } + return false; + } + public function format_methodsynopsis($open, $name, $attrs, $props) { if ($open) { diff --git a/tests/package/generic/attribute_formatting_001.phpt b/tests/package/generic/attribute_formatting_001.phpt new file mode 100644 index 00000000..6c060192 --- /dev/null +++ b/tests/package/generic/attribute_formatting_001.phpt @@ -0,0 +1,60 @@ +--TEST-- +Attribute formatting 001 +--FILE-- + $xml_file]); + +$format = new TestGenericChunkedXHTML; + +$format->SQLiteIndex( + null, // $context, + null, // $index, + "class.knownattribute", // $id, + "file.knownattribute.is.in", // $filename, + "", // $parent, + "", // $sdesc, + "", // $ldesc, + "", // $element, + "", // $previous, + "", // $next, + 0, // $chunk +); + +$render = new TestRender(new Reader, new Config, $format); + +$render->run(); +?> +--EXPECT-- +Filename: attribute-formatting.html +Content: +
+
+

1. Class methodparameter with unknown attribute

+
public mysqli::__construct(#[\UnknownAttribute]stringnull $password = null)
+ +
+ +
+

2. Class methodparameter with known attribute

+
public mysqli::__construct(#[\KnownAttribute] stringnull $password = null)
+ +
+ +
+

3. Function parameter with unknown attribute

+
bool password_verify#[\UnknownAttribute](string $password, string $hash)
+ +
+ +
+

4. Function parameter with known attribute

+
bool password_verify#[\KnownAttribute] (string $password, string $hash)
+ +
+
diff --git a/tests/package/generic/data/attribute_formatting_001.xml b/tests/package/generic/data/attribute_formatting_001.xml new file mode 100644 index 00000000..af69ccb0 --- /dev/null +++ b/tests/package/generic/data/attribute_formatting_001.xml @@ -0,0 +1,35 @@ + +
+ 1. Class methodparameter with unknown attribute + + public mysqli::__construct + #[\UnknownAttribute]stringnullpasswordnull + +
+ +
+ 2. Class methodparameter with known attribute + + public mysqli::__construct + #[\KnownAttribute]stringnullpasswordnull + +
+ +
+ 3. Function parameter with unknown attribute + + boolpassword_verify + #[\UnknownAttribute]stringpassword + stringhash + +
+ +
+ 4. Function parameter with known attribute + + boolpassword_verify + #[\KnownAttribute]stringpassword + stringhash + +
+