# 2022-01-22 Initial version
*/
# insert into HTML
metadata element
\SDV($HTMLStylesFmt[MESSAGESID],
'.messages {font-family: monospace;}'
. '.messagesdebug {font-size: smaller; font-family: monospace;}');
# set debug flag
\SDV($MessagesReplacementDebug, false); # set default debug setting if not defined in a configuration file
$Messages_debugon = (bool) $MessagesReplacementDebug; # if on writes input and output to web page
# Version date
$RecipeInfo[MESSAGESNAME]['Version'] = '2022-01-22' . MESSAGESNEW; # 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']);
# declare $Messages for (:if enabled MessagesReplacement:) recipe installation check
$MessagesReplacement = true; # enabled
#
// Initialise constants
DEFINE ('NL', "\n");
DEFINE ('BR', '
' . NL);
## Add a PmWiki custom markup
# (:messages optional,name,list :)
$markup_pattern = '/\\(:messages(?: (.*?))?:\\)/i';
# see https://regex101.com/r/HNkxC5/1
\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; # import variables
global $MessagesFmt;
//extract ($GLOBALS['MarkupToHTML']); # doesn't appear to be required
$retval = '<:block>'; # start a block-level element, i.e. break out of the paragraphs.
if (empty ($MessagesFmt)) {
if ($Messages_debugon) {
return 'No messages'; # no messages
} else {
return ''; # no messages
}
}
if(count($m) == 1) { # no parameters supplied
$msgkeys = ['*']; # wildcard to display all messages
} else {
$msgkeys = strtolower (preg_replace('/\s+/', ',', $m[1])); # parameters case insensitive
}
//if ($Messages_debugon) $retval .= 'msgkeys: ' . $msgkeys . BR;
$arr = []; # store messages by key
foreach ($MessagesFmt as $mk=>$val) {
$mk = strtolower ($mk); # treat keys as case insensitive
if (is_integer($mk)) $mk = '';
if (!isset($arr[$mk])) $arr[$mk] = [];
foreach((array)$val as $vv) {
$arr[$mk][] = $vv;
}
}
//if ($Messages_debugon) $retval .= 'MF keys: ' . implode (', ', array_keys($arr)) . BR;
$out = '';
$foundkeys = \MatchNames (array_keys($arr), $msgkeys); # PmWiki function
//if ($Messages_debugon) $retval .= 'foundkeys: ' . implode (', ', $foundkeys) . BR;
foreach ($foundkeys as $fk) {
$out .= implode('', (array)$arr[$fk]);
}
$retval .= \FmtPageName($out, $pagename); # convert $ constants
return Keep ($retval . '');
} # end MessagesReplacement_Parse
##
// Debug function
function dmsg(string $dmsgprefix, $dmsgtxt) {
# see https://www.pmwiki.org/wiki/Cookbook/DebuggingForCookbookAuthors
# add debug text to message array
global $MessagesFmt, $Messages_debugon;
$MessageFmt = '' . $dmsgprefix . ': ';
switch (true) {
case (is_null($dmsgtxt)) :
$MessageFmt .= 'null';
break;
case (is_bool($dmsgtxt)) :
$MessageFmt .= var_export($dmsgtxt,true);
break;
case (is_array($dmsgtxt)) :
$MessageFmt .= '' . print_r($dmsgtxt, true) . '
' . NL;
break;
case (is_string ($dmsgtxt)):
$MessageFmt .= "'" . htmlspecialchars ($dmsgtxt) . "'";
break;
default:
$MessageFmt .= '"' . htmlspecialchars ($dmsgtxt) . '"';
break;
}
$MessageFmt .= '' . BR;
$MessagesFmt [MESSAGESNAME] .= $MessageFmt;
} # end dmsg