Skip to content
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

Add links to constants #102

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions phpdotnet/phd/Package/Generic/XHTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
),
Expand Down Expand Up @@ -1613,6 +1613,25 @@ public function format_constant($open, $name, $attrs)
}
return "</code></strong>";
}
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 '<a href="' . $link . '">' . $value . '</a>';
}
public function admonition_title($title, $lang)
{
return '<strong class="' .(strtolower($title)). '">' .($this->autogen($title, $lang)). '</strong>';
Expand Down
23 changes: 22 additions & 1 deletion tests/TestRender.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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() {
Expand Down
66 changes: 66 additions & 0 deletions tests/php/constant_links_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
--TEST--
Constant links 001
--FILE--
<?php
namespace phpdotnet\phd;

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

$formatclass = "TestChunkedXHTML";
$xml_file = __DIR__ . "/data/constant_links.xml";

$opts = array(
"index" => 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:
<div id="constant_links" class="chapter">

<div class="section">
<p class="para">%d. Existing constants</p>
<strong><code><a href="extensionname.constantspage.html#constant.definitely-exists">DEFINITELY_EXISTS</a></code></strong>
<p class="para">
<strong><code><a href="extensionname2.constantspage2.html#vendor-namespace.constants.definitely-exists2">Vendor\Namespace::DEFINITELY_EXISTS2</a></code></strong>
</p>
</div>

<div class="section">
<p class="para">%d. Nonexistent constants</p>
<strong><code>THIS_DOES_NOT_EXIST</code></strong>
<p class="para">
<strong><code>Vendor\Namespace::THIS_DOES_NOT_EXIST_EITHER</code></strong>
</p>
</div>

</div>
20 changes: 20 additions & 0 deletions tests/php/data/constant_links.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<chapter xml:id="constant_links">

<section>
<para>1. Existing constants</para>
<constant>DEFINITELY_EXISTS</constant>
<para>
<constant>Vendor\Namespace::DEFINITELY_EXISTS2</constant>
</para>
</section>

<section>
<para>2. Nonexistent constants</para>
<constant>THIS_DOES_NOT_EXIST</constant>
<para>
<constant>Vendor\Namespace::THIS_DOES_NOT_EXIST_EITHER</constant>
</para>
</section>

</chapter>
Loading