Skip to main content

rumdl_lib/utils/
mod.rs

1//!
2//! Shared utilities for rumdl, including document structure analysis, code block handling, regex helpers, and string extensions.
3//! Provides reusable traits and functions for rule implementations and core linter logic.
4
5pub mod anchor_styles;
6pub mod blockquote;
7pub mod code_block_utils;
8// DocumentStructure has been merged into LintContext
9// pub mod document_structure;
10pub mod early_returns;
11pub mod element_cache;
12pub mod emphasis_utils;
13pub mod fix_utils;
14pub mod header_id_utils;
15pub mod jinja_utils;
16pub mod kramdown_utils;
17pub mod line_ending;
18pub mod markdown_elements;
19pub mod mkdocs_abbreviations;
20pub mod mkdocs_admonitions;
21pub mod mkdocs_attr_list;
22pub mod mkdocs_common;
23pub mod mkdocs_config;
24pub mod mkdocs_critic;
25pub mod mkdocs_definition_lists;
26pub mod mkdocs_extensions;
27pub mod mkdocs_footnotes;
28pub mod mkdocs_html_markdown;
29pub mod mkdocs_icons;
30pub mod mkdocs_patterns;
31pub mod mkdocs_snippets;
32pub mod mkdocs_tabs;
33pub mod mkdocs_test_utils;
34pub mod mkdocstrings_refs;
35pub mod pymdown_blocks;
36pub mod quarto_divs;
37pub mod range_utils;
38pub mod regex_cache;
39pub mod sentence_utils;
40pub mod skip_context;
41pub mod string_interner;
42pub mod table_utils;
43pub mod text_reflow;
44pub mod utf8_offsets;
45
46pub use code_block_utils::CodeBlockUtils;
47// pub use document_structure::DocumentStructure;
48pub use line_ending::{
49    LineEnding, detect_line_ending, detect_line_ending_enum, ensure_consistent_line_endings, get_line_ending_str,
50    normalize_line_ending,
51};
52pub use markdown_elements::{ElementQuality, ElementType, MarkdownElement, MarkdownElements};
53pub use range_utils::LineIndex;
54
55/// Check if a line is a definition list item (Extended Markdown)
56///
57/// Definition lists use the pattern:
58/// ```text
59/// Term
60/// : Definition
61/// ```
62///
63/// Supported by: PHP Markdown Extra, Kramdown, Pandoc, Hugo, and others
64pub fn is_definition_list_item(line: &str) -> bool {
65    let trimmed = line.trim_start();
66    trimmed.starts_with(": ")
67        || (trimmed.starts_with(':') && trimmed.len() > 1 && trimmed.chars().nth(1).is_some_and(|c| c.is_whitespace()))
68}
69
70/// Trait for string-related extensions
71pub trait StrExt {
72    /// Replace trailing spaces with a specified replacement string
73    fn replace_trailing_spaces(&self, replacement: &str) -> String;
74
75    /// Check if the string has trailing whitespace
76    fn has_trailing_spaces(&self) -> bool;
77
78    /// Count the number of trailing spaces in the string
79    fn trailing_spaces(&self) -> usize;
80}
81
82impl StrExt for str {
83    fn replace_trailing_spaces(&self, replacement: &str) -> String {
84        // Custom implementation to handle both newlines and tabs specially
85
86        // Check if string ends with newline
87        let (content, ends_with_newline) = if let Some(stripped) = self.strip_suffix('\n') {
88            (stripped, true)
89        } else {
90            (self, false)
91        };
92
93        // Find where the trailing spaces begin
94        let mut non_space_len = content.len();
95        for c in content.chars().rev() {
96            if c == ' ' {
97                non_space_len -= 1;
98            } else {
99                break;
100            }
101        }
102
103        // Build the final string
104        let mut result =
105            String::with_capacity(non_space_len + replacement.len() + if ends_with_newline { 1 } else { 0 });
106        result.push_str(&content[..non_space_len]);
107        result.push_str(replacement);
108        if ends_with_newline {
109            result.push('\n');
110        }
111
112        result
113    }
114
115    fn has_trailing_spaces(&self) -> bool {
116        self.trailing_spaces() > 0
117    }
118
119    fn trailing_spaces(&self) -> usize {
120        // Custom implementation to handle both newlines and tabs specially
121
122        // Prepare the string without newline if it ends with one
123        let content = self.strip_suffix('\n').unwrap_or(self);
124
125        // Count only trailing spaces at the end, not tabs
126        let mut space_count = 0;
127        for c in content.chars().rev() {
128            if c == ' ' {
129                space_count += 1;
130            } else {
131                break;
132            }
133        }
134
135        space_count
136    }
137}
138
139use std::collections::hash_map::DefaultHasher;
140use std::hash::{Hash, Hasher};
141
142/// Fast hash function for string content
143///
144/// This utility function provides a quick way to generate a hash from string content
145/// for use in caching mechanisms. It uses Rust's built-in DefaultHasher.
146///
147/// # Arguments
148///
149/// * `content` - The string content to hash
150///
151/// # Returns
152///
153/// A 64-bit hash value derived from the content
154pub fn fast_hash(content: &str) -> u64 {
155    let mut hasher = DefaultHasher::new();
156    content.hash(&mut hasher);
157    hasher.finish()
158}
159
160#[cfg(test)]
161mod tests {
162    use super::*;
163
164    #[test]
165    fn test_detect_line_ending_pure_lf() {
166        // Test content with only LF line endings
167        let content = "First line\nSecond line\nThird line\n";
168        assert_eq!(detect_line_ending(content), "\n");
169    }
170
171    #[test]
172    fn test_detect_line_ending_pure_crlf() {
173        // Test content with only CRLF line endings
174        let content = "First line\r\nSecond line\r\nThird line\r\n";
175        assert_eq!(detect_line_ending(content), "\r\n");
176    }
177
178    #[test]
179    fn test_detect_line_ending_mixed_more_lf() {
180        // Test content with mixed line endings where LF is more common
181        let content = "First line\nSecond line\r\nThird line\nFourth line\n";
182        assert_eq!(detect_line_ending(content), "\n");
183    }
184
185    #[test]
186    fn test_detect_line_ending_mixed_more_crlf() {
187        // Test content with mixed line endings where CRLF is more common
188        let content = "First line\r\nSecond line\r\nThird line\nFourth line\r\n";
189        assert_eq!(detect_line_ending(content), "\r\n");
190    }
191
192    #[test]
193    fn test_detect_line_ending_empty_string() {
194        // Test empty string - should default to LF
195        let content = "";
196        assert_eq!(detect_line_ending(content), "\n");
197    }
198
199    #[test]
200    fn test_detect_line_ending_single_line_no_ending() {
201        // Test single line without any line endings - should default to LF
202        let content = "This is a single line with no line ending";
203        assert_eq!(detect_line_ending(content), "\n");
204    }
205
206    #[test]
207    fn test_detect_line_ending_equal_lf_and_crlf() {
208        // Test edge case with equal number of CRLF and LF
209        // Since LF count is calculated as total '\n' minus CRLF count,
210        // and the algorithm uses > (not >=), it should default to LF
211        let content = "Line 1\r\nLine 2\nLine 3\r\nLine 4\n";
212        assert_eq!(detect_line_ending(content), "\n");
213    }
214
215    #[test]
216    fn test_detect_line_ending_single_lf() {
217        // Test with just a single LF
218        let content = "Line 1\n";
219        assert_eq!(detect_line_ending(content), "\n");
220    }
221
222    #[test]
223    fn test_detect_line_ending_single_crlf() {
224        // Test with just a single CRLF
225        let content = "Line 1\r\n";
226        assert_eq!(detect_line_ending(content), "\r\n");
227    }
228
229    #[test]
230    fn test_detect_line_ending_embedded_cr() {
231        // Test with CR characters that are not part of CRLF
232        // These should not affect the count
233        let content = "Line 1\rLine 2\nLine 3\r\nLine 4\n";
234        // This has 1 CRLF and 2 LF (after subtracting the CRLF)
235        assert_eq!(detect_line_ending(content), "\n");
236    }
237}