summaryrefslogtreecommitdiff
path: root/src/util/language.rs
diff options
context:
space:
mode:
authorA Farzat <a@farzat.xyz>2026-06-20 13:50:34 +0300
committerA Farzat <a@farzat.xyz>2026-06-20 13:50:34 +0300
commitfc1b96579c24ca062d6a569139006fe475710ea6 (patch)
tree13d6d16a340adbbd9941961bf5b38e6e8eea153d /src/util/language.rs
parent7ae5e1146684e6daeaf6522cb83a47bbcfefe87e (diff)
downloadrepo2markdown-fc1b96579c24ca062d6a569139006fe475710ea6.tar.gz
repo2markdown-fc1b96579c24ca062d6a569139006fe475710ea6.zip
Add module/function docs to language utils
Diffstat (limited to 'src/util/language.rs')
-rw-r--r--src/util/language.rs10
1 files changed, 10 insertions, 0 deletions
diff --git a/src/util/language.rs b/src/util/language.rs
index c9bd1ea..c0f9c0d 100644
--- a/src/util/language.rs
+++ b/src/util/language.rs
@@ -1,11 +1,20 @@
+//! Language detection for Markdown code fences.
+
use std::path::Path;
+/// Detects a Markdown code-fence language tag from a filename and file contents.
+///
+/// Extension-based detection is tried first. If the extension is unknown, the
+/// first line is inspected for a shebang.
+///
+/// Returns an empty string when no language is detected.
pub fn detect_language(filename: &Path, contents: &str) -> &'static str {
detect_from_extension(filename)
.or_else(|| detect_from_shebang(contents))
.unwrap_or("")
}
+/// Detects a language tag from the file extension.
fn detect_from_extension(filename: &Path) -> Option<&'static str> {
let ext = filename
.extension()
@@ -24,6 +33,7 @@ fn detect_from_extension(filename: &Path) -> Option<&'static str> {
}
}
+/// Detects a language tag from a shebang on the first line.
fn detect_from_shebang(contents: &str) -> Option<&'static str> {
if let Some(first_line) = contents.lines().next()
&& first_line.starts_with("#!")