From d5cf4984a95cfa7a22ab549d94de346eb7d2058d Mon Sep 17 00:00:00 2001 From: haszi Date: Thu, 22 Feb 2024 20:01:43 +0100 Subject: [PATCH] Add links to constants --- phpdotnet/phd/Package/Generic/XHTML.php | 23 ++++++++- tests/TestRender.php | 23 ++++++++- tests/php/constant_links_001.phpt | 66 +++++++++++++++++++++++++ tests/php/data/constant_links.xml | 20 ++++++++ 4 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 tests/php/constant_links_001.phpt create mode 100644 tests/php/data/constant_links.xml diff --git a/phpdotnet/phd/Package/Generic/XHTML.php b/phpdotnet/phd/Package/Generic/XHTML.php index 3c5ee6a1..7fda77b1 100644 --- a/phpdotnet/phd/Package/Generic/XHTML.php +++ b/phpdotnet/phd/Package/Generic/XHTML.php @@ -437,9 +437,9 @@ abstract class Package_Generic_XHTML extends Format_Abstract_XHTML { ), /* FIXME: This is one crazy stupid workaround for footnotes */ 'constant' => array( - /* DEFAULT */ false, + /* DEFAULT */ 'format_constant_text', 'para' => array( - /* DEFAULT */ false, + /* DEFAULT */ 'format_constant_text', 'footnote' => 'format_footnote_constant_text', ), ), @@ -1613,6 +1613,25 @@ public function format_constant($open, $name, $attrs) } return ""; } + public function format_constant_text($value, $tag) { + if (str_contains($value, '::')) { + // class constant + $normalizedLinkFormat = str_replace( + array("::", "\\", "_"), + array(".constants.", "-", "-"), + strtolower($value) + ); + } else { + $normalizedLinkFormat = 'constant.' . str_replace('_', '-', strtolower($value)); + } + $link = $this->createLink($normalizedLinkFormat); + + if ($link === null) { + return $value; + } + + return '' . $value . ''; + } public function admonition_title($title, $lang) { return '' .($this->autogen($title, $lang)). ''; diff --git a/tests/TestRender.php b/tests/TestRender.php index 0fd8deab..6c74f8dc 100644 --- a/tests/TestRender.php +++ b/tests/TestRender.php @@ -4,7 +4,12 @@ class TestRender { protected $format; - public function __construct($formatclass, $opts, $extra = array()) { + public function __construct( + string $formatclass, + array $opts, + ?array $extra = [], + ?array $indices = [] + ) { foreach ($opts as $k => $v) { $method = "set_$k"; Config::$method($v); @@ -14,6 +19,22 @@ public function __construct($formatclass, $opts, $extra = array()) { } $classname = __NAMESPACE__ . "\\" . $formatclass; $this->format = new $classname(); + + foreach ($indices as $index) { + $this->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 + ); + } } public function run() { diff --git a/tests/php/constant_links_001.phpt b/tests/php/constant_links_001.phpt new file mode 100644 index 00000000..15604229 --- /dev/null +++ b/tests/php/constant_links_001.phpt @@ -0,0 +1,66 @@ +--TEST-- +Constant links 001 +--FILE-- + true, + "xml_root" => dirname($xml_file), + "xml_file" => $xml_file, + "output_dir" => __DIR__ . "/output/", +); + +$extra = array( + "lang_dir" => __PHDDIR__ . "phpdotnet/phd/data/langs/", + "phpweb_version_filename" => dirname($xml_file) . '/version.xml', + "phpweb_acronym_filename" => dirname($xml_file) . '/acronyms.xml', +); + +$indeces = [ + [ + "docbook_id" => "constant.definitely-exists", + "filename" => "extensionname.constantspage", + ], + [ + "docbook_id" => "vendor-namespace.constants.definitely-exists2", + "filename" => "extensionname2.constantspage2", + ], +]; + +$render = new TestRender($formatclass, $opts, $extra, $indeces); + +if (Index::requireIndexing() && !file_exists($opts["output_dir"])) { + mkdir($opts["output_dir"], 0755); +} + +$render->run(); +?> +--EXPECTF-- +Filename: constant_links.html +Content: + diff --git a/tests/php/data/constant_links.xml b/tests/php/data/constant_links.xml new file mode 100644 index 00000000..8609dab7 --- /dev/null +++ b/tests/php/data/constant_links.xml @@ -0,0 +1,20 @@ + + + +
+ 1. Existing constants + DEFINITELY_EXISTS + + Vendor\Namespace::DEFINITELY_EXISTS2 + +
+ +
+ 2. Nonexistent constants + THIS_DOES_NOT_EXIST + + Vendor\Namespace::THIS_DOES_NOT_EXIST_EITHER + +
+ +