Index: gc_lang/fr/nodejs/cli/bin/gramma-cli.js ================================================================== --- gc_lang/fr/nodejs/cli/bin/gramma-cli.js +++ gc_lang/fr/nodejs/cli/bin/gramma-cli.js @@ -14,12 +14,10 @@ https://stackoverflow.com/questions/41058569/what-is-the-difference-between-const-and-const-in-javascript */ const argCmd = require("../lib/minimist.js")(process.argv.slice(2)); const { performance } = require("perf_hooks"); -const path = require("path"); -const fs = require("fs"); //Initialisation des messages const msgStart = "\x1b[31mBienvenue sur Grammalecte pour NodeJS!!!\x1b[0m\n"; const msgPrompt = "\x1b[36mGrammaJS\x1b[33m>\x1b[0m "; const msgSuite = "\x1b[33m…\x1b[0m "; @@ -417,23 +415,15 @@ repAction[action] = repPreference[action] ? "ON" : "OFF"; } } if (getArg(aArg, ["dicomain"])) { - let filename = sText.endsWith(".json") ? sText : sText + ".json"; - repAction["dicomain"] = "Chargement du dictionnaire principal " + (oGrammarChecker.setMainDictionary(filename) ? "OK" : "Pas OK"); + repAction["dicomain"] = "Chargement du dictionnaire principal " + (oGrammarChecker.setMainDictionary(sText) ? "OK" : "Pas OK"); } if (getArg(aArg, ["dicoperso"])) { - let pathnormalized = path.normalize(sText); - if (fs.existsSync(pathnormalized)) { - let filename = path.basename(pathnormalized); - let dirname = path.dirname(pathnormalized); - repAction["dicoperso"] = "Chargement du dictionnaire personnel " + (oGrammarChecker.setPersonalDictionary(filename, dirname) ? "OK" : "Pas OK"); - } else { - repAction["dicoperso"] = "Le fichier de dictionnaire n'existe pas."; - } + repAction["dicoperso"] = "Chargement du dictionnaire personnel " + (oGrammarChecker.setPersonalDictionary(sText) ? "OK" : "Pas OK"); } for (const action of ["gceoption", "tfoption", "gcerule"]) { if (getArg(aArg, [action])) { let sFonction = action == "gceoption" ? "GceOption" : action == "tfoption" ? "TfOption" : "GceIgnoreRule"; Index: gc_lang/fr/nodejs/core/api.js ================================================================== --- gc_lang/fr/nodejs/core/api.js +++ gc_lang/fr/nodejs/core/api.js @@ -7,10 +7,13 @@ /* jslint esversion:6 */ /* global require, exports, console */ "use strict"; +const path = require("path"); +const fs = require("fs"); + class GrammarChecker { constructor(aInit, sLangCode = "fr", sContext = "Javascript") { this.sLangCode = sLangCode; this.sContext = sContext; @@ -195,16 +198,47 @@ this.load(["Graphspell"]); } return this.oSpellChecker; } - setMainDictionary(dictionary, sPath = "") { - return this.oSpellChecker.setMainDictionary(dictionary, sPath); + setMainDictionary(dictionary) { + if (typeof dictionary == "string") { + let pathnormalized = path.normalize(dictionary); + if (pathnormalized == "fr-allvars" || pathnormalized == "fr-classic" || pathnormalized == "fr-reform") { + pathnormalized = path.normalize(this.sPathRoot + "/graphspell/_dictionaries/" + pathnormalized); + } + pathnormalized = !pathnormalized.endsWith(".json") ? pathnormalized + ".json" : pathnormalized; + if (fs.existsSync(pathnormalized)) { + let filename = path.basename(pathnormalized); + let dirname = path.dirname(pathnormalized); + return this.oSpellChecker.setMainDictionary(filename, dirname); + } + return false; + } + //It's a valid json? + if (typeof dictionary !== "undefined" && dictionary.sHeader && dictionary.sHeader.startsWith("/grammalecte-fsa/")) { + return this.oSpellChecker.setMainDictionary(dictionary); + } + return false; } - setPersonalDictionary(dictionary, sPath = "", bActivate = true) { - return this.oSpellChecker.setPersonalDictionary(dictionary, sPath, bActivate); + setPersonalDictionary(dictionary, bActivate = true) { + if (typeof dictionary == "string") { + let pathnormalized = path.normalize(dictionary); + pathnormalized = !pathnormalized.endsWith(".json") ? pathnormalized + ".json" : pathnormalized; + if (fs.existsSync(pathnormalized)) { + let filename = path.basename(pathnormalized); + let dirname = path.dirname(pathnormalized); + return this.oSpellChecker.setPersonalDictionary(filename, dirname, bActivate); + } + return false; + } + //It's a valid json? + if (typeof dictionary !== "undefined" && dictionary.sHeader && dictionary.sHeader.startsWith("/grammalecte-fsa/")) { + return this.oSpellChecker.setPersonalDictionary(dictionary, bActivate); + } + return false; } spellParagraph(sText, bSuggest = true) { if (!this.isInit.Graphspell) { this.load(["Graphspell"]);