summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA Farzat <a@farzat.xyz>2026-06-07 19:57:48 +0300
committerA Farzat <a@farzat.xyz>2026-06-07 19:59:11 +0300
commit0d6d889dfe32f793f63027e1d773aebe65c38c6f (patch)
tree4321f30118a5a988eb73530bc870c315c37698b9
parent94649d3423da0cf02b58c784941cb614a0cfe487 (diff)
downloadrepo2markdown-0d6d889dfe32f793f63027e1d773aebe65c38c6f.tar.gz
repo2markdown-0d6d889dfe32f793f63027e1d773aebe65c38c6f.zip
Start adding shebang filetype detection
-rw-r--r--src/renderer.rs37
1 files changed, 34 insertions, 3 deletions
diff --git a/src/renderer.rs b/src/renderer.rs
index 14d30d6..164012e 100644
--- a/src/renderer.rs
+++ b/src/renderer.rs
@@ -64,7 +64,7 @@ impl<W: Write> Renderer<W> {
fn write_file(&mut self, filename: &Path, contents: &str) -> std::io::Result<()> {
let name = display_path(filename);
let fence = outer_backticks(contents);
- let language = detect_language(filename);
+ let language = detect_language(filename, contents);
self.log_file_render(filename);
writeln!(self.output)?;
writeln!(self.output, "## File: {}", name)?;
@@ -142,7 +142,7 @@ fn human_readable_size(bytes: u64) -> String {
}
}
-fn detect_language(filename: &Path) -> &'static str {
+fn detect_language(filename: &Path, contents: &str) -> &'static str {
let ext = filename
.extension()
.and_then(|e| e.to_str())
@@ -152,7 +152,14 @@ fn detect_language(filename: &Path) -> &'static str {
Some("rs") => "rust",
Some("py") => "python",
Some("json") => "json",
- _ => "",
+ _ => {
+ if let Some(first_line) = contents.lines().next()
+ && first_line.contains("python")
+ {
+ return "python";
+ }
+ ""
+ }
}
}
@@ -246,6 +253,18 @@ mod tests {
}
#[test]
+ fn txt_file_has_no_language_fence() {
+ let mut output = Vec::new();
+ let mut renderer = Renderer::new(&mut output);
+
+ let input = Cursor::new("hello world");
+ renderer.render_file(Path::new("hello.txt"), input).unwrap();
+ let expected = "\n## File: hello.txt\n```\nhello world\n```\n";
+
+ assert_eq!(String::from_utf8(output).unwrap(), expected);
+ }
+
+ #[test]
fn python_file_has_python_language_fence() {
let mut output = Vec::new();
let mut renderer = Renderer::new(&mut output);
@@ -282,6 +301,18 @@ mod tests {
}
#[test]
+ fn python_file_detected_from_shebang() {
+ let mut output = Vec::new();
+ let mut renderer = Renderer::new(&mut output);
+
+ let input = Cursor::new("#! /bin/python3\nprint('hello')");
+ renderer.render_file(Path::new("hello"), input).unwrap();
+ let expected = "\n## File: hello\n```python\n#! /bin/python3\nprint('hello')\n```\n";
+
+ assert_eq!(String::from_utf8(output).unwrap(), expected);
+ }
+
+ #[test]
fn renderer_places_placeholder_for_large_files_by_default() {
let mut output = Vec::new();
let mut renderer = Renderer::new(&mut output).with_max_file_size(5); // smaller than file