diff --git a/lang/en/lang.php b/lang/en/lang.php index 45987b4..0597357 100644 --- a/lang/en/lang.php +++ b/lang/en/lang.php @@ -6,4 +6,4 @@ */ $lang['error'] = "ERROR in function"; $lang['no_such_function'] = "there is no such function!"; -$lang['not_enough_params'] = "poorly constructed, needs at least 2 parameters!"; \ No newline at end of file +$lang['not_enough_params'] = "not enough parameters were provided!"; \ No newline at end of file diff --git a/lang/pt-br/lang.php b/lang/pt-br/lang.php index 2d15f97..dff59c1 100644 --- a/lang/pt-br/lang.php +++ b/lang/pt-br/lang.php @@ -6,4 +6,4 @@ */ $lang['error'] = "ERRO na função"; $lang['no_such_function'] = "a função não existe!"; -$lang['not_enough_params'] = "mal construída, precisa de pelo menos 2 parâmetros."; \ No newline at end of file +$lang['not_enough_params'] = "não foram fornecidos parâmetros suficientes!"; \ No newline at end of file diff --git a/syntax.php b/syntax.php index 93c0cfa..2069ec6 100644 --- a/syntax.php +++ b/syntax.php @@ -42,7 +42,7 @@ public function getSort() /* READ: https://www.dokuwiki.org/devel:parser:getsort_list * Don't understand exactly what it does, need more study. * - * Should go after WST plugin, to be able to render {{{1}}}. + * Should go after Templater and WST plugin, to be able to render @1@ and {{{1}}}. */ return 320; } @@ -54,7 +54,7 @@ public function connectTo($mode) * Regex accepts any alphabetical function name * but not nested functions */ - $this->Lexer->addSpecialPattern('\{\{#[A-Za-z]+:[^(#\}\})]+#\}\}', $mode, 'plugin_parserfunctions'); + $this->Lexer->addSpecialPattern('\{\{#[[:alpha:]]+:[^(\{\{#)(#\}\})]+#\}\}', $mode, 'plugin_parserfunctions'); // $this->Lexer->addEntryPattern('', $mode, 'plugin_parserfunctions'); } @@ -92,22 +92,21 @@ public function handle($match, $state, $pos, Doku_Handler $handler) return false; } - // Function name: "{{#if" - $func_name = preg_split('/:/', $match); - /* Function name: "if" - * strtolower converts only ASCII - * PhpString::strtolower supports UTF-8, added by "use dokuwiki\Utf8\PhpString;" at line 15 - * The function names will probably only use ASCII characters, but it's a precaution. + /* Function name: "if", "ifeq", "ifexpr" etc. + * strtolower converts only ASCII; PhpString::strtolower supports UTF-8, + * added by "use dokuwiki\Utf8\PhpString;" at line 15. The function + * names will probably only use ASCII characters, but it's a precaution. */ - $func_name = PhpString::strtolower(substr($func_name[0], 3)); + $func_name = preg_replace('/\{\{#([[:alpha:]]+):.*#\}\}/', '\1', $match); + $func_name = PhpString::strtolower($func_name); - // Delete delimiters "{{#if:" and "#}}". - $parts = substr($match, 6, -3); + // Delete delimiters "{{#functionname:" and "#}}". + $parts = preg_replace('/\{\{#[[:alpha:]]+:(.*)#\}\}/', '\1', $match); // Create list with all parameters splited by "|" pipe - // Could use "preg_split('/\|/', $parts)" too + // Could use "preg_split('/\|/', $parts)" instead $params = explode('|', $parts); - + // Stripping whitespace from the beginning and end of strings foreach ($params as &$value) { $value = trim($value); @@ -120,6 +119,9 @@ public function handle($match, $state, $pos, Doku_Handler $handler) case 'if': $func_result = $this->_IF($params, $func_name); break; + case 'ifeq': + $func_result = $this->_IFEQ($params, $func_name); + break; default: $func_result = ' ' . $this->getLang('error') . ' '. $func_name . ': ' . @@ -167,26 +169,50 @@ public function render($mode, Doku_Renderer $renderer, $data) /** * ========== #IF - * {{#if: first parameter | second parameter | third optional parameter }} - * {{#if: test string | value if test string is not empty | value if test string is empty (or only white space) }} + * {{#if: 1st parameter | 2nd parameter | 3rd optional parameter #}} + * {{#if: test string | value if test string is not empty | value if test string is empty (or only white space) #}} */ function _IF($params, $func_name) { - if ( !isset($params[1]) ) { + if ( count($params) < 2 ) { $result = ' ' . $this->getLang('error') . ' '. $func_name . ': ' . $this->getLang('not_enough_params') . ' '; } else { if ( !empty($params[0]) ) { $result = $params[1]; - } else if ( !empty($params[2]) ) { + } else { + if ( !empty($params[2]) ) { + $result = $params[2]; + } else { + /** + * The last parameter (false) must have been intentionally omitted: + * user wants the result to be null if the test string is empty. + */ + $result = null; + } + } + } + + 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, $func_name) + { + if ( count($params) < 4 ) { + $result = ' ' . $this->getLang('error') . + ' '. $func_name . ': ' . $this->getLang('not_enough_params') . + ' '; + } else { + if ( $params[0] == $params[1] ) { $result = $params[2]; } else { - /** - * The last parameter (false) must have been intentionally omitted: - * user wants the result to be null if the test string is empty. - */ - $result = null; + $result = $params[3]; } }