From f3d3912c2fe91b87cb172af0a26125dea817f0e8 Mon Sep 17 00:00:00 2001 From: Tammo ter Hark Date: Mon, 30 Nov 2026 16:57:44 +0100 Subject: [PATCH] feat(variants): document value format per option key in variant jsons Every variants/.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 + 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 --- README.md | 13 +++++ variants/build.php | 18 ++++++- variants/document_options.php | 89 +++++++++++++++++++++++++++++++++ variants/option_formats.php | 94 +++++++++++++++++++++++++++++++++++ 4 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 variants/document_options.php create mode 100644 variants/option_formats.php diff --git a/README.md b/README.md index 5c02146..6ba83ec 100644 --- a/README.md +++ b/README.md @@ -413,6 +413,19 @@ the variant's `preview.png` is identical to the mother's. leave the rest empty. Legacy keys (e.g. `logotopmargin`, `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 # update _formats in every *.json + php variants/document_options.php --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_`, metadata `name` `respondage_` (must equal the file name), `title` `" LimeSurvey theme"` (e.g. `respondage_acme` → diff --git a/variants/build.php b/variants/build.php index 73eb2e1..50101e1 100644 --- a/variants/build.php +++ b/variants/build.php @@ -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: * ..//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; diff --git a/variants/document_options.php b/variants/document_options.php new file mode 100644 index 0000000..667d45b --- /dev/null +++ b/variants/document_options.php @@ -0,0 +1,89 @@ + $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); diff --git a/variants/option_formats.php b/variants/option_formats.php new file mode 100644 index 0000000..f4477f0 --- /dev/null +++ b/variants/option_formats.php @@ -0,0 +1,94 @@ +.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 / 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: = mode dropdown, 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 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 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; + } +}