UNPKG

@4dsas/doc_preprocessing

Version:

Preprocess 4D doc to be built by docusaurus

174 lines (146 loc) 4.93 kB
const fs = require('fs') const SETTINGS_KEY = { PATH: "path", CONFIG: "config", SYNTAX_OUTPUT: "syntax_output", OUTPUT: "output", WATCH: "watch", SETTINGS: "settings", SETTINGS_TYPE: "settings_type", DEPENDENCIES: "dependencies", BUILD_DEPENDENCIES: "build_dependencies", ARGS_DEPENDENCIES: "args_dependencies", LIST_ARGS: "list_args", SYNTAX_ONLY: "syntax_only", SYNTAX_PRETTY: "syntax_pretty", LANGUAGES_LIST: "languages", EXCLUDE_LIST: "exclude_list", VERBOSE: "verbose", SYNTAX_ESCAPE_LIST: "syntax_escape_list", INCLUDE_ESCAPE_LIST: "include_escape_list", } const SETTINGS_TYPE = { DEFAULT: "default" } class Settings { constructor() { this._path = "" this._settings = new Object this.set(SETTINGS_KEY.VERBOSE, true); } has(inSetting) { var found = Object.keys(this._settings).find(e => e == inSetting) return found && (this._settings[inSetting] !== null && this._settings[inSetting] !== undefined); } get(inSetting) { var v = this._settings[inSetting] if (v !== undefined && v !== null) { return v } return null } getArray(inSetting) { var v = this._settings[inSetting] if (v !== undefined && v !== null) { return v } return null } getBoolean(inSetting) { var v = this._settings[inSetting] if (v !== undefined && v !== null) { return !!v } return null } set(inSetting, inValue) { this._settings[inSetting] = inValue } getString(inSetting, inValue) { var v = this._settings[inSetting] if ((v !== undefined && v !== null) && typeof v == 'string' && v.length > 0) { if (inValue !== undefined) return v + inValue; else return v; } return "" } clone() { var s = new Settings() s._settings = Object.assign({}, this._settings); return s; } load(inArgs, inRoot = "", inFatherSettings = null) { this._path = inRoot + ".doc_preprocessing.js" if (inArgs != undefined && inArgs[SETTINGS_KEY.SETTINGS] != undefined) { this._path = inRoot + inArgs[SETTINGS_KEY.SETTINGS] } let obj = null if (fs.existsSync(this._path)) { if (this._path.endsWith(".js")) { // Convert to absolute path for require() const path = require('path') const absolutePath = path.resolve(this._path) // Clear require cache to allow reloading delete require.cache[absolutePath] obj = require(absolutePath) } else { obj = JSON.parse(fs.readFileSync(this._path, 'utf8')) } } if (obj != null) { //fill values with JSON let defaultValue = obj[SETTINGS_TYPE.DEFAULT] for (let [key, value] of Object.entries(defaultValue)) { this._settings[key] = value } //Get the kind and fill let kind; if (inArgs != undefined) { kind = obj[inArgs[SETTINGS_KEY.SETTINGS_TYPE]] } //check father if (!kind && inFatherSettings !== null) { kind = inFatherSettings[inArgs[SETTINGS_KEY.SETTINGS_TYPE]] } if (!kind) { return false; } let listArgs if (inFatherSettings && inFatherSettings._settings) { listArgs = inFatherSettings._settings[SETTINGS_KEY.LIST_ARGS] } if (kind !== undefined) { for (let [key, value] of Object.entries(kind)) { if (listArgs) { let re = /%(.*?)%/g let content = value; let matches = [...content.matchAll(re)] for (let match of matches) { const arg = listArgs[match[1]] if (arg) { content = content.substring(0, match.index) + arg + content.substring(match.index + match[0].length); } } value = content } this._settings[key] = value } } } //fill values with args if (inArgs != undefined) { for (let [key, value] of Object.entries(inArgs)) { if (key !== '_') { this._settings[key] = value } } } return true; } } exports.Settings = Settings exports.SETTINGS_KEY = SETTINGS_KEY exports.SETTINGS_TYPE = SETTINGS_TYPE