| 20 | |
| 21 | // Returns a result of the shape: { keywordToEngine, validationErrors }. |
| 22 | export function parseConfig(configText) { |
| 23 | const results = {}; |
| 24 | const errors = []; |
| 25 | for (const line of commands.parseLines(configText)) { |
| 26 | const tokens = line.split(/\s+/); |
| 27 | if (tokens.length < 2) { |
| 28 | errors.push(`This line has less than two tokens: ${line}`); |
| 29 | continue; |
| 30 | } |
| 31 | if (!tokens[0].includes(":")) { |
| 32 | errors.push(`This line doesn't include a ":" character: ${line}`); |
| 33 | continue; |
| 34 | } |
| 35 | const keyword = tokens[0].split(":")[0]; |
| 36 | const url = tokens[1]; |
| 37 | const description = tokens.length > 2 ? tokens.slice(2).join(" ") : `search (${keyword})`; |
| 38 | |
| 39 | if (!UrlUtils.urlHasProtocol(url) && !UrlUtils.hasJavascriptProtocol(url)) { |
| 40 | errors.push(`This search engine doesn't have a valid URL: ${line}`); |
| 41 | continue; |
| 42 | } |
| 43 | results[keyword] = new UserSearchEngine({ keyword, url, description }); |
| 44 | } |
| 45 | return { |
| 46 | keywordToEngine: results, |
| 47 | validationErrors: errors, |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | export function set(searchEnginesConfigText) { |
| 52 | keywordToEngine = parseConfig(searchEnginesConfigText).keywordToEngine; |