Echo Writes Code

syntax.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
enum Language {
	Cmake,
	Cpp,
	Css,
	Html,
	Json,
	Lua,
	Markdown,
	Rust,
}

pub fn highlight(filename: &str, content: &str) -> String {
	// Our strategy is basically to determine the language through a heuristic, then highlight it if
	// we find a match.
	// If there's no match, or we're not sure, we don't highlight to be safe.

	match detect_language(filename, content) {
		Some(Cmake) => highlight_cmake(content),
		Some(Cpp) => highlight_cpp(content),
		Some(Css) => highlight_css(content),
		Some(Html) => highlight_html(content),
		Some(Json) => highlight_json(content),
		Some(Lua) => highlight_lua(content),
		Some(Markdown) => highlight_markdown(content),
		Some(Rust) => highlight_rust(content),
		None => content.to_string(),
	}
}

fn detect_language(filename: &str, content: &str) -> Option<Language> {
	if is_cmake(filename, content) {
		Some(Language::Cmake)
	} else if is_cpp(filename, content) {
		Some(Language::Cpp)
	} else if is_css(filename, content) {
		Some(Language::Css)
	} else if is_html(filename, content) {
		Some(Language::Html)
	} else if is_json(filename, content) {
		Some(Language::Json)
	} else if is_lua(filename, content) {
		Some(Language::Lua)
	} else if is_markdown(filename, content) {
		Some(Language::Markdown)
	} else if is_rust(filename, content) {
		Some(Language::Rust)
	} else {
		None
	}
}

fn is_cmake(filename: &str, _content: &str) -> bool {
	filename == "CMakeLists.txt" || filename.ends_with(".cmake")
}

fn is_cpp(filename: &str, _content: &str) -> bool {
	&[".hpp", ".inl", ".cpp"]
		.iter()
		.any(|&extension| filename.ends_with(extension))
}

fn is_css(filename: &str, _content: &str) -> bool {
	filename.ends_with(".css")
}

fn is_html(filename: &str, _content: &str) -> bool {
	filename.ends_with(".html")
}

fn is_json(filename: &str, _content: &str) -> bool {
	filename.ends_with(".json")
}

fn is_lua(filename: &str, _content: &str) -> bool {
	filename.ends_with(".lua")
}

fn is_markdown(filename: &str, _content: &str) -> bool {
	&[".md", ".markdown"]
		.iter()
		.any(|&extension| filename.ends_with(extension))
}

fn is_rust(filename: &str, _content: &str) -> bool {
	filename.ends_with(".rs")
}

fn highlight_cmake(content: &str) -> String {
}

fn highlight_cpp(content: &str) -> String {
}

fn highlight_css(content: &str) -> String {
}

fn highlight_html(content: &str) -> String {
}

fn highlight_json(content: &str) -> String {
}

fn highlight_lua(content: &str) -> String {
}

fn highlight_markdown(content: &str) -> String {
}

fn highlight_rust(content: &str) -> String {
}