UNPKG

php-htx-cli

Version:

A cli to convert .htx (different components) files to .php

76 lines (75 loc) 2.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.toPhpFormat = void 0; const regex_1 = __importDefault(require("./regex")); function processVariables(value, localComponentName, localUid, config) { let output = ""; let nestLevel = 0; let isInString = false; let skipNext = false; for (let charIndex = 0; charIndex < value.length; charIndex++) { let match; if ((match = value.slice(charIndex).match(regex_1.default.variable.global)) != null && (isInString == false || skipNext == false)) { output += "$" + match[0].slice("$GLOBAL_".length); charIndex += match[0].length - 1; continue; } if ((match = value.slice(charIndex).match(regex_1.default.variable.local)) != null && (isInString == false || skipNext == false)) { output += "$" + config.constant.variable .replace("<component>", localComponentName) .replace("<uid>", localUid.toString()) .replace("<variable>", match[0].slice(1)); charIndex += match[0].length - 1; continue; } if (value[charIndex] == "(" && isInString == false) { nestLevel++; } if (value[charIndex] == ")" && isInString == false) { nestLevel--; } if (value[charIndex] == "{" && isInString == false) { nestLevel++; } if (value[charIndex] == "}" && isInString == false) { nestLevel--; } if (value[charIndex] == "[" && isInString == false) { nestLevel++; } if (value[charIndex] == "]" && isInString == false) { nestLevel--; } if (value[charIndex] == "\"" && isInString == false && !skipNext) { isInString = "\""; } else if (value[charIndex] == "\"" && isInString == "\"" && !skipNext) { isInString = false; } if (value[charIndex] == "'" && isInString == false && !skipNext) { isInString = "'"; } else if (value[charIndex] == "'" && isInString == "'" && !skipNext) { isInString = false; } if (value[charIndex] == "\\" && isInString != false) { skipNext = true; } else { skipNext = false; } output += value[charIndex]; } return output; } function toPhpFormat(props, localComponentName, localUid, config) { let outputParts = []; for (let key in props) { outputParts.push(`"${key}"=>${processVariables(props[key], localComponentName, localUid, config)}`); } return `[${outputParts.join(",")}]`; } exports.toPhpFormat = toPhpFormat;