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 "$errorMsg"; } - + return $errorMsg; } /** * Evaluates a mathematical expression consistently */ - public function evaluateMathExpression($expr) { + public function evaluateMathExpression($expr) + { $funcName = 'expr'; $expr = trim($expr); // Rejects characters outside the permitted set - if (!preg_match('/^( + if ( + !preg_match( + '/^( \s*| \b(?:and|or|xor|not)\b| # Reserved words first ==|!=|<=|>=|<|>| # Comparisons @@ -250,8 +259,10 @@ public function evaluateMathExpression($expr) { \(|\)| # Parentheses &&|\|\||!| # Symbolic logics [0-9]+(\.[0-9]+)?([eE][\+\-]?[0-9]+)? # Numbers with period and exponent - )+$/ix' - , $expr)) { + )+$/ix', + $expr + ) + ) { return $this->formatError('alert', $funcName, 'invalid_expression'); } @@ -259,19 +270,17 @@ public function evaluateMathExpression($expr) { $expr = preg_replace('/\bnot\b/i', '!', $expr); // Simple evaluation $result = eval('return (' . $expr . ');'); - + if (!is_numeric($result) || is_infinite($result) || is_nan($result)) { if (is_bool($result)) { return $result ? 1 : 0; - } else { - return $this->formatError('alert', $funcName, 'undefined_result'); } + return $this->formatError('alert', $funcName, 'undefined_result'); } - + return $result; - } catch (Throwable $e) { + } catch (Throwable) { return $this->formatError('alert', $funcName, 'evaluation_error'); } } } - diff --git a/syntax.php b/syntax.php index 255c96e..988e0dc 100644 --- a/syntax.php +++ b/syntax.php @@ -1,15 +1,18 @@ * @created Sat, 09 Dec 2023 14:59 -0300 - * + * * This is my first plugin, and I don't even know PHP well, that's why it's full * of comments, but I'll leave it that way so I can consult it in the future. - * + * */ + +use dokuwiki\Parsing\Handler; use dokuwiki\Extension\SyntaxPlugin; use dokuwiki\Utf8\PhpString; @@ -18,7 +21,8 @@ class syntax_plugin_parserfunctions extends SyntaxPlugin /** @var helper_plugin_parserfunctions $helper */ private $helper; - public function __construct() { + public function __construct() + { $this->helper = plugin_load('helper', 'parserfunctions'); } @@ -73,7 +77,7 @@ public function connectTo($mode) public function resolveFunction($text) { // Remove {{# and #}} delimiters if present - if (substr($text, 0, 3) === '{{#' && substr($text, -3) === '#}}') { + if (str_starts_with($text, '{{#') && str_ends_with($text, '#}}')) { $text = substr($text, 3, -3); } @@ -92,20 +96,14 @@ public function resolveFunction($text) $params = $this->helper->parseParameters($paramsText); - switch ($funcName) { - case 'if': - return $this->_IF($params, $funcName); - case 'ifeq': - return $this->_IFEQ($params, $funcName); - case 'ifexist': - return $this->_IFEXIST($params, $funcName); - case 'switch': - return $this->_SWITCH($params, $funcName); - case 'expr': - return $this->_EXPR($params, $funcName); - default: - return $this->helper->formatError('important', $funcName, 'no_such_function'); - } + return match ($funcName) { + 'if' => $this->_IF($params, $funcName), + 'ifeq' => $this->_IFEQ($params, $funcName), + 'ifexist' => $this->_IFEXIST($params, $funcName), + 'switch' => $this->_SWITCH($params, $funcName), + 'expr' => $this->_EXPR($params, $funcName), + default => $this->helper->formatError('important', $funcName, 'no_such_function'), + }; } // @author ChatGPT -- Wed, 02 jul 2025 12:04:42 -0300 @@ -144,10 +142,11 @@ private function resolveNestedFunctions($text) } /** @inheritDoc */ - public function handle($match, $state, $pos, Doku_Handler $handler) { + public function handle($match, $state, $pos, Handler $handler) + { /* This method is only called if the Lexer, in the connectTo() method, * finds a $match. - * + * * READ: https://www.dokuwiki.org/devel:syntax_plugins#handle_method * This is the part of your plugin which should do all the work. Before * DokuWiki renders the wiki page it creates a list of instructions for @@ -158,19 +157,19 @@ public function handle($match, $state, $pos, Doku_Handler $handler) { * Parameters: * * $match (string) — The text matched by the patterns - * + * * $state (int) — The lexer state for the match, representing * the type of pattern which triggered this call * to handle(): DOKU_LEXER_SPECIAL — a pattern * set by addSpecialPattern(). - * + * * $pos (int) — The character position of the matched text. - * + * * $handler — Object Reference to the Doku_Handler object. */ return $this->resolveFunction($match); } - + /** @inheritDoc */ public function render($mode, Doku_Renderer $renderer, $data) { @@ -191,70 +190,70 @@ public function render($mode, Doku_Renderer $renderer, $data) * method. The render() must interpret the instruction and * generate the appropriate output. */ - + if ($mode !== 'xhtml') { return false; } - + if (!$data) { return false; } - + // escape sequences $data = $this->helper->processEscapes($data); // Do not use
because we need inline substitution! - $data = $renderer->render_text($data, 'xhtml'); - // Remove the first '

' 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]); } } -