summaryrefslogtreecommitdiff
path: root/src/util/language.rs
diff options
context:
space:
mode:
authorA Farzat <a@farzat.xyz>2026-06-07 21:04:37 +0300
committerA Farzat <a@farzat.xyz>2026-06-07 21:04:37 +0300
commit133dba2bc968c9081144887ceb19bd9711c16df6 (patch)
tree5d0db2eda1e4242f815105e1e26da6a81493e249 /src/util/language.rs
parent99d21463574bb88fa86f176c2e4ad154be5e1353 (diff)
downloadrepo2markdown-133dba2bc968c9081144887ceb19bd9711c16df6.tar.gz
repo2markdown-133dba2bc968c9081144887ceb19bd9711c16df6.zip
Ensure shebangs properly start with #!
Diffstat (limited to 'src/util/language.rs')
-rw-r--r--src/util/language.rs23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/util/language.rs b/src/util/language.rs
index fae7bf9..8819a1e 100644
--- a/src/util/language.rs
+++ b/src/util/language.rs
@@ -21,7 +21,9 @@ fn detect_from_extension(filename: &Path) -> Option<&'static str> {
}
fn detect_from_shebang(contents: &str) -> Option<&'static str> {
- if let Some(first_line) = contents.lines().next() {
+ if let Some(first_line) = contents.lines().next()
+ && first_line.starts_with("#!")
+ {
if first_line.contains("python") {
return Some("python");
}
@@ -31,3 +33,22 @@ fn detect_from_shebang(contents: &str) -> Option<&'static str> {
}
None
}
+
+#[cfg(test)]
+mod tests {
+ use std::path::Path;
+
+ use super::detect_language;
+
+ #[test]
+ fn non_shebang_line_does_not_trigger_detection() {
+ let contents = "this mentions bash but is not a shebang";
+ assert_eq!(detect_language(Path::new("file"), contents), "");
+ }
+
+ #[test]
+ fn detects_python_from_shebang() {
+ let contents = "#!/usr/bin/python3\nprint('hi')";
+ assert_eq!(detect_language(Path::new("file"), contents), "python");
+ }
+}