diff options
| author | A Farzat <a@farzat.xyz> | 2026-06-20 13:50:34 +0300 |
|---|---|---|
| committer | A Farzat <a@farzat.xyz> | 2026-06-20 13:50:34 +0300 |
| commit | fc1b96579c24ca062d6a569139006fe475710ea6 (patch) | |
| tree | 13d6d16a340adbbd9941961bf5b38e6e8eea153d | |
| parent | 7ae5e1146684e6daeaf6522cb83a47bbcfefe87e (diff) | |
| download | repo2markdown-fc1b96579c24ca062d6a569139006fe475710ea6.tar.gz repo2markdown-fc1b96579c24ca062d6a569139006fe475710ea6.zip | |
Add module/function docs to language utils
| -rw-r--r-- | src/util/language.rs | 10 |
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("#!") |
