phrep can have a "config" file.
- It's not created automatically.
- Meant to be a plain PHP include() script.
Probed locations:
~/.config/phrep/config.php
- alternatively
$XDG_CONFIG_HOME
- and any overridden --config subpath.
Purpose:
- Can internally predefine shorter callback/utility functions for complex macros.
- Autoload some more dependencies (PHP-Parser) or further config includes.
- Inject constant or
#macro
definitions directly.
Usage:
- It receives a
$proc
(MacroProcessor) instance and an$opts
(getopt/ShellFlags) holder. - So it could override/inject flags per
$opts->force = 1;
simply. - Or preset processor defines, flags, or options per
$proc->pragma["omit"] = ...;
directly.
Example .config/phrep/config.php
Primary purpose of the config file is defining macro helper functions, which is easier for development than multi-line macro #define statements.
Set a single flag, and define a MACRO@ generator.
<?php # api: phrep # type: config namespace macro; $opts->force = 1; // custom macro callback for raw HTML output with escaped vars $proc->defines["HTML@"] = [['raw'], 'return macro\\HTML($token);']; function HTML($token) { $code = []; $html = ""; foreach ($token[0] as $t) { if ($t[0] === T_VARIABLE) { $code[] = var_export($html, 1); $html = ""; $code[] = "htmlspecialchars({$t[1]}, ENT_QUOTES)"; } else { $html .= is_array($t) ? $t[1] : $t; } } $code[] = var_export($html, 1); return join(" . ", $code); }
Which allows a terse
HTML@
macro definition like:print HTML@( <a href="$link"> $title </a> );
That expands during preprocessing to:
print " <a href=" . htmlspecialchars($link, ENT_QUOTES) . "> " . htmlspecialchars($title, ENT_QUOTES) . " </a> ";
See examples/config/ for a more correct version, btw. And of course this very task is less suited for preprocessing, and better left to a proper PHP templating engine anyway.
There's eventually going to be a convenience method $proc->addMacro()
for
this. And anonymous functions may be bound once the internal $proc->defines
structure is settled (or complicated).
Registering constants/macros is easier btw. per $proc->d_define("NAME(x)=2*x")
anyway.
To utilize a distributed config script, use phrep --config ./defines.php
for example. Albeit it's likely more practical to prepare them into plain
include (e.g. .ph
) files.
Further .config/phrep/*.php
includes
The default config include just loads further *.php scripts alongside itself.
So just drop other examples/ into ~/.config/phrep/
.