Skip to content

Commit

Permalink
Add PI processing support for common phrases
Browse files Browse the repository at this point in the history
  • Loading branch information
Girgias committed Oct 29, 2024
1 parent 6e94db8 commit 1aa6ef1
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
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') {
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') {
continue;
}

var_dump($pi->target, $pi->data);
$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(
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);
}

0 comments on commit 1aa6ef1

Please sign in to comment.