ext_wildcards.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Regex
  2. const WC_REGEX = /\b__([^,]+)__([^, ]*)\b/g;
  3. // Trigger conditions
  4. const WC_TRIGGER = () => CFG.useWildcards && [...tagword.matchAll(WC_REGEX)].length > 0;
  5. const WC_FILE_TRIGGER = () => CFG.useWildcards && (tagword.startsWith("__") && !tagword.endsWith("__") || tagword === "__");
  6. class WildcardParser extends BaseTagParser {
  7. async parse() {
  8. // Show wildcards from a file with that name
  9. let wcMatch = [...tagword.matchAll(WC_REGEX)]
  10. let wcFile = wcMatch[0][1];
  11. let wcWord = wcMatch[0][2];
  12. // Look in normal wildcard files
  13. let wcFound = wildcardFiles.find(x => x[1].toLowerCase() === wcFile);
  14. // Use found wildcard file or look in external wildcard files
  15. let wcPair = wcFound || wildcardExtFiles.find(x => x[1].toLowerCase() === wcFile);
  16. let wildcards = (await readFile(`${wcPair[0]}/${wcPair[1]}.txt`)).split("\n")
  17. .filter(x => x.trim().length > 0 && !x.startsWith('#')); // Remove empty lines and comments
  18. let finalResults = [];
  19. let tempResults = wildcards.filter(x => (wcWord !== null && wcWord.length > 0) ? x.toLowerCase().includes(wcWord) : x) // Filter by tagword
  20. tempResults.forEach(t => {
  21. let result = new AutocompleteResult(t.trim(), ResultType.wildcardTag);
  22. result.meta = wcFile;
  23. finalResults.push(result);
  24. });
  25. return finalResults;
  26. }
  27. }
  28. class WildcardFileParser extends BaseTagParser {
  29. parse() {
  30. // Show available wildcard files
  31. let tempResults = [];
  32. if (tagword !== "__") {
  33. let lmb = (x) => x[1].toLowerCase().includes(tagword.replace("__", ""))
  34. tempResults = wildcardFiles.filter(lmb).concat(wildcardExtFiles.filter(lmb)) // Filter by tagword
  35. } else {
  36. tempResults = wildcardFiles.concat(wildcardExtFiles);
  37. }
  38. let finalResults = [];
  39. // Get final results
  40. tempResults.forEach(wcFile => {
  41. let result = new AutocompleteResult(wcFile[1].trim(), ResultType.wildcardFile);
  42. result.meta = "Wildcard file";
  43. finalResults.push(result);
  44. });
  45. return finalResults;
  46. }
  47. }
  48. async function load() {
  49. if (wildcardFiles.length === 0 && wildcardExtFiles.length === 0) {
  50. try {
  51. let wcFileArr = (await readFile(`${tagBasePath}/temp/wc.txt`)).split("\n");
  52. let wcBasePath = wcFileArr[0].trim(); // First line should be the base path
  53. wildcardFiles = wcFileArr.slice(1)
  54. .filter(x => x.trim().length > 0) // Remove empty lines
  55. .map(x => [wcBasePath, x.trim().replace(".txt", "")]); // Remove file extension & newlines
  56. // To support multiple sources, we need to separate them using the provided "-----" strings
  57. let wcExtFileArr = (await readFile(`${tagBasePath}/temp/wce.txt`)).split("\n");
  58. let splitIndices = [];
  59. for (let index = 0; index < wcExtFileArr.length; index++) {
  60. if (wcExtFileArr[index].trim() === "-----") {
  61. splitIndices.push(index);
  62. }
  63. }
  64. // For each group, add them to the wildcardFiles array with the base path as the first element
  65. for (let i = 0; i < splitIndices.length; i++) {
  66. let start = splitIndices[i - 1] || 0;
  67. if (i > 0) start++; // Skip the "-----" line
  68. let end = splitIndices[i];
  69. let wcExtFile = wcExtFileArr.slice(start, end);
  70. let base = wcExtFile[0].trim() + "/";
  71. wcExtFile = wcExtFile.slice(1)
  72. .filter(x => x.trim().length > 0) // Remove empty lines
  73. .map(x => x.trim().replace(base, "").replace(".txt", "")); // Remove file extension & newlines;
  74. wcExtFile = wcExtFile.map(x => [base, x]);
  75. wildcardExtFiles.push(...wcExtFile);
  76. }
  77. } catch (e) {
  78. console.error("Error loading wildcards: " + e);
  79. }
  80. }
  81. }
  82. function sanitize(tagType, text) {
  83. if (tagType === ResultType.wildcardFile) {
  84. return `__${text}__`;
  85. } else if (tagType === ResultType.wildcardTag) {
  86. return text.replace(/^.*?: /g, "");
  87. }
  88. return null;
  89. }
  90. function keepOpenIfWildcard(tagType, sanitizedText, newPrompt, textArea) {
  91. // If it's a wildcard, we want to keep the results open so the user can select another wildcard
  92. if (tagType === ResultType.wildcardFile) {
  93. hideBlocked = true;
  94. autocomplete(textArea, newPrompt, sanitizedText);
  95. setTimeout(() => { hideBlocked = false; }, 100);
  96. return true;
  97. }
  98. return false;
  99. }
  100. // Register the parsers
  101. PARSERS.push(new WildcardParser(WC_TRIGGER));
  102. PARSERS.push(new WildcardFileParser(WC_FILE_TRIGGER));
  103. // Add our utility functions to their respective queues
  104. QUEUE_FILE_LOAD.push(load);
  105. QUEUE_SANITIZE.push(sanitize);
  106. QUEUE_AFTER_INSERT.push(keepOpenIfWildcard);