1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use super::element_text::ElementText;
use std::str::Lines;

#[derive(Debug)]
struct TagInfo {
    name: String,
    indent: usize,
    is_self_closing: bool,
    in_props: bool,
}

pub fn transform(elm: String) -> String {
    let self_closing_tags = vec!["Image", "img", "SectionDivider"];

    let mut output = String::new();
    let mut tag_stack: Vec<TagInfo> = Vec::new();
    let lines = elm.lines();
    let mut lines_to_skip: u32 = 0;
    for (index, line) in lines.clone().enumerate() {
        if lines_to_skip > 0 {
            lines_to_skip = lines_to_skip - 1;
            continue;
        }
        let trimmed_line = line.trim_start();
        let indent = line.len() - trimmed_line.len();
        check_indent_size(indent, index);

        if trimmed_line.starts_with("|> ") {
            let tag_name = trimmed_line[3..].to_string();
            tag_loop(&mut tag_stack, &mut output, &indent);
            output.push_str(&format!("<{}\n", tag_name));
            tag_stack.push(TagInfo {
                name: tag_name,
                indent,
                is_self_closing: self_closing_tags.contains(&&trimmed_line[3..].trim_end()),
                in_props: true,
            });
            continue;
        }
        if trimmed_line.is_empty() // end of props
          && tag_stack
              .last()
              .map_or(false, |tag| tag.in_props && !tag.is_self_closing)
        {
            output.push_str(">\n\"\" ");
            if let Some(last) = tag_stack.last_mut() {
                last.in_props = false;
            }
            continue;
        }
        if !trimmed_line.is_empty() {
            tag_loop(&mut tag_stack, &mut output, &indent);
            let last = tag_stack.last().expect("There is no parent tag");
            if last.in_props {
                // tag props
                output.push_str(&format!("{}\n", line));
                continue;
            }
            // tag content
            let mut nodes: Vec<(String, bool)> = Vec::new(); // if there is an inline element in the text , the text should be seperated to sub text before the element , the element ( bool is added to know if the node is an element ) and the subtext after the element . it shouldn't be handeled as 1 text
            let mut text_node = String::new();
            let mut inner_lines_to_skip: u32 = 0;

            for (j, text_line) in lines.clone().skip(index).enumerate() {
                if inner_lines_to_skip > 0 {
                    inner_lines_to_skip = inner_lines_to_skip - 1;
                    continue;
                }
                let trimmed_line = text_line.trim_start();
                let indent = text_line.len() - trimmed_line.len();
                check_indent_size(indent, index);
                if trimmed_line.is_empty() {
                    break;
                }
                if !trimmed_line.starts_with("|>") {
                    text_node += &format!(" {}", trimmed_line);
                    lines_to_skip += 1;
                    continue;
                }
                nodes.push((text_node, false));
                text_node = "".to_string();
                // handle inline element that can't be handled by delimiters ( they need props )
                let (element, skips) =
                    handle_inline_element(lines.clone(), trimmed_line, j + index + 1, indent);
                inner_lines_to_skip += skips;
                lines_to_skip += skips;
                nodes.push((format!("\"#{}r#\"", element), true));
            }
            if text_node != "" {
                nodes.push((text_node, false));
            }
            let mut processed_text = String::new();
            for (text, is_element) in nodes {
                if is_element {
                    processed_text += &text;
                    continue;
                }
                processed_text += &ElementText::new(&text).handle_delimeters();
            }
            output.push_str(&concat_ignore_spaces("r#\"", &processed_text, "\"#\n"));
        }
    }

    while let Some(last_tag_info) = tag_stack.pop() {
        if last_tag_info.is_self_closing {
            output.push_str("/>\n");
            continue;
        }
        output.push_str(&format!("</{}>\n", last_tag_info.name));
    }

    output.replace("\n", " ")
}

fn handle_inline_element(
    lines: Lines<'_>,
    element_line: &str,
    start_from: usize,
    initial_indent: usize,
) -> (String, u32) {
    let mut element = "".to_string();
    let mut element_content = "".to_string();
    let mut end_line = start_from;
    let mut inner_indent = initial_indent + 4;
    let mut skips: u32 = 0;
    let mut in_props = true;

    let tag_name = element_line[3..].to_string();
    element.push_str(&format!("<{} \n", tag_name));
    while inner_indent > initial_indent || inner_indent == 0 {
        if lines.clone().nth(end_line).is_none() {
            break;
        }
        let inner_line = lines.clone().nth(end_line).unwrap();
        let inner_trimmed_line = inner_line.trim_start();
        inner_indent = inner_line.len() - inner_trimmed_line.len();
        end_line += 1;
        if inner_indent == 0 && in_props {
            element.push_str(">\n r#\"");
            skips += 1;
            in_props = false;
            continue;
        }
        if inner_indent > initial_indent {
            skips += 1;
            if in_props {
                element.push_str(&(inner_trimmed_line.to_string() + " "));
                continue;
            }
            element_content.push_str(&(inner_trimmed_line.to_string() + " "));
        }
    }

    element.push_str(&ElementText::new(&element_content).handle_delimeters());
    element.push_str(&format!("\"#</{}>\n", tag_name));
    (element, skips)
}

fn concat_ignore_spaces(start: &str, content: &str, end: &str) -> String {
    let trimmed_content = content.trim_start(); // Remove leading spaces from content
    format!("{}{}{}", start, trimmed_content, end)
}

fn tag_loop(tag_stack: &mut Vec<TagInfo>, output: &mut String, indent: &usize) {
    while let Some(last_tag_info) = tag_stack.last() {
        if *indent <= last_tag_info.indent {
            if last_tag_info.is_self_closing {
                output.push_str("/>\n");
            } else {
                output.push_str(&format!("</{}>\n", last_tag_info.name));
            }
            tag_stack.pop();
        } else {
            break;
        }
    }
}

fn check_indent_size(size: usize, error_at: usize) {
    if size % 4 != 0 {
        panic!(
            "Syntax error at line {}, There must be 4 spaces before each block",
            error_at
        )
    }
}