# 2024-05-05 add key= parameter to maintain compatibility with built in directive # 2024-04-27 update for PHP 8 warnings, add details=show||hide parameter # 2023-12-31 use mb_strtolower, refactor debug message out # 2022-02-02 Separate groups of messages, enable headings, additional simple displays # 2022-01-22 Initial version */ // Initialise constants const NL = "\n"; const BR = '
' . NL; # $localDmsg = false; if (!function_exists('dmsg')) { # if (!isset ($MessagesFmt[__NAMESPACE__])) $MessagesFmt[__NAMESPACE__] = []; # initialise $localDmsg = true; function dmsg (string $smsgprefix, $smsgdata) { # local instance global $MessagesFmt; $MessagesFmt[__NAMESPACE__] [] = '' . $smsgprefix . '= ' . (is_array ($smsgdata) ? @implode (BR . '' . $smsgprefix . '= ', \PHSC ($smsgdata)) : \PHSC ($smsgdata)); } # end function } # end if # insert into HTML metadata element \SDV($HTMLStylesFmt[__NAMESPACE__], NL . '.dmsgprefix {display:inline-block; font-style:italic; min-width:9rem;}' . NL . '.dmsgpre {font-size:smaller;}' . NL . '.messages {font-family: monospace; font-size:small;}' . NL); # set debug flag \SDV($MessagesReplacementDebug, false); # set default debug setting if not defined in a configuration file \SDV($MessagesReplacementHeading, false); # set headings \SDV($MessagesReplacementDetails, ''); # set details display (show or hide) $Messages_debugon = boolval ($MessagesReplacementDebug); # if on writes input and output to $MessagesFmt $Messages_heading = boolval ($MessagesReplacementHeading); # if on writes headings level 3 to web page $MessagesReplacementDetails = mb_strtolower ($MessagesReplacementDetails); $Messages_detailOpts = (('show' === $MessagesReplacementDetails) || ('hide' === $MessagesReplacementDetails)) ? $MessagesReplacementDetails : ''; # Version date $RecipeInfo[MESSAGESNAME]['Version'] = '2024-05-31' . $MessagesReplacementNew; # PmWiki version numbering is done by date # recipe version page variable $FmtPV['$MessagesReplacementVersion'] = "'" . MESSAGESNAME . ' version ' . $RecipeInfo[MESSAGESNAME]['Version'] . "'"; // return version as a custom page variable # if ($Messages_debugon) { dmsg('
' . __FILE__, $RecipeInfo[MESSAGESNAME]['Version']); dmsg ('Messages_detailOpts', $Messages_detailOpts); } # declare $Messages for (:if enabled MessagesReplacement:) recipe installation check $MessagesReplacement = true; # enabled # ## Add a PmWiki custom markup # (:messages optional,name,list :) $markup_pattern = '/\\(:messages(?: (.*?))?:\\)/i'; # see https://regex101.com/r/HNkxC5/2 \Markup('messages', 'directives', $markup_pattern, __NAMESPACE__ . '\MessagesReplacement_Parse' ); # i = case insensitive # uses lazy evaluation, preserves leading and trailing white space // return; # completed messages replacement recipe setup /*-----------------------------------------------------------------------------------------------------------*/ # /** Main Messages parser * /param arguments as documented above * /return The HTML-formatted information wrapped in a
of class "messages". */ function MessagesReplacement_Parse (array $m):string { global $Messages_debugon, $pagename; # import variables global $MessagesFmt; # import data array for reading global $Messages_heading, $Messages_detailOpts; $retVal = '<:block>
' . NL; # start a block-level element, i.e. break out of the paragraphs. if (empty ($MessagesFmt)) { return ($Messages_debugon) ? 'No messages' . BR : ''; # no messages } if ($Messages_debugon) { $retVal .= '
' . __NAMESPACE__ . '' . NL; $retVal .= 'm[]: ' . (is_array ($m) ? "'" . implode ("' '", $m) . "'" : '"' . $m . '"') . BR; } # end debug $detailOpts = $Messages_detailOpts; # initialise $args[''] = ['*']; # initialise, defualt is all messages $msgKeys = ''; # initialise if (isset($m[1])) { # parameters supplied $args = \ParseArgs($m[1]); # contains all text within directive if (!empty($args ['keys'])) { # some keys were specified $msgKeys .= mb_strtolower ($args['keys']); # parameters case insensitive if ($Messages_debugon) { $retVal .= "args['keys']: " . (is_array ($args['keys']) ? "'" . implode ("' '", $args['keys']) . "'" : '"' . $args['keys'] . '"') . BR; } } if (!empty($args [''])) { # some keys were specified $msgKeys .= mb_strtolower (implode (',', $args[''])); # parameters case insensitive if ($Messages_debugon) { $retVal .= "args['']: " . (is_array ($args['']) ? "'" . implode ("' '", $args['']) . "'" : '"' . $args[''] . '"') . BR; } } if (array_key_exists ('details', $args)) { $detailOpts = mb_strtolower ($args ['details']); } $debugOpt = array_key_exists ('debug', $args) ? $args['debug'] === 'true' : false; if ($debugOpt) $Messages_debugon = true; # set on } $displayDetail = ('show' === $detailOpts) || ('hide' === $detailOpts); if ($Messages_debugon) { $retVal .= 'detailOpts: ' . (is_array ($detailOpts) ? "'" . implode ("' '", $detailOpts) . "'" : '"' . $detailOpts . '"') . BR; $retVal .= 'displayDetail: "' . var_export($displayDetail, true) . '"' . BR; } # end debug if (empty ($msgKeys)) $msgKeys = '*'; # display all keys if ($Messages_debugon) { $retVal .= 'msgKeys: ' . (is_array ($msgKeys) ? "'" . implode ("' '", $msgKeys) . "'" : '"' . $msgKeys . '"') . BR; } $outArr = []; # store messages by key foreach ($MessagesFmt as $key=>$val) { # set up a case insensitive key, numeric keys are coalesced to the key '' $mky = $key; if (is_integer($key)) $mky = ''; # coalesce numeric keys if (is_string($key)) $mky = mb_strtolower ($key); # treat keys as case insensitive if (!isset($outArr[$mky])) $outArr[$mky] = []; # initialise # extract all the values for this key to the output array foreach((array)$val as $vvl) { $outArr[$mky][] = $vvl; } # end foreach } # end foreach if ($Messages_debugon) { $retVal .= 'MsgF keys: ' . implode (', ', array_keys($outArr)) . BR; } $out = ''; $foundkeys = \MatchNames (array_keys($outArr), $msgKeys); # PmWiki function if ($Messages_debugon) { $retVal .= 'foundkeys: ' . implode (', ', $foundkeys) . BR; $retVal .= '
' . NL; } foreach ($foundkeys as $fky) { if ($displayDetail) { $out .= ''; } if ($Messages_heading) { $out .= '

' . $fky . '

'; } $out .= implode(BR, $outArr[$fky]) . BR; if ($displayDetail) { $out .= '' . NL; } } # end foreach $retVal .= \FmtPageName($out, $pagename); # convert $ constants return Keep ($retVal . NL . '
' . NL); } # end MessagesReplacement_Parse