From 2eafe88dc65fdae7c45bd0fba6b3e159dff765f8 Mon Sep 17 00:00:00 2001
From: nerun <3987342+nerun@users.noreply.github.com>
Date: Thu, 24 Sep 2026 17:48:49 +0000
Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Automatic=20code=20style=20fixes?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
action.php | 11 ++----
helper.php | 97 ++++++++++++++++++++++++++-----------------------
syntax.php | 104 ++++++++++++++++++++++++++---------------------------
3 files changed, 107 insertions(+), 105 deletions(-)
diff --git a/action.php b/action.php
index 9cdd7e3..b8d2a5c 100644
--- a/action.php
+++ b/action.php
@@ -1,4 +1,5 @@
helper = plugin_load('helper', 'parserfunctions');
- }
-
/** @inheritDoc */
public function register(EventHandler $controller)
{
@@ -37,6 +30,7 @@ public function handlePreprocess(Event $event)
{
$text = $event->data;
$text = $this->processParserFunctions($text);
+
$event->data = $text;
}
@@ -93,4 +87,3 @@ private function extractBalancedFunction($text)
return false; // Malformed
}
}
-
diff --git a/helper.php b/helper.php
index 9d8e247..8e9c1c6 100644
--- a/helper.php
+++ b/helper.php
@@ -1,16 +1,19 @@
- * @created Tue, 01 jul 2025 15:06:42 -0300
+ * @created Tue, 01 jul 2025 15:06:42 -0300
*/
if (!defined('DOKU_INC')) die();
-class helper_plugin_parserfunctions extends DokuWiki_Plugin {
-
+class helper_plugin_parserfunctions extends Plugin
+{
/**
* Processes raw function input into normalized parameters, handling pipe escapes
*
@@ -41,22 +44,21 @@ class helper_plugin_parserfunctions extends DokuWiki_Plugin {
* @note Empty strings are valid parameters (unlike array_filter)
* @note Trims only outer whitespace (inner spaces remain)
*/
- public function parseParameters($input) {
+ public function parseParameters($input)
+ {
// 1) Replace escaped pipes with temporary marker
$input = str_replace('%%|%%', '%%TEMP_PIPE%%', $input);
-
+
// 2) Split by unescaped pipes
$params = explode('|', $input);
-
+
// 3) Restore escaped pipes
- $params = array_map(function($param) {
- return str_replace('%%TEMP_PIPE%%', '%%|%%', $param);
- }, $params);
+ $params = array_map(fn($param) => str_replace('%%TEMP_PIPE%%', '%%|%%', $param), $params);
// 4) Remove whitespace
- return array_map('trim', $params);
+ return array_map(trim(...), $params);
}
-
+
/**
* Parses parameters for a SWITCH parser function and structures them for evaluation
*
@@ -96,17 +98,18 @@ public function parseParameters($input) {
* @note All case keys and test values are trimmed of whitespace
* @note Empty strings are valid as both test values and case values
*/
- public function parseSwitchCases($params) {
+ public function parseSwitchCases($params)
+ {
$cases = [];
$default = null;
$testString = null;
$lastValue = null;
-
+
foreach ($params as $param) {
$param = str_replace('%%=%%', '%%TEMP_EQUAL%%', $param);
$parts = explode('=', $param, 2);
- $parts = array_map('trim', $parts);
-
+ $parts = array_map(trim(...), $parts);
+
if (count($parts) === 2) {
// Case with explicit value (case = value)
$parts[1] = str_replace('%%TEMP_EQUAL%%', '%%=%%', $parts[1]);
@@ -115,7 +118,7 @@ public function parseSwitchCases($params) {
} else {
// Case without explicit value (fallthrough or default)
$parts[0] = str_replace('%%TEMP_EQUAL%%', '%%=%%', $parts[0]);
-
+
if ($testString === null) {
$testString = trim($parts[0]); // First parameter is the test value
} elseif (trim($parts[0]) === '#default') {
@@ -125,14 +128,14 @@ public function parseSwitchCases($params) {
}
}
}
-
+
return [
'cases' => $cases,
'test' => $testString,
'default' => $default ?? $lastValue // Implicit default is the last value
];
}
-
+
/**
* Checks for the existence of a folder (namespace) or a file (media or page)
*
@@ -144,17 +147,18 @@ public function parseSwitchCases($params) {
* @param string $target The identifier or path to check
* @return bool True if it exists (file, page, media, or namespace), false otherwise
*/
- public function checkExistence($target) {
+ public function checkExistence($target)
+ {
// Normalize spaces around ':', transform "wiki : help" → "wiki:help"
$target = preg_replace('/\s*:\s*/', ':', $target);
-
+
// If it is a real absolute or relative path, test as file or folder
if (file_exists($target)) {
return true;
}
-
+
// If path started with '/', try as relative to DOKU_INC by removing '/'
- if (strlen($target) > 0 && $target[0] === '/') {
+ if ((string) $target !== '' && $target[0] === '/') {
$relativePath = ltrim($target, '/');
$fullPath = DOKU_INC . $relativePath;
if (file_exists($fullPath)) {
@@ -181,13 +185,13 @@ public function checkExistence($target) {
return false;
}
-
+
/**
* Escape sequence handling (for backwards compatibility)
*
* To add more escapes, please refer to:
* https://www.freeformatter.com/html-entities.html
- *
+ *
* Before 2025-01-18, escape sequences had to use "&#NUMBER;" instead of
* "NUMBER;", because "#" was not escaped.
*
@@ -199,9 +203,10 @@ public function checkExistence($target) {
* escaped only by wrapping them in '%%', following the standard DokuWiki
* syntax. So, the escapes below are DEPRECATED, but kept for backwards
* compatibility.
- *
+ *
*/
- public function processEscapes($text) {
+ public function processEscapes($text)
+ {
// DEPRECATED, but kept for backwards compatibility:
$escapes = [
"&#61;" => "=",
@@ -210,39 +215,43 @@ public function processEscapes($text) {
"&#125;" => "%%}%%",
"#" => "#" // Always leave this as the last element!
];
-
+
foreach ($escapes as $key => $value) {
$text = str_replace($key, $value, $text);
}
-
+
return $text;
}
-
+
/**
* Format error messages consistently
*/
- public function formatError($type, $function, $messageKey) {
+ public function formatError($type, $function, $messageKey)
+ {
$wrapPluginExists = file_exists(DOKU_INC . 'lib/plugins/wrap');
-
+
$errorMsg = '**' . $this->getLang('error') . ' ' . $function . ': '
. $this->getLang($messageKey) . '**';
-
+
if ($wrapPluginExists) {
return "
' and the last '
' - if (substr($data, 0, 3) === '' && substr($data, -4) === '
') { + $data = $renderer->render_text($data, 'xhtml'); + // Remove the first '' and the last '
' + if (str_starts_with($data, '') && str_ends_with($data, '
')) { $data = substr($data, 3, -4); } - $renderer->doc .= $data; + $renderer->doc .= $data; return true; } - + /** * ========== #IF * {{#if: 1st parameter | 2nd parameter | 3rd parameter #}} * {{#if: test string | value if test string is not empty | value if test * string is empty (or only white space) #}} */ - function _IF($params, $funcName) + public function _IF($params, $funcName) { - if ( count($params) < 1 ) { + if (count($params) < 1) { $result = $this->helper->formatError('alert', $funcName, 'not_enough_params'); } else { - if ( !empty($params[0]) ) { + if (!empty($params[0])) { $result = $params[1] ?? ''; } else { $result = $params[2] ?? ''; } } - + return $result; } - + /** * ========== #IFEQ * {{#ifeq: 1st parameter | 2nd parameter | 3rd parameter | 4th parameter #}} * {{#ifeq: string 1 | string 2 | value if identical | value if different #}} */ - function _IFEQ($params, $funcName) + public function _IFEQ($params, $funcName) { - if ( count($params) < 2 ) { + if (count($params) < 2) { $result = $this->helper->formatError('alert', $funcName, 'not_enough_params'); } else { - if ( $params[0] == $params[1] ) { + if ($params[0] == $params[1]) { $result = $params[2] ?? ''; } else { $result = $params[3] ?? ''; } } - + return $result; } - + /** * ======= #IFEXIST * Syntax: {{#ifexist: target | if-true | if-false #}} @@ -272,24 +271,24 @@ function _IFEQ($params, $funcName) * @param string $funcName Name of the parser function (for error messages) * @return string Rendered output based on existence check */ - function _IFEXIST($params, $funcName) + public function _IFEXIST($params, $funcName) { if (count($params) < 1) { return $this->helper->formatError('alert', $funcName, 'not_enough_params'); } - + $target = trim($params[0]); if ($target === '') { return $this->helper->formatError('alert', $funcName, 'empty_test_parameter'); } - + $exists = $this->helper->checkExistence($target); - + return $exists ? ($params[1] ?? '') : ($params[2] ?? ''); } - + /** * ========== #SWITCH * {{#switch: comparison string @@ -299,18 +298,19 @@ function _IFEXIST($params, $funcName) * | default result * #}} */ - function _SWITCH($params, $funcName) { + public function _SWITCH($params, $funcName) + { if (count($params) < 2) { return $this->helper->formatError('alert', $funcName, 'not_enough_params'); } $parsed = $this->helper->parseSwitchCases($params); - + // Checks if the test string exists as a key in the switch cases array if (array_key_exists($parsed['test'], $parsed['cases'])) { return $parsed['cases'][$parsed['test']]; // ← May return empty string } - + // Returns the default (explicit or implicit) only if the case does not exist return $parsed['default'] ?? ''; } @@ -320,7 +320,8 @@ function _SWITCH($params, $funcName) { * This function evaluates a mathematical expression and returns the * calculated value. */ - private function _EXPR($params, $funcName) { + private function _EXPR($params, $funcName) + { if (!isset($params[0])) { return $this->helper->formatError('alert', $funcName, 'empty_test_parameter'); } @@ -328,4 +329,3 @@ private function _EXPR($params, $funcName) { return $this->helper->evaluateMathExpression($params[0]); } } -