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:
Tammo ter Hark 2026-11-30 16:57:44 +01:00
commit f3d3912c2f
4 changed files with 213 additions and 1 deletions

View file

@ -413,6 +413,19 @@ the variant's `preview.png` is identical to the mother's.
leave the rest empty. Legacy keys (e.g. `logotopmargin`, `<pos>logo`,
hex values in chooser options) are still accepted and migrated by
`build.php`, but writing the new-model keys directly is recommended.
The json also carries a **`_formats` map** that documents, behind each 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"`,
`"off | ./files/logo.png"`, `"font value slug, e.g. \"inter\""`. Keep it up to
date during development with:
```
php variants/document_options.php <definitions-dir> # update _formats in every *.json
php variants/document_options.php <definitions-dir> --check # exit 1 when stale
```
The hints are derived from the mother `config.xml` (option type + options
list) via `variants/option_formats.php`; `build.php` also adds a hint when it
auto-adds a NEW option key to a json. `_formats` is documentation only — the
build never reads it and the drift check only looks inside `options`.
Naming convention: folder/json `respondage_<client>`, metadata `name`
`respondage_<client>` (must equal the file name), `title`
`"<Client> LimeSurvey theme"` (e.g. `respondage_acme` →

View file

@ -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;

View 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);

View 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;
}
}