diff options
| author | Lester Caine <lester@lsces.co.uk> | 2026-05-16 13:40:40 +0100 |
|---|---|---|
| committer | Lester Caine <lester@lsces.co.uk> | 2026-05-16 13:40:40 +0100 |
| commit | f0efd6a9415a6cfccc1c0af5701ac5dddb4221a6 (patch) | |
| tree | ea78679327ef5804b0a4bb85b89d4451123d585a | |
| parent | 9a6b8f5562e0c59587a7e283733ccb2a94b1706b (diff) | |
| download | themes-f0efd6a9415a6cfccc1c0af5701ac5dddb4221a6.tar.gz themes-f0efd6a9415a6cfccc1c0af5701ac5dddb4221a6.tar.bz2 themes-f0efd6a9415a6cfccc1c0af5701ac5dddb4221a6.zip | |
Further improvements to bitweaver smarty extension
| -rwxr-xr-x | includes/classes/BitweaverExtension.php | 210 | ||||
| -rw-r--r-- | smartyplugins/modifier.highlight.php | 77 | ||||
| -rw-r--r-- | smartyplugins/modifier.implode.php | 7 |
3 files changed, 107 insertions, 187 deletions
diff --git a/includes/classes/BitweaverExtension.php b/includes/classes/BitweaverExtension.php index 9344532..3d6ce08 100755 --- a/includes/classes/BitweaverExtension.php +++ b/includes/classes/BitweaverExtension.php @@ -2,7 +2,6 @@ namespace Bitweaver\Themes; -use Bitweaver\KernelTools; use Smarty\Exception; use Smarty\Extension\Base; @@ -70,10 +69,11 @@ class BitweaverExtension extends Base { $basename = basename($file); $functionName = str_replace('modifier.', '', $basename); $functionName = str_replace('.php', '', $functionName); - $this->callbacks[$functionName]['name'] = 'smarty_modifier_' . $functionName; + $callable = 'Bitweaver\\Plugins\\smarty_modifier_' . $functionName; + $this->callbacks[$functionName]['name'] = $callable; $this->callbacks[$functionName]['loaded'] = true; - if ( is_callable('BitWeaver\\Plugins\\smarty_modifier_' . $functionName)) { - $gBitSmarty->registerPlugin ('modifier', $functionName, 'BitWeaver\\Plugins\\smarty_modifier_' . $functionName ); + if ( is_callable( $callable )) { + $gBitSmarty->registerPlugin( 'modifier', $functionName, $callable ); } } } @@ -85,7 +85,6 @@ class BitweaverExtension extends Base { } switch ($modifier) { -// case 'add_link_ticket': $this->modifiers[$modifier] = new \Bitweaver\Plugins\AddLinkTicket(); break; case 'tr': $this->modifiers[$modifier] = new \Bitweaver\Plugins\PreTr(); // case 'bit_short_datetime': $this->modifiers[$modifier] = new \Bitweaver\Plugins\BitShortDatetime(); } @@ -94,32 +93,26 @@ class BitweaverExtension extends Base { } public function getModifierCallback(string $modifierName) { -// if (!isset($this->modifiers[$modifierName])) { - if (!isset($this->callbacks[$modifierName])) { - switch ($modifierName) { - case 'highlight': return [$this, 'smarty_modifier_highlight']; - case 'implode': return [$this, 'smarty_modifier_implode']; - case 'extension_loaded': return [$this, 'smarty_modifier_extension_loaded']; - case 'function_exists': return [$this, 'smarty_modifier_function_exists']; - case 'basename': return [$this, 'smarty_modifier_basename']; - case 'strpos': return [$this, 'smarty_modifier_function_strpos']; - case 'http_build_query': return [$this, 'smarty_modifier_http_build_query']; - case 'ucwords': return [$this, 'smarty_modifier_ucwords']; - case 'is_object': return [$this, 'smarty_modifier_is_object']; - case 'floor': return [$this, 'smarty_modifier_floor']; - case 'is_a': return [$this, 'smarty_modifier_is_a']; - case 'is_file': return [$this, 'smarty_modifier_is_file']; - case 'is_readable': return [$this, 'smarty_modifier_is_readable']; - case 'ucfirst': return [$this, 'smarty_modifier_ucfirst']; - case 'html_entity_decode': return [$this, 'smarty_modifier_html_entity_decode']; - } - } else { - if ( !$this->callbacks[$modifierName]['loaded'] ) { - } - return [$this, $this->callbacks[$modifierName]['name']]; + if (!isset($this->callbacks[$modifierName])) { + switch ($modifierName) { + case 'extension_loaded': return 'extension_loaded'; + case 'function_exists': return 'function_exists'; + case 'basename': return 'basename'; + case 'strpos': return 'strpos'; + case 'http_build_query': return 'http_build_query'; + case 'ucwords': return 'ucwords'; + case 'is_object': return 'is_object'; + case 'floor': return 'floor'; + case 'is_a': return 'is_a'; + case 'is_file': return 'is_file'; + case 'is_readable': return 'is_readable'; + case 'ucfirst': return 'ucfirst'; + case 'html_entity_decode': return 'html_entity_decode'; } - return null; -// } + } else { + return $this->callbacks[$modifierName]['name']; + } + return null; } public function getFunctionHandler(string $functionName): ?\Smarty\FunctionHandler\FunctionHandlerInterface { @@ -158,161 +151,4 @@ class BitweaverExtension extends Base { return $this->blockHandlers[$blockTagName] ?? null; } - /** - * Smarty plugin - * @package Smarty - * @subpackage plugins - */ - public function smarty_modifier_highlight( $source ) { - global $gBitSystem, $gBitSmarty; - // Skip out if nothing to highlight - if ( empty($_REQUEST['highlight']) ) return $source; - - if( $gBitSystem->isFeatureActive( 'themes_output_highlighting' ) ) { - // This array is used to choose colours for supplied highlight terms - $colorArr = [ '#ffffcc', '#ffcccc', '#a0ffff', '#ffccff', '#ccffcc' ]; - - // don't highlight characters that are used as replacements - $find = [ - "!(\s|^)%(\s|$)!", - "!(\s|^)#(\s|$)!", - "!(\s|^)@(\s|$)!", - "!(\s|^):(\s|$)!", - "!(\s|^)&(\s|$)!", - ]; - - $words = trim( preg_replace( $find, "$1$2", urldecode( $_REQUEST['highlight'] ?? '' ))); - if( empty( $words )) { - return $source; - } - - $highlight = $source; - - // extraction patterns and their replacements - $patterns = [ - // scripts - "!<script[^>]+>.*?</script>!is" => "@@@##########:#########%:#########&@@@", - // maketoc - "!<div class=.?maketoc[^>]*>.*?</div>!si" => "@@@##########:#########%:#########@@@@", - // html tags - "'<[\/\!]*?[^<>]*?>'si" => "@@@##########:#########%:#########:@@@", - ]; - - ksort( $patterns ); - - foreach( $patterns as $pattern => $replace ) { - preg_match_all( $pattern, $highlight, $match ); - $matches[$replace] = $match[0]; - $highlight = preg_replace( $pattern, $replace, $highlight ); - } - - // Wrap all the highlight words with a colourful span - $wordArr = []; - $pattern = '#"([^"]*)"#'; - if( preg_match_all( $pattern, $words, $ms ) ) { - $wordArr = $ms[1]; - // remove the words we've just dealt with - $words = preg_replace( $pattern, "", $words ); - } - - $words = preg_replace( "!\s+!", " ", $words ); - if( !empty( $words ) ) { - $wordArr = array_merge( $wordArr, explode( ' ', strip_tags($words) ) ); - } - - $i = 0; - $wordList = KernelTools::tra( "Highlighted words" ).': '; - foreach ( $wordArr as $word ) { - $wordList .= '<span style="font-weight:bold;color:black;background-color:'.$colorArr[$i].';">'.$word.'</span> '; - $highlight = preg_replace( "/(".preg_quote( $word, '/' ).")/si", '<span style="font-weight:bold;color:black;background-color:'.$colorArr[$i++].';">$1</span>', $highlight ); - } - - krsort( $patterns ); - - foreach( $patterns as $pattern ) { - foreach( $matches[$pattern] as $insert ) { - $highlight = preg_replace( "!{$pattern}!", $insert, $highlight, 1 ); - } - } - $source = $highlight; - $wordList = "<div class=\"wordlist\">$wordList</div>"; - $gBitSmarty->assign( 'highlightWordList', $wordList ?? ''); - } - return $source; - } - - /** - * smarty_modifier_bit_short_date - */ - public function smarty_modifier_bit_short_date( $pString ) { - global $gBitSystem; - return \Bitweaver\Plugins\smarty_modifier_bit_date_format( $pString, $gBitSystem->get_short_date_format(), '%d %b %Y' ); - } - - /** - * PHP core functions reenabled in smarty5 templates - * - */ - public function smarty_modifier_implode( $array, $glue = '' ) { - if (!is_array($array)) { - return $array; - } - return join($array, $glue); - } - - public function smarty_modifier_file_exists( $file ) { - return file_exists($file); - } - - public function smarty_modifier_basename( $file ) { - return basename( $file, $suffix = ""); - } - - public function smarty_modifier_extension_loaded( $ext ) { - return extension_loaded( $ext ); - } - - public function smarty_modifier_function_exists( $func ) { - return function_exists($func); - } - - public function smarty_modifier_function_strpos ( $haystack, $needle ) { - return strpos( $haystack, $needle ); - } - - public function smarty_modifier_http_build_query ( $data ) { - return http_build_query( $data ); - } - - public function smarty_modifier_ucwords ( $string ) { - return ucwords( $string ?? '' ); - } - - public function smarty_modifier_is_object ( $string ) { - return is_object( $string ?? '' ); - } - - public function smarty_modifier_floor ( $num ) { - return floor( $num ?? 0 ); - } - - public function smarty_modifier_is_a ( object|string $hash, $class ) { - return is_a( $hash, $class ); - } - - public function smarty_modifier_is_file ( string $file ) { - return is_file( $file ); - } - - public function smarty_modifier_is_readable ( string $file ) { - return is_readable( $file ); - } - - public function smarty_modifier_ucfirst ( string $file ) { - return ucfirst( $file ); - } - - public function smarty_modifier_html_entity_decode ( string $file ) { - return html_entity_decode( $file ); - } } diff --git a/smartyplugins/modifier.highlight.php b/smartyplugins/modifier.highlight.php new file mode 100644 index 0000000..be9c764 --- /dev/null +++ b/smartyplugins/modifier.highlight.php @@ -0,0 +1,77 @@ +<?php + +namespace Bitweaver\Plugins; + +use Bitweaver\KernelTools; + +function smarty_modifier_highlight( $source ) { + global $gBitSystem, $gBitSmarty; + + if ( empty($_REQUEST['highlight']) ) return $source; + + if( !$gBitSystem->isFeatureActive( 'themes_output_highlighting' ) ) { + return $source; + } + + $colorArr = [ '#ffffcc', '#ffcccc', '#a0ffff', '#ffccff', '#ccffcc' ]; + + $find = [ + "!(\s|^)%(\s|$)!", + "!(\s|^)#(\s|$)!", + "!(\s|^)@(\s|$)!", + "!(\s|^):(\s|$)!", + "!(\s|^)&(\s|$)!", + ]; + + $words = trim( preg_replace( $find, "$1$2", urldecode( $_REQUEST['highlight'] ?? '' ))); + if( empty( $words )) { + return $source; + } + + $highlight = $source; + + $patterns = [ + "!<script[^>]+>.*?</script>!is" => "@@@##########:#########%:#########&@@@", + "!<div class=.?maketoc[^>]*>.*?</div>!si" => "@@@##########:#########%:#########@@@@", + "'<[\/\!]*?[^<>]*?>'si" => "@@@##########:#########%:#########:@@@", + ]; + + ksort( $patterns ); + + foreach( $patterns as $pattern => $replace ) { + preg_match_all( $pattern, $highlight, $match ); + $matches[$replace] = $match[0]; + $highlight = preg_replace( $pattern, $replace, $highlight ); + } + + $wordArr = []; + $pattern = '#"([^"]*)"#'; + if( preg_match_all( $pattern, $words, $ms ) ) { + $wordArr = $ms[1]; + $words = preg_replace( $pattern, "", $words ); + } + + $words = preg_replace( "!\s+!", " ", $words ); + if( !empty( $words ) ) { + $wordArr = array_merge( $wordArr, explode( ' ', strip_tags($words) ) ); + } + + $i = 0; + $wordList = KernelTools::tra( "Highlighted words" ).': '; + foreach ( $wordArr as $word ) { + $wordList .= '<span style="font-weight:bold;color:black;background-color:'.$colorArr[$i].';">'.$word.'</span> '; + $highlight = preg_replace( "/(".preg_quote( $word, '/' ).")/si", '<span style="font-weight:bold;color:black;background-color:'.$colorArr[$i++].';">$1</span>', $highlight ); + } + + krsort( $patterns ); + + foreach( $patterns as $pattern ) { + foreach( $matches[$pattern] as $insert ) { + $highlight = preg_replace( "!{$pattern}!", $insert, $highlight, 1 ); + } + } + $source = $highlight; + $gBitSmarty->assign( 'highlightWordList', "<div class=\"wordlist\">$wordList</div>" ); + + return $source; +} diff --git a/smartyplugins/modifier.implode.php b/smartyplugins/modifier.implode.php new file mode 100644 index 0000000..37a60b4 --- /dev/null +++ b/smartyplugins/modifier.implode.php @@ -0,0 +1,7 @@ +<?php + +namespace Bitweaver\Plugins; + +function smarty_modifier_implode( array $array, string $glue = '' ): string { + return implode( $glue, $array ); +} |
