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
187
188
189
190
191
192
193
194
195
196
197
198
199
use class::parse_class;
use lazy_static::lazy_static;
use line_col::LineColLookup;
use modifiers::{generate_state_selector, MediaQuery, State};
use regex::Regex;
use std::fs::{self, File};
use std::{io::Write, path::Path};
use traits::ToStaticStr;
use utils::indent_string;
use warning::{Position, Warning};

mod class;
mod config;
mod modifiers;
mod traits;
mod utils;
mod warning;

lazy_static! {
    static ref STYLE_REGEX: Regex =
        Regex::new(r#"(?:class|className)=(?:["']\W+\s*(?:\w+)\()?["']([^'"]+)['"]"#).unwrap();
}

pub fn parse_html(input: &Path, output: &Path, include_preflight: bool) -> Vec<Warning> {
    let html = fs::read_to_string(input).unwrap();
    let lookup = LineColLookup::new(&html);

    let mut generated_classes = String::new();
    let mut generated_warnings = Vec::new();

    for captures in STYLE_REGEX.captures_iter(&html) {
        if let Some(group) = captures.get(1) {
            let mut index = group.start();

            for cap in group.as_str().split([' ', '\n']) {
                if cap.is_empty() {
                    index += cap.len() + 1;
                    continue;
                }

                let mut warning_types = Vec::new();

                // use linecol crate to get the current line and column
                let position: Position = lookup.get(index).into();

                // if the capture contains a colon ':' then split it and parse everything
                // before the colon as states (hover, after...) and everything after as a regular class
                // TODO: this could probably be improved as it contains a lot duplicated code from
                // the else block
                if let Some((states, raw_class)) = cap.rsplit_once(":") {
                    // if the raw_class starts with a '-', used by negative margins, then skip over the first character
                    let mut split_args = if raw_class.starts_with('-') {
                        raw_class[1..].split("-")
                    } else {
                        raw_class.split("-")
                    };

                    // get the first argument as the class name
                    let class_name = split_args.next().unwrap();

                    // collect the rest into an array
                    let args = [split_args.next(), split_args.next(), split_args.next()]
                        .map(|arg| if let Some(a) = arg { a } else { "" });

                    // parse all the states into a buffer
                    let mut states_buf = Vec::new();
                    for s in states.split(':') {
                        if let Some(state) = State::new(s, &mut warning_types) {
                            states_buf.push(state)
                        }
                    }

                    // parse class from class_name and args
                    if let Some(class) = parse_class(class_name, &args, &mut warning_types) {
                        // generate a CSS selector for the class
                        // ':' have to be replaced to be valid CSS
                        // and State::PseudoElement and State::PseudoClass go after the class name
                        // .hover:aspect-auto:hover becomes .hover\:aspect-auto:hover
                        let class_selector = if states_buf.iter().any(|s| match s {
                            State::PseudoClass(_) | State::PseudoElement(_) => true,
                            _ => false,
                        }) {
                            format!(
                                "{}:{}",
                                cap.replace(':', "\\:"),
                                generate_state_selector(&states_buf)
                            )
                        } else {
                            cap.replace(':', "\\:")
                        };

                        // generate the entire class with curly braces and new lines
                        let mut generated_class =
                            format!(".{} {{\n    {};\n}}", class_selector, class.join(";\n    "));

                        // if it's a media query, wrap the entire class with a
                        // pair of curly braces and indent the CSS one level
                        // TODO: setup matches for MediaQuery::Print, MediaQuery::Ltr, MediaQuery::Rtl
                        for state in states_buf {
                            match state {
                                State::MediaQuery(mq) => match mq {
                                    MediaQuery::Sm
                                    | MediaQuery::Md
                                    | MediaQuery::Lg
                                    | MediaQuery::Xl
                                    | MediaQuery::Xxl
                                    | MediaQuery::Dark
                                    | MediaQuery::MotionReduce
                                    | MediaQuery::MotionSafe
                                    | MediaQuery::ContrastMore
                                    | MediaQuery::ContrastLess
                                    | MediaQuery::Portrait
                                    | MediaQuery::Landscape => {
                                        generated_class = format!(
                                            "@media ({}) {{\n{}}}",
                                            mq.to_static_str(),
                                            indent_string(&generated_class)
                                        );
                                    }
                                    _ => (),
                                },
                                _ => (),
                            }
                        }

                        // push the generated class to a buffer
                        generated_classes.push_str(&generated_class);
                    }
                } else {
                    let mut split_args = if cap.starts_with('-') {
                        cap[1..].split("-")
                    } else {
                        cap.split("-")
                    };

                    let class_name = split_args.next().unwrap();
                    let args = [split_args.next(), split_args.next(), split_args.next()]
                        .map(|arg| if let Some(a) = arg { a } else { "" });

                    if let Some(class) = parse_class(class_name, &args, &mut warning_types) {
                        let generated_class = format!(
                            ".{} {{\n    {};\n}}",
                            cap.replace(':', "\\:"),
                            class.join(";\n    ")
                        );

                        generated_classes.push_str(&generated_class);
                    }
                }

                for warning_type in warning_types {
                    generated_warnings.push(Warning::new(cap, &position, warning_type));
                }

                index += cap.len() + 1;

                generated_classes.push('\n');
                generated_classes.push('\n');
            }
        }
    }

    // replaces a double newline with a single newline
    if generated_classes.ends_with("\n\n") {
        generated_classes = generated_classes.trim_end().to_string();
        generated_classes.push('\n');
    }

    let mut css_file = File::create(output).unwrap();

    if include_preflight {
        let preflight = fs::read_to_string("preflight.css").unwrap();
        css_file.write_all(preflight.as_bytes()).unwrap();
        css_file.write_all("\n\n".as_bytes()).unwrap();
    }

    css_file.write_all(generated_classes.as_bytes()).unwrap();

    generated_warnings
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_html() {
        let input = Path::new("../index.html");
        let output = Path::new("../railwind.css");

        let warnings = parse_html(input, output, false);

        for warning in warnings {
            println!("{}", warning);
        }

        assert!(true)
    }
}