summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-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("#!")