| | |
| | const WC_REGEX = /\b__([^,]+)__([^, ]*)\b/g; |
| |
|
| | |
| | const WC_TRIGGER = () => CFG.useWildcards && [...tagword.matchAll(WC_REGEX)].length > 0; |
| | const WC_FILE_TRIGGER = () => CFG.useWildcards && (tagword.startsWith("__") && !tagword.endsWith("__") || tagword === "__"); |
| |
|
| | class WildcardParser extends BaseTagParser { |
| | async parse() { |
| | |
| | let wcMatch = [...tagword.matchAll(WC_REGEX)] |
| | let wcFile = wcMatch[0][1]; |
| | let wcWord = wcMatch[0][2]; |
| |
|
| | |
| | let wcFound = wildcardFiles.find(x => x[1].toLowerCase() === wcFile); |
| | |
| | let wcPair = wcFound || wildcardExtFiles.find(x => x[1].toLowerCase() === wcFile); |
| |
|
| | let wildcards = (await readFile(`${wcPair[0]}/${wcPair[1]}.txt`)).split("\n") |
| | .filter(x => x.trim().length > 0 && !x.startsWith('#')); |
| |
|
| | let finalResults = []; |
| | let tempResults = wildcards.filter(x => (wcWord !== null && wcWord.length > 0) ? x.toLowerCase().includes(wcWord) : x) |
| | tempResults.forEach(t => { |
| | let result = new AutocompleteResult(t.trim(), ResultType.wildcardTag); |
| | result.meta = wcFile; |
| | finalResults.push(result); |
| | }); |
| |
|
| | return finalResults; |
| | } |
| | } |
| |
|
| | class WildcardFileParser extends BaseTagParser { |
| | parse() { |
| | |
| | let tempResults = []; |
| | if (tagword !== "__") { |
| | let lmb = (x) => x[1].toLowerCase().includes(tagword.replace("__", "")) |
| | tempResults = wildcardFiles.filter(lmb).concat(wildcardExtFiles.filter(lmb)) |
| | } else { |
| | tempResults = wildcardFiles.concat(wildcardExtFiles); |
| | } |
| |
|
| | let finalResults = []; |
| | |
| | tempResults.forEach(wcFile => { |
| | let result = new AutocompleteResult(wcFile[1].trim(), ResultType.wildcardFile); |
| | result.meta = "Wildcard file"; |
| | finalResults.push(result); |
| | }); |
| |
|
| | return finalResults; |
| | } |
| | } |
| |
|
| | async function load() { |
| | if (wildcardFiles.length === 0 && wildcardExtFiles.length === 0) { |
| | try { |
| | let wcFileArr = (await readFile(`${tagBasePath}/temp/wc.txt`)).split("\n"); |
| | let wcBasePath = wcFileArr[0].trim(); |
| | wildcardFiles = wcFileArr.slice(1) |
| | .filter(x => x.trim().length > 0) |
| | .map(x => [wcBasePath, x.trim().replace(".txt", "")]); |
| |
|
| | |
| | let wcExtFileArr = (await readFile(`${tagBasePath}/temp/wce.txt`)).split("\n"); |
| | let splitIndices = []; |
| | for (let index = 0; index < wcExtFileArr.length; index++) { |
| | if (wcExtFileArr[index].trim() === "-----") { |
| | splitIndices.push(index); |
| | } |
| | } |
| | |
| | for (let i = 0; i < splitIndices.length; i++) { |
| | let start = splitIndices[i - 1] || 0; |
| | if (i > 0) start++; |
| | let end = splitIndices[i]; |
| |
|
| | let wcExtFile = wcExtFileArr.slice(start, end); |
| | let base = wcExtFile[0].trim() + "/"; |
| | wcExtFile = wcExtFile.slice(1) |
| | .filter(x => x.trim().length > 0) |
| | .map(x => x.trim().replace(base, "").replace(".txt", "")); |
| |
|
| | wcExtFile = wcExtFile.map(x => [base, x]); |
| | wildcardExtFiles.push(...wcExtFile); |
| | } |
| | } catch (e) { |
| | console.error("Error loading wildcards: " + e); |
| | } |
| | } |
| | } |
| |
|
| | function sanitize(tagType, text) { |
| | if (tagType === ResultType.wildcardFile) { |
| | return `__${text}__`; |
| | } else if (tagType === ResultType.wildcardTag) { |
| | return text.replace(/^.*?: /g, ""); |
| | } |
| | return null; |
| | } |
| |
|
| | function keepOpenIfWildcard(tagType, sanitizedText, newPrompt, textArea) { |
| | |
| | if (tagType === ResultType.wildcardFile) { |
| | hideBlocked = true; |
| | autocomplete(textArea, newPrompt, sanitizedText); |
| | setTimeout(() => { hideBlocked = false; }, 100); |
| | return true; |
| | } |
| | return false; |
| | } |
| |
|
| | |
| | PARSERS.push(new WildcardParser(WC_TRIGGER)); |
| | PARSERS.push(new WildcardFileParser(WC_FILE_TRIGGER)); |
| |
|
| | |
| | QUEUE_FILE_LOAD.push(load); |
| | QUEUE_SANITIZE.push(sanitize); |
| | QUEUE_AFTER_INSERT.push(keepOpenIfWildcard); |