flav_md_engine/util/
string.rs1pub mod string {
2 pub fn split_string(content: String) -> Vec<String> {
3 content.split('\n').into_iter().map(|s| s.into()).collect()
4 }
5
6 pub fn escape_code_string(content: &String) -> String {
7 content
8 .replace("&", "&")
9 .replace("<", "<")
10 .replace(">", ">")
11 .replace("\"", """)
12 .replace("'", "'")
13 }
14
15 #[macro_export]
16 macro_rules! vec_string {
17 ($($x : expr), + $(,) ? ) => {
18 vec![$($x), +].iter()
19 .map(|s| s.to_string())
20 .collect()
21 }
22 }
23
24 #[cfg(test)]
25 mod test_split_string {
26 use super::*;
27
28 #[test]
29 fn should_return_true_when_code_block_starts_with_lang_name() {
30 let input = r#"this
31is
32a"#;
33 let result = split_string(input.to_string());
34 let expected: Vec<String> = ["this", "is", "a"].iter().map(|&s| s.into()).collect();
35 assert_eq!(result, expected);
36 }
37 }
38
39 #[cfg(test)]
40 mod test_escape_code_string {
41 use super::*;
42
43 #[derive(Debug)]
44 struct TestCase {
45 it: String,
46 input: String,
47 expected: String,
48 }
49
50 #[test]
51 fn test() {
52 let test_cases = [
53 TestCase {
54 it: String::from("should correctly escape &"),
55 input: String::from("true && false"),
56 expected: String::from("true && false"),
57 },
58 TestCase {
59 it: String::from("should correctly escape <"),
60 input: String::from("<script"),
61 expected: String::from("<script"),
62 },
63 TestCase {
64 it: String::from("should correctly escape >"),
65 input: String::from("script>"),
66 expected: String::from("script>"),
67 },
68 TestCase {
69 it: String::from("should correctly escape '"),
70 input: String::from("'aaa'"),
71 expected: String::from("'aaa'"),
72 },
73 TestCase {
74 it: String::from("should correctly escape whole script tag"),
75 input: String::from(r#"<script src="/a/b.js">alert('aaa')</script>"#),
76 expected: String::from(
77 "<script src="/a/b.js">alert('aaa')</script>",
78 ),
79 },
80 ];
81 for test_case in test_cases.iter() {
82 let output = escape_code_string(&test_case.input);
83 assert_eq!(output, test_case.expected, "Failed: {}\n", test_case.it);
84 }
85 }
86 }
87}