Skip to content

Commit 5716e55

Browse files
committed
handle embedded slim template in Ruby files
1 parent 55b1903 commit 5716e55

File tree

1 file changed

+19
-0
lines changed
  • crates/oxide/src/extractor/pre_processors

1 file changed

+19
-0
lines changed

crates/oxide/src/extractor/pre_processors/ruby.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
use crate::cursor;
44
use crate::extractor::bracket_stack;
55
use crate::extractor::pre_processors::pre_processor::PreProcessor;
6+
use crate::pre_process_input;
7+
use bstr::ByteSlice;
8+
use regex::Regex;
9+
use std::sync;
10+
11+
static SLIM_TEMPLATE_REGEX: sync::LazyLock<Regex> =
12+
sync::LazyLock::new(|| Regex::new(r#"<<[-~]SLIM\n([\s\S]*?)SLIM"#).unwrap());
613

714
#[derive(Debug, Default)]
815
pub struct Ruby;
@@ -14,6 +21,18 @@ impl PreProcessor for Ruby {
1421
let mut cursor = cursor::Cursor::new(content);
1522
let mut bracket_stack = bracket_stack::BracketStack::default();
1623

24+
// Extract embedded Slim languages
25+
// https://viewcomponent.org/guide/templates.html#interpolations
26+
let content_as_str = std::str::from_utf8(content).unwrap();
27+
for (_, [body]) in SLIM_TEMPLATE_REGEX
28+
.captures_iter(content_as_str)
29+
.map(|c| c.extract())
30+
{
31+
let replaced = pre_process_input(body.as_bytes(), "slim");
32+
result = result.replace(body, replaced);
33+
}
34+
35+
// Ruby extraction
1736
while cursor.pos < len {
1837
// Looking for `%w` or `%W`
1938
if cursor.curr != b'%' && !matches!(cursor.next, b'w' | b'W') {

0 commit comments

Comments
 (0)