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 PI processing support for common phrases #173

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,34 @@ function getFileModificationHistory(): array {
}
}

echo "Process PI nodes...\n";
if (!$ac['LANG'] == 'en') {
Girgias marked this conversation as resolved.
Show resolved Hide resolved
if (file_exists($srcdir . '/pi_processing/langs/' . $ac['LANG'] . '.php')) {
require_once $srcdir . '/pi_processing/langs/' . $ac['LANG'] . '.php';
}
}
require_once $srcdir . '/pi_processing/langs/en.php';
require_once $srcdir . '/pi_processing/processors.php';
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//processing-instruction()') as $pi) {
if (!str_starts_with($pi->target, 'phpdoc')) {
continue;
}
// Don't care about generic phpdoc PI target
if ($pi->target === 'phpdoc') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplication with str_starts_with ?

Copy link
Member Author

@Girgias Girgias Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because we have a bunch of PI instruction that are just phpdoc and those that will generate documentation node have more complicated names, so the target is effectively the instruction and the data are values to pass to the corresponding function.

Maybe this design is crap, but I didn't want to figure out how to parse the function to call

continue;
}

var_dump($pi->target, $pi->data);
Girgias marked this conversation as resolved.
Show resolved Hide resolved
$fn = match ($pi->target) {
'phpdoc_error_ValueError_between' => error_section_value_error_between(...),
'phpdoc_error_ValueError_between_changelog' => error_section_value_error_between_changelog(...),
};
$data = explode(' ', $pi->data);
$node = $fn($dom, ...$data);
$pi->parentNode->insertBefore($node, $pi);
}

echo "Validating {$ac["INPUT_FILENAME"]}... ";
flush();
if ($ac['PARTIAL'] != '' && $ac['PARTIAL'] != 'no') { // {{{
Expand Down
25 changes: 25 additions & 0 deletions pi_processing/langs/en.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

if (!defined('VALUE_ERROR_BETWEEN_ERROR_SECTION')) {
define('VALUE_ERROR_BETWEEN_ERROR_SECTION', <<<'CONTENT'
<simpara xmlns="http://docbook.org/ns/docbook">
Throws a <exceptionname>ValueError</exceptionname> if <parameter>PARAMETER_NAME</parameter>
is less than MIN_TAG or greater than MAX_TAG.
</simpara>
CONTENT
);
}

if (!defined('VALUE_ERROR_BETWEEN_CHANGELOG')) {
define('VALUE_ERROR_BETWEEN_CHANGELOG', <<<'CONTENT'
<row xmlns="http://docbook.org/ns/docbook">
<entry>VERSION</entry>
<entry>
A <exceptionname>ValueError</exceptionname> is now thrown if
<parameter>PARAMETER_NAME</parameter> is less than MIN_TAG
or greater than MAX_TAG
</entry>
</row>
CONTENT
);
}
18 changes: 18 additions & 0 deletions pi_processing/langs/fr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

const VALUE_ERROR_BETWEEN_ERROR_SECTION = <<<'CONTENT'
<simpara>
Lance une <exceptionname>ValueError</exceptionname> si <parameter>PARAMETER_NAME</parameter>
est moins que MIN_TAG ou plus grand que MAX_TAG.
</simpara>
CONTENT;

const VALUE_ERROR_BETWEEN_CHANGELOG = <<<'CONTENT'
<row>
<entry>VERSION</entry>
<entry>
Une <exceptionname>ValueError</exceptionname> est désormais lancé si
<parameter>PARAMETER_NAME</parameter> est moins que MIN_TAG ou plus grand que MAX_TAG
</entry>
</row>
CONTENT;
46 changes: 46 additions & 0 deletions pi_processing/processors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

function error_section_value_error_between(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some duplication between these functions which would be nice to somehow get rid of; at least it should be possible to move the import to the caller. Note that there's also an appendXML method you can use on DOMElement.

DOMDocument $dom,
string $parameter,
string $min,
string $max
): DOMElement {
$min_tag_name = is_numeric($min) ? 'literal' : 'constant';
$max_tag_name = is_numeric($max) ? 'literal' : 'constant';

$min_tag = '<' . $min_tag_name . '>' . $min . '</' . $min_tag_name . '>';
$max_tag = '<' . $max_tag_name . '>' . $max . '</' . $max_tag_name . '>';

$xml = str_replace(
['PARAMETER_NAME', 'MIN_TAG', 'MAX_TAG'],
[$parameter, $min_tag, $max_tag],
VALUE_ERROR_BETWEEN_ERROR_SECTION,
);
$localDom = new DOMDocument();
$localDom->loadXML($xml);
return $dom->importNode($localDom->documentElement, true);
}

function error_section_value_error_between_changelog(
DOMDocument $dom,
string $version,
string $parameter,
string $min,
string $max
): DOMElement {
$min_tag_name = is_numeric($min) ? 'literal' : 'constant';
$max_tag_name = is_numeric($max) ? 'literal' : 'constant';

$min_tag = '<' . $min_tag_name . '>' . $min . '</' . $min_tag_name . '>';
$max_tag = '<' . $max_tag_name . '>' . $max . '</' . $max_tag_name . '>';

$xml = str_replace(
['VERSION', 'PARAMETER_NAME', 'MIN_TAG', 'MAX_TAG'],
[$version, $parameter, $min_tag, $max_tag],
VALUE_ERROR_BETWEEN_CHANGELOG,
);
$localDom = new DOMDocument();
$localDom->loadXML($xml);
return $dom->importNode($localDom->documentElement, true);
}
Loading