|
| 1 | +use crate::cursor; |
| 2 | +use crate::extractor::bracket_stack::BracketStack; |
| 3 | +use crate::extractor::pre_processors::pre_processor::PreProcessor; |
| 4 | + |
| 5 | +#[derive(Debug, Default)] |
| 6 | +pub struct Elixir; |
| 7 | + |
| 8 | +impl PreProcessor for Elixir { |
| 9 | + fn process(&self, content: &[u8]) -> Vec<u8> { |
| 10 | + let mut cursor = cursor::Cursor::new(content); |
| 11 | + let mut result = content.to_vec(); |
| 12 | + let mut bracket_stack = BracketStack::default(); |
| 13 | + |
| 14 | + while cursor.pos < content.len() { |
| 15 | + // Look for a sigil marker |
| 16 | + if cursor.curr != b'~' { |
| 17 | + cursor.advance(); |
| 18 | + continue; |
| 19 | + } |
| 20 | + |
| 21 | + // Scan charlists, strings, and wordlists |
| 22 | + if !matches!(cursor.next, b'c' | b'C' | b's' | b'S' | b'w' | b'W') { |
| 23 | + cursor.advance(); |
| 24 | + continue; |
| 25 | + } |
| 26 | + |
| 27 | + cursor.advance_twice(); |
| 28 | + |
| 29 | + // Match the opening for a sigil |
| 30 | + if !matches!(cursor.curr, b'(' | b'[' | b'{') { |
| 31 | + continue; |
| 32 | + } |
| 33 | + |
| 34 | + // Replace the opening bracket with a space |
| 35 | + result[cursor.pos] = b' '; |
| 36 | + |
| 37 | + // Scan until we find a balanced closing one and replace it too |
| 38 | + bracket_stack.push(cursor.curr); |
| 39 | + |
| 40 | + while cursor.pos < content.len() { |
| 41 | + cursor.advance(); |
| 42 | + |
| 43 | + match cursor.curr { |
| 44 | + // Escaped character, skip ahead to the next character |
| 45 | + b'\\' => cursor.advance_twice(), |
| 46 | + b'(' | b'[' | b'{' => { |
| 47 | + bracket_stack.push(cursor.curr); |
| 48 | + } |
| 49 | + b')' | b']' | b'}' if !bracket_stack.is_empty() => { |
| 50 | + bracket_stack.pop(cursor.curr); |
| 51 | + |
| 52 | + if bracket_stack.is_empty() { |
| 53 | + // Replace the closing bracket with a space |
| 54 | + result[cursor.pos] = b' '; |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + _ => {} |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + result |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +#[cfg(test)] |
| 68 | +mod tests { |
| 69 | + use super::Elixir; |
| 70 | + use crate::extractor::pre_processors::pre_processor::PreProcessor; |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn test_elixir_pre_processor() { |
| 74 | + for (input, expected) in [ |
| 75 | + // Simple sigils |
| 76 | + ("~W(flex underline)", "~W flex underline "), |
| 77 | + ("~W[flex underline]", "~W flex underline "), |
| 78 | + ("~W{flex underline}", "~W flex underline "), |
| 79 | + // Sigils with nested brackets |
| 80 | + ( |
| 81 | + "~W(text-(--my-color) bg-(--my-color))", |
| 82 | + "~W text-(--my-color) bg-(--my-color) ", |
| 83 | + ), |
| 84 | + ("~W[text-[red] bg-[red]]", "~W text-[red] bg-[red] "), |
| 85 | + // Word sigils with modifiers |
| 86 | + ("~W(flex underline)a", "~W flex underline a"), |
| 87 | + ("~W(flex underline)c", "~W flex underline c"), |
| 88 | + ("~W(flex underline)s", "~W flex underline s"), |
| 89 | + // Other sigil types |
| 90 | + ("~w(flex underline)", "~w flex underline "), |
| 91 | + ("~c(flex underline)", "~c flex underline "), |
| 92 | + ("~C(flex underline)", "~C flex underline "), |
| 93 | + ("~s(flex underline)", "~s flex underline "), |
| 94 | + ("~S(flex underline)", "~S flex underline "), |
| 95 | + ] { |
| 96 | + Elixir::test(input, expected); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn test_extract_candidates() { |
| 102 | + let input = r#" |
| 103 | + ~W(c1 c2) |
| 104 | + ~W[c3 c4] |
| 105 | + ~W{c5 c6} |
| 106 | + ~W(text-(--c7) bg-(--c8)) |
| 107 | + ~W[text-[c9] bg-[c10]] |
| 108 | + ~W(c13 c14)a |
| 109 | + ~W(c15 c16)c |
| 110 | + ~W(c17 c18)s |
| 111 | + ~w(c19 c20) |
| 112 | + ~c(c21 c22) |
| 113 | + ~C(c23 c24) |
| 114 | + ~s(c25 c26) |
| 115 | + ~S(c27 c28) |
| 116 | + ~W"c29 c30" |
| 117 | + ~W'c31 c32' |
| 118 | + "#; |
| 119 | + |
| 120 | + Elixir::test_extract_contains( |
| 121 | + input, |
| 122 | + vec![ |
| 123 | + "c1", |
| 124 | + "c2", |
| 125 | + "c3", |
| 126 | + "c4", |
| 127 | + "c5", |
| 128 | + "c6", |
| 129 | + "text-(--c7)", |
| 130 | + "bg-(--c8)", |
| 131 | + "c13", |
| 132 | + "c14", |
| 133 | + "c15", |
| 134 | + "c16", |
| 135 | + "c17", |
| 136 | + "c18", |
| 137 | + "c19", |
| 138 | + "c20", |
| 139 | + "c21", |
| 140 | + "c22", |
| 141 | + "c23", |
| 142 | + "c24", |
| 143 | + "c25", |
| 144 | + "c26", |
| 145 | + "c27", |
| 146 | + "c28", |
| 147 | + "c29", |
| 148 | + "c30", |
| 149 | + "c31", |
| 150 | + "c32", |
| 151 | + ], |
| 152 | + ); |
| 153 | + } |
| 154 | +} |
0 commit comments