tack/
lib.rs

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
/// Marks up CSS to coloured HTML
///
/// Assumptions: no trailing whitespace
///              CSS is valid and properly formatted
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let s =  let a = "
/// body {
///		display: flex;
///		min-height: 100%;
/// }".to_string();
///	coloured_html(s);
/// ```

pub fn coloured_html(s: String) -> String {
    let mut new = String::new();
    for line in s.lines() {
        if line.contains("{") {
            // mark up the entity, e.g. a classname or a CSS selector
            let entity = line.split_once(" {").unwrap().0.trim_start();
            let newline = &line.replace(entity, &html_tag(entity));
            new.push_str(newline);
            new.push('\n');
        } else if line.contains(":") {
            let split = line.split_once(':').unwrap();

            // mark up the CSS property
            let property = split.0.trim_start();
            let newline = &line.replace(property, &html_property(property));

            // mark up the value
            let mut value = split.1.chars();
            value.next(); // remove leading whitespace that was between colon and value
            value.next_back(); // don't touch trailing semicolon

            let value_first_char = value.next().unwrap();
            let value = value_first_char.to_string() + value.as_str();
            let html_value = if value_first_char.is_numeric() {
                html_value_numeric(&value)
            } else {
                html_value_string(&value)
            };
            let newline = newline.replace(&value, &html_value);
            new.push_str(&newline);
            new.push('\n');
        } else if line == "}" {
            new.push('}');
            new.push('\n');
        }
    }
    new
}

// HTML generator for a single tag
fn html_tag(s: &str) -> String {
    format!("<span class=\"tag\">{}</span>", s)
}

// HTML generator for a property
fn html_property(s: &str) -> String {
    format!("<span class=\"property\">{}</span>", s)
}

// HTML generator for a numeric value
fn html_value_numeric(s: &str) -> String {
    format!("<span class=\"numeric\">{}</span>", s)
}

// HTML generator for a string value
fn html_value_string(s: &str) -> String {
    format!("<span class=\"string\">{}</span>", s)
}

/// Check if braces are valid.
fn valid_braces<'a>(s: &'a str) -> bool {
    let mut stack = Vec::with_capacity(s.len());

    for c in s.chars() {
        match c {
            '}' => {
                if !stack.is_empty() && stack.last() == Some(&'{') {
                    stack.pop();
                } else {
                    return false;
                }
            }
            // Add the open brace to the stack.
            '{' => stack.push(c),
            _ => (),
        }
    }
    if stack.is_empty() {
        true
    } else {
        false
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn one_open_brace() {
        assert_eq!(super::valid_braces("{"), false);
    }

    #[test]
    fn one_closed_brace() {
        assert_eq!(super::valid_braces("}"), false);
    }

    #[test]
    fn two_open_braces() {
        assert_eq!(super::valid_braces("{{"), false);
    }

    #[test]
    fn two_closed_braces() {
        assert_eq!(super::valid_braces("}}"), false);
    }

    #[test]
    fn one_brace_pair() {
        assert_eq!(super::valid_braces("{}"), true);
    }

    #[test]
    fn two_brace_pairs() {
        assert_eq!(super::valid_braces("{}{}"), true);
    }

    #[test]
    fn one_brace_pair_with_word() {
        assert_eq!(super::valid_braces("{hi}"), true);
    }

    #[test]
    fn html_html_tag() {
        assert_eq!(super::html_tag("html"), "<span class=\"tag\">html</span>");
    }

    #[test]
    fn html_body_tag() {
        assert_eq!(super::html_tag("body"), "<span class=\"tag\">body</span>");
    }

    #[test]
    fn html_main_tag() {
        assert_eq!(super::html_tag("main"), "<span class=\"tag\">main</span>");
    }

    #[test]
    fn html_value_int() {
        assert_eq!(
            super::html_value_numeric("5"),
            "<span class=\"numeric\">5</span>"
        );
    }

    #[test]
    fn html_value_pct() {
        assert_eq!(
            super::html_value_numeric("10%"),
            "<span class=\"numeric\">10%</span>"
        );
    }

    #[test]
    fn html_value_flex() {
        assert_eq!(
            super::html_value_string("flex"),
            "<span class=\"string\">flex</span>"
        );
    }
}