feat(variants): document value format per option key in variant jsons
Every variants/<client>.json now carries a "_formats" map: behind each
option key, the form the value should take ("on|off", "hex color, e.g.
#123456", "plain px number, e.g. 15", "off | ./files/logo.png", font
slug, chooser mode + <key>custom, ...), derived from the mother config.xml
by variants/option_formats.php. variants/document_options.php refreshes the
map in every json (--check for staleness); build.php adds a hint when it
auto-adds a NEW option key to a json. _formats is documentation only and
never read by the build or the drift check.
Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
parent
6786adf092
commit
f3d3912c2f
3 changed files with 213 additions and 1 deletions
|
|
@ -63,6 +63,11 @@
|
|||
* Row 246: when the mother config.xml gains NEW option keys, they are added to
|
||||
* the json automatically as "" and a NOTE is printed listing them, so the user
|
||||
* is informed about every new settable key (existing values are never touched).
|
||||
* Row 264: the "_formats" map in the json documents, per option key, in which
|
||||
* FORM the value should be filled in (e.g. "on|off", "hex color, e.g. #123456",
|
||||
* "plain px number, e.g. 15"). It is generated from the mother config.xml by
|
||||
* variants/document_options.php; new auto-added keys get their hint here too.
|
||||
* "_formats" is documentation only and is never read back by the build.
|
||||
*
|
||||
* Output:
|
||||
* ../<variantName>/config.xml (next to this theme, in upload/themes/survey)
|
||||
|
|
@ -140,6 +145,7 @@ foreach ($motherOptionKeys as $k) {
|
|||
}
|
||||
}
|
||||
if (!empty($newKeys)) {
|
||||
require_once __DIR__ . '/option_formats.php';
|
||||
$ordered = [];
|
||||
foreach ($motherOptionKeys as $k) {
|
||||
$ordered[$k] = $definition['options'][$k];
|
||||
|
|
@ -150,8 +156,18 @@ if (!empty($newKeys)) {
|
|||
}
|
||||
}
|
||||
$definition['options'] = $ordered;
|
||||
// row 264: document the expected value format per new key in "_formats"
|
||||
// (documentation only — never read back by the build)
|
||||
if (!isset($definition['_formats']) || !is_array($definition['_formats'])) {
|
||||
$definition['_formats'] = [];
|
||||
}
|
||||
foreach ($xpath->query('//options/*') as $opt) {
|
||||
if ($opt instanceof DOMElement && in_array($opt->nodeName, $newKeys, true)) {
|
||||
$definition['_formats'][$opt->nodeName] = option_format_hint($opt);
|
||||
}
|
||||
}
|
||||
file_put_contents($definitionFile, json_encode($definition, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n");
|
||||
echo "NOTE: " . count($newKeys) . " NEW option key(s) in the mother config.xml were added to $definitionFile as \"\" (inherit default) — review them: " . implode(', ', $newKeys) . "\n";
|
||||
echo "NOTE: " . count($newKeys) . " NEW option key(s) in the mother config.xml were added to $definitionFile as \"\" (inherit default) with a format hint in \"_formats\" — review them: " . implode(', ', $newKeys) . "\n";
|
||||
}
|
||||
|
||||
$motherName = (string) $xpath->query('//metadata/name')->item(0)->textContent;
|
||||
|
|
|
|||
89
variants/document_options.php
Normal file
89
variants/document_options.php
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
/**
|
||||
* Document the variant json files: write/update the "_formats" map that tells,
|
||||
* per option key, in which FORM the value should be filled in — e.g.
|
||||
* "on|off", "hex color, e.g. #123456", "plain px number, e.g. 15".
|
||||
*
|
||||
* The map is derived from the mother config.xml (option type + options list),
|
||||
* so it stays in sync during development: run this after changing config.xml.
|
||||
*
|
||||
* Usage:
|
||||
* php variants/document_options.php [definitions-dir] update _formats in every *.json
|
||||
* php variants/document_options.php [definitions-dir] --check exit 1 when a json is stale
|
||||
*
|
||||
* "_formats" is documentation only: build.php never reads it, and the
|
||||
* unknown-key drift check only looks inside "options", so "_formats" never
|
||||
* breaks a build. Existing "_formats" entries are kept (hints refreshed),
|
||||
* new mother keys are appended, keys gone from the mother are dropped.
|
||||
*/
|
||||
|
||||
if (PHP_SAPI !== 'cli') {
|
||||
fwrite(STDERR, "This script must be run from the command line.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
require __DIR__ . '/option_formats.php';
|
||||
|
||||
$args = array_values(array_filter($argv, fn($a) => $a !== '--check' && $a !== $argv[0]));
|
||||
$check = in_array('--check', $argv, true);
|
||||
$defsDir = $args[0] ?? (getenv('RESPONDAGE_VARIANTS_DIR') ?: __DIR__);
|
||||
$motherConfig = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'config.xml';
|
||||
|
||||
foreach ([$motherConfig] as $f) {
|
||||
if (!is_file($f)) {
|
||||
fwrite(STDERR, "Required file not found: $f\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if (!is_dir($defsDir)) {
|
||||
fwrite(STDERR, "Definitions dir not found: $defsDir\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$doc = new DOMDocument();
|
||||
if (!$doc->load($motherConfig)) {
|
||||
fwrite(STDERR, "Could not parse $motherConfig\n");
|
||||
exit(1);
|
||||
}
|
||||
$formats = option_format_map($doc);
|
||||
|
||||
$jsons = glob(rtrim($defsDir, '/\\') . DIRECTORY_SEPARATOR . '*.json');
|
||||
if (empty($jsons)) {
|
||||
fwrite(STDERR, "No *.json files in $defsDir\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$fail = 0;
|
||||
foreach ($jsons as $jsonFile) {
|
||||
$definition = json_decode(file_get_contents($jsonFile), true);
|
||||
if (!is_array($definition)) {
|
||||
fwrite(STDERR, "Invalid JSON in $jsonFile — skipped\n");
|
||||
$fail++;
|
||||
continue;
|
||||
}
|
||||
// merge: mother order first (hints refreshed), then legacy keys kept at the end
|
||||
$merged = [];
|
||||
foreach ($formats as $k => $hint) {
|
||||
$merged[$k] = $hint;
|
||||
}
|
||||
foreach (($definition['_formats'] ?? []) as $k => $hint) {
|
||||
if (!array_key_exists($k, $merged)) {
|
||||
$merged[$k] = $hint; // legacy key no longer in the mother config
|
||||
}
|
||||
}
|
||||
$definition['_formats'] = $merged;
|
||||
$out = json_encode($definition, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n";
|
||||
$current = str_replace("\r\n", "\n", file_get_contents($jsonFile));
|
||||
if ($check) {
|
||||
if ($current !== $out) {
|
||||
echo "STALE: " . basename($jsonFile) . " — run: php variants/document_options.php\n";
|
||||
$fail++;
|
||||
} else {
|
||||
echo "OK: " . basename($jsonFile) . " (" . count($merged) . " format hints)\n";
|
||||
}
|
||||
continue;
|
||||
}
|
||||
file_put_contents($jsonFile, $out);
|
||||
echo "Wrote _formats in " . basename($jsonFile) . ": " . count($merged) . " hints\n";
|
||||
}
|
||||
exit($fail > 0 ? 1 : 0);
|
||||
94
variants/option_formats.php
Normal file
94
variants/option_formats.php
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
/**
|
||||
* Shared helper: human-readable "what format should this option value have"
|
||||
* hint per option key, derived from the mother config.xml.
|
||||
*
|
||||
* Used by:
|
||||
* - variants/document_options.php — writes/updates the "_formats" map in
|
||||
* every variants/<client>.json (the per-item format documentation);
|
||||
* - variants/build.php — when new option keys are auto-added to a
|
||||
* variant json (row 246), their format hint is added to "_formats" too.
|
||||
*
|
||||
* The hint is documentation only; build.php never reads "_formats".
|
||||
*/
|
||||
|
||||
if (!function_exists('option_format_hint')) {
|
||||
/**
|
||||
* @param DOMElement $opt an <options>/<key> element of the mother config.xml
|
||||
* @return string format hint, e.g. 'on|off', 'hex color, e.g. #123456'
|
||||
*/
|
||||
function option_format_hint(DOMElement $opt): string
|
||||
{
|
||||
$key = $opt->nodeName;
|
||||
$type = $opt->getAttribute('type');
|
||||
$options = $opt->getAttribute('options');
|
||||
$parent = $opt->getAttribute('parent');
|
||||
|
||||
// color-chooser pair: <key> = mode dropdown, <key>custom = hex picker
|
||||
if ($type === 'dropdown' && $options === 'default|defaultbackgroundcolor|color1|color2|color3|other') {
|
||||
return "chooser mode: one of $options — when 'other', put the hex in {$key}custom";
|
||||
}
|
||||
if ($type === 'colorpicker') {
|
||||
// a colorpicker always stores a hex value (the on|off options attr is
|
||||
// only UI chrome); the <key>custom pickers feed the chooser dropdowns
|
||||
$hint = "hex color, e.g. #123456";
|
||||
if (substr($key, -6) === 'custom') {
|
||||
$base = substr($key, 0, -6);
|
||||
$hint .= " (used when $base = 'other')";
|
||||
} elseif ($parent !== '') {
|
||||
$hint .= " (used when $parent = 'other')";
|
||||
}
|
||||
return $hint;
|
||||
}
|
||||
if ($type === 'buttons') {
|
||||
return $options !== '' ? "one of: $options" : "on|off";
|
||||
}
|
||||
if ($type === 'checkbox') {
|
||||
return "on|off (checkbox)";
|
||||
}
|
||||
if ($type === 'fontselect') {
|
||||
return "font value slug or 'base' (= same as base font), e.g. \"inter\"";
|
||||
}
|
||||
if ($type === 'dropdown') {
|
||||
if ($key === 'font') {
|
||||
return "font value slug, e.g. \"inter\" (dropdown optgroups; a variant's default = its first fontOptions value)";
|
||||
}
|
||||
return $options !== '' ? "one of: $options" : "dropdown value";
|
||||
}
|
||||
if ($type === 'imagefile') {
|
||||
return "off | theme-relative image path, e.g. ./files/logo.png (gif|ico|jpg|jpeg|png only — SVG is rejected)";
|
||||
}
|
||||
if ($type === 'text') {
|
||||
if (preg_match('/margin|height|radius/i', $key)) {
|
||||
return "plain px number, e.g. 15 (margins 0-100)";
|
||||
}
|
||||
if (preg_match('/distance/i', $key)) {
|
||||
return "length, e.g. 20px or 1.5rem";
|
||||
}
|
||||
if ($key === 'questionborderstyle') {
|
||||
return "CSS border-style keyword, e.g. solid|dashed|dotted|none";
|
||||
}
|
||||
return "free text";
|
||||
}
|
||||
if ($type === 'duration') {
|
||||
return "duration in seconds, e.g. 300";
|
||||
}
|
||||
return "value of type '$type'";
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the full key => hint map from a loaded mother config.xml DOMDocument.
|
||||
*
|
||||
* @return array<string,string> in mother config order
|
||||
*/
|
||||
function option_format_map(DOMDocument $doc): array
|
||||
{
|
||||
$map = [];
|
||||
foreach ((new DOMXPath($doc))->query('//options/*') as $opt) {
|
||||
if ($opt instanceof DOMElement) {
|
||||
$map[$opt->nodeName] = option_format_hint($opt);
|
||||
}
|
||||
}
|
||||
return $map;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue