|
| 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 Haml; |
| 7 | + |
| 8 | +impl PreProcessor for Haml { |
| 9 | + fn process(&self, content: &[u8]) -> Vec<u8> { |
| 10 | + let len = content.len(); |
| 11 | + let mut result = content.to_vec(); |
| 12 | + let mut cursor = cursor::Cursor::new(content); |
| 13 | + let mut bracket_stack = BracketStack::default(); |
| 14 | + |
| 15 | + while cursor.pos < len { |
| 16 | + match cursor.curr { |
| 17 | + // Consume strings as-is |
| 18 | + b'\'' | b'"' => { |
| 19 | + let len = cursor.input.len(); |
| 20 | + let end_char = cursor.curr; |
| 21 | + |
| 22 | + cursor.advance(); |
| 23 | + |
| 24 | + while cursor.pos < len { |
| 25 | + match cursor.curr { |
| 26 | + // Escaped character, skip ahead to the next character |
| 27 | + b'\\' => cursor.advance_twice(), |
| 28 | + |
| 29 | + // End of the string |
| 30 | + b'\'' | b'"' if cursor.curr == end_char => break, |
| 31 | + |
| 32 | + // Everything else is valid |
| 33 | + _ => cursor.advance(), |
| 34 | + }; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Replace following characters with spaces if they are not inside of brackets |
| 39 | + b'.' | b'#' | b'=' if bracket_stack.is_empty() => { |
| 40 | + result[cursor.pos] = b' '; |
| 41 | + } |
| 42 | + |
| 43 | + b'(' | b'[' | b'{' => { |
| 44 | + // Replace first bracket with a space |
| 45 | + if bracket_stack.is_empty() { |
| 46 | + result[cursor.pos] = b' '; |
| 47 | + } |
| 48 | + bracket_stack.push(cursor.curr); |
| 49 | + } |
| 50 | + |
| 51 | + b')' | b']' | b'}' => { |
| 52 | + bracket_stack.pop(cursor.curr); |
| 53 | + |
| 54 | + // Replace closing bracket with a space |
| 55 | + if bracket_stack.is_empty() { |
| 56 | + result[cursor.pos] = b' '; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Consume everything else |
| 61 | + _ => {} |
| 62 | + }; |
| 63 | + |
| 64 | + cursor.advance(); |
| 65 | + } |
| 66 | + |
| 67 | + result |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +#[cfg(test)] |
| 72 | +mod tests { |
| 73 | + use super::Haml; |
| 74 | + use crate::extractor::pre_processors::pre_processor::PreProcessor; |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_haml_pre_processor() { |
| 78 | + for (input, expected) in [ |
| 79 | + // Element with classes |
| 80 | + ( |
| 81 | + "%body.flex.flex-col.items-center.justify-center", |
| 82 | + "%body flex flex-col items-center justify-center", |
| 83 | + ), |
| 84 | + // Plain element |
| 85 | + ( |
| 86 | + ".text-slate-500.xl:text-gray-500", |
| 87 | + " text-slate-500 xl:text-gray-500", |
| 88 | + ), |
| 89 | + // Element with hash attributes |
| 90 | + ( |
| 91 | + ".text-black.xl:text-red-500{ data: { tailwind: 'css' } }", |
| 92 | + " text-black xl:text-red-500 data: { tailwind: 'css' } ", |
| 93 | + ), |
| 94 | + // Element with a boolean attribute |
| 95 | + ( |
| 96 | + ".text-green-500.xl:text-blue-500(data-sidebar)", |
| 97 | + " text-green-500 xl:text-blue-500 data-sidebar ", |
| 98 | + ), |
| 99 | + // Element with interpreted content |
| 100 | + ( |
| 101 | + ".text-yellow-500.xl:text-purple-500= 'Element with interpreted content'", |
| 102 | + " text-yellow-500 xl:text-purple-500 'Element with interpreted content'", |
| 103 | + ), |
| 104 | + // Element with a hash at the end and an extra class. |
| 105 | + ( |
| 106 | + ".text-orange-500.xl:text-pink-500{ class: 'bg-slate-100' }", |
| 107 | + " text-orange-500 xl:text-pink-500 class: 'bg-slate-100' ", |
| 108 | + ), |
| 109 | + // Object reference |
| 110 | + ( |
| 111 | + ".text-teal-500.xl:text-indigo-500[@user, :greeting]", |
| 112 | + " text-teal-500 xl:text-indigo-500 @user, :greeting ", |
| 113 | + ), |
| 114 | + // Element with an ID |
| 115 | + ( |
| 116 | + ".text-lime-500.xl:text-emerald-500#root", |
| 117 | + " text-lime-500 xl:text-emerald-500 root", |
| 118 | + ), |
| 119 | + ] { |
| 120 | + Haml::test(input, expected); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments