|
| 1 | + |
| 2 | +// Mocking the necessary parts from app.js to test the parser |
| 3 | + |
| 4 | +function getCleanText(rawText) { |
| 5 | + if (!rawText) return ""; |
| 6 | + |
| 7 | + let text = rawText; |
| 8 | + |
| 9 | + // 1. First pass: Remove hidden structure annotations like @@.(# Title) specifically |
| 10 | + text = text.replace(/@@\.\([^)]*\)/g, ""); |
| 11 | + |
| 12 | + // 2. Second pass: Remove null entity patterns without visible parameters |
| 13 | + // - @@. (bare null entity) |
| 14 | + // - @@.modifier(hidden) patterns |
| 15 | + text = text.replace(/@@\.(?:[\w:!?]+(?:\([^)]*\))?)?(?!\[)/g, ""); |
| 16 | + |
| 17 | + // 3. Third pass: Remove other hidden annotations without visible parameters |
| 18 | + // - REMOVED as it was interfering with @@(Hidden).Modifier[Visible] |
| 19 | + // - Pass 4 should handle removal of hidden entities without visible params |
| 20 | + // const hiddenAnnotationRegex = /@@\([^)]+\)(?!\[[^\]]*\])/gu; |
| 21 | + // text = text.replace(hiddenAnnotationRegex, ""); |
| 22 | + |
| 23 | + // 4. Fourth pass: Process annotations that are meant to be visible. |
| 24 | + const visibleAnnotationRegex = |
| 25 | + /@@(?:([\p{L}\p{N}_]+)|\(([^)]+)\)|(\.))((?:\.[\w:!?]+(?:(?:\([^)]*\)|\[[^\]]*\]))?)*)/gu; |
| 26 | + |
| 27 | + text = text.replace( |
| 28 | + visibleAnnotationRegex, |
| 29 | + (fullMatch, visibleName, hiddenContent, isNullDot, modifiersStr) => { |
| 30 | + // Extract content from visible parameters `[...]` only. |
| 31 | + const visibleParamRegex = /\[([^\]]*)\]/g; |
| 32 | + let visibleParamsOutput = ""; |
| 33 | + let match; |
| 34 | + while ((match = visibleParamRegex.exec(modifiersStr)) !== null) { |
| 35 | + visibleParamsOutput += match[1]; |
| 36 | + } |
| 37 | + |
| 38 | + // If it's a visible entity, output its name + visible params. |
| 39 | + if (visibleName) { |
| 40 | + let separator = ""; |
| 41 | + // Add space if visibleParamsOutput doesn't start with punctuation |
| 42 | + // Punctuation chars that shouldn't have preceding space: . , : ; ? ! ) |
| 43 | + if (visibleParamsOutput.length > 0 && !/^[.,:;?!)]/.test(visibleParamsOutput)) { |
| 44 | + separator = " "; |
| 45 | + } |
| 46 | + return visibleName.replace(/_/g, " ") + separator + visibleParamsOutput; |
| 47 | + } |
| 48 | + |
| 49 | + // If it's a hidden entity or null entity with visible parameters, output only the visible parameters. |
| 50 | + if ((hiddenContent || isNullDot) && visibleParamsOutput.length > 0) { |
| 51 | + return visibleParamsOutput; |
| 52 | + } |
| 53 | + |
| 54 | + // This case should ideally not be reached, as fully hidden ones should be gone. |
| 55 | + // But as a fallback, return empty. |
| 56 | + return ""; |
| 57 | + }, |
| 58 | + ); |
| 59 | + |
| 60 | + // 5. Final cleanup: Remove any remaining modifier syntax that wasn't part of a match, |
| 61 | + // e.g. .modifier or .modifier(hidden) that was left over due to partial replacement |
| 62 | + text = text.replace(/\.[\w:!?]+(?:\([^)]*\))?/g, ""); |
| 63 | + |
| 64 | + return text; |
| 65 | +} |
| 66 | + |
| 67 | +function parseMuseTag(rawText) { |
| 68 | + const cleanText = getCleanText(rawText); |
| 69 | + const entities = new Map(); |
| 70 | + |
| 71 | + // STEP 1: Parse all @@ annotations |
| 72 | + const annotationRegex = |
| 73 | + /(@{2,4})(?:([\p{L}\p{N}_]+)|\(([^)]+)\))((?:\.[\w:!?]+(?:(?:\(([^)]*)\)|\[[^\]]*\]))?)*)/gu; |
| 74 | + |
| 75 | + let match; |
| 76 | + while ((match = annotationRegex.exec(rawText)) !== null) { |
| 77 | + const rawName = match[2] || match[3]; |
| 78 | + const entityName = rawName.replace(/_/g, " "); |
| 79 | + const modifiersString = match[4] || ""; |
| 80 | + |
| 81 | + if (!entities.has(entityName)) { |
| 82 | + entities.set(entityName, { |
| 83 | + name: entityName, |
| 84 | + occurrences: [] |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + const entityData = entities.get(entityName); |
| 89 | + const currentOccurrence = { |
| 90 | + position: match.index, |
| 91 | + localInfo: [], |
| 92 | + }; |
| 93 | + |
| 94 | + // Parse modifiers |
| 95 | + const modifierRegex = /\.([\w:!?]+)(?:(?:\(([^)]*)\)|\[([^\]]*)\]))?/g; |
| 96 | + let modMatch; |
| 97 | + while ((modMatch = modifierRegex.exec(modifiersString)) !== null) { |
| 98 | + const modName = modMatch[1]; |
| 99 | + const modValue = |
| 100 | + modMatch[2] !== undefined |
| 101 | + ? modMatch[2] |
| 102 | + : modMatch[3] !== undefined |
| 103 | + ? modMatch[3] |
| 104 | + : null; |
| 105 | + |
| 106 | + currentOccurrence.localInfo.push({ name: modName, value: modValue }); |
| 107 | + } |
| 108 | + entityData.occurrences.push(currentOccurrence); |
| 109 | + } |
| 110 | + |
| 111 | + return { cleanText, entities }; |
| 112 | +} |
| 113 | + |
| 114 | +// Test Cases |
| 115 | +console.log("--- Test Case 1: No automatic separator ---"); |
| 116 | +const text1 = "@@Sherlock.Dialog[: Hello]"; |
| 117 | +const result1 = parseMuseTag(text1); |
| 118 | +console.log(`Input: ${text1}`); |
| 119 | +console.log(`Output: ${result1.cleanText}`); |
| 120 | +console.log(`Expected: Sherlock: Hello`); |
| 121 | +if (result1.cleanText === "Sherlock: Hello") console.log("PASS"); else console.log("FAIL"); |
| 122 | + |
| 123 | +console.log("\n--- Test Case 2: Hidden entity with Dialog ---"); |
| 124 | +const text2 = "@@(Sherlock).Dialog[Hello]."; |
| 125 | +const result2 = parseMuseTag(text2); |
| 126 | +console.log(`Input: ${text2}`); |
| 127 | +console.log(`Output: ${result2.cleanText}`); |
| 128 | +console.log(`Expected: Hello.`); |
| 129 | +if (result2.cleanText === "Hello.") console.log("PASS"); else console.log("FAIL"); |
| 130 | + |
| 131 | +console.log("Entities found:"); |
| 132 | +result2.entities.forEach((e, name) => { |
| 133 | + console.log(`Entity: ${name}`); |
| 134 | + console.log("Occurrences:", JSON.stringify(e.occurrences)); |
| 135 | +}); |
| 136 | + |
| 137 | +console.log("\n--- Test Case 3: Hidden entity removal ---"); |
| 138 | +const text3 = "This is @@(Hidden)."; |
| 139 | +const result3 = parseMuseTag(text3); |
| 140 | +console.log(`Input: ${text3}`); |
| 141 | +console.log(`Output: ${result3.cleanText}`); |
| 142 | +console.log(`Expected: This is .`); |
| 143 | +if (result3.cleanText === "This is .") console.log("PASS"); else console.log("FAIL"); |
| 144 | +console.log("\n--- Test Case 4: Spacing with text ---"); |
| 145 | +const text4 = "@@Sherlock.Dialog[Elementary]"; |
| 146 | +const result4 = parseMuseTag(text4); |
| 147 | +console.log(`Input: ${text4}`); |
| 148 | +console.log(`Output: ${result4.cleanText}`); |
| 149 | +console.log(`Expected: Sherlock Elementary`); |
| 150 | +if (result4.cleanText === "Sherlock Elementary") console.log("PASS"); else console.log("FAIL"); |
| 151 | + |
| 152 | +console.log("\n--- Test Case 5: Spacing with punctuation ---"); |
| 153 | +const text5 = "@@Sherlock.Dialog[: Elementary]"; |
| 154 | +const result5 = parseMuseTag(text5); |
| 155 | +console.log(`Input: ${text5}`); |
| 156 | +console.log(`Output: ${result5.cleanText}`); |
| 157 | +console.log(`Expected: Sherlock: Elementary`); |
| 158 | +if (result5.cleanText === "Sherlock: Elementary") console.log("PASS"); else console.log("FAIL"); |
0 commit comments